0

I'm trying to achieve the below in nginx:

Hit nginx on http://<nginx-host>:81/admin/metrics and have nginx proxy it to http://127.0.0.1:8001 with the /admin/metrics rewritten to /metrics. The actual proxied admin app would receive http://127.0.0.1:8001/metrics.

So, http://<nginx-host>:81/admin/metrics => http://127.0.0.1:8001/metrics, and without causing a redirect.

This is what I have so far:

#admin app
upstream admin_backend {
    server 127.0.0.1:8001;
    keepalive 16;
}

#host /admin paths on another port
server {
  listen 81;

  #pass everything to backend admin app that matches /admin/* with /admin/ removed
  location ~ /admin/ {
    limit_except GET HEAD POST PUT { }

    proxy_set_header Host $host;
    proxy_http_version  1.1;
    proxy_pass http://admin_backend;

    rewrite ^/admin/(.*)$ /$1 last;
  }
}

This is not working. nginx is not proxying the request back to the admin app and keeps returning a 404.

What am I missing here?

2 Answers 2

1

I cross-posted this question on the nginx users forum, and got a solution from nanaya (thank you!).

Basically, all I needed to do was replace last with break in the rewrite directive:

So,

rewrite ^/admin/(.*)$ /$1 last;

became

rewrite ^/admin/(.*)$ /$1 break;
Sign up to request clarification or add additional context in comments.

Comments

0

redirect api and files request to backend

location ~ /api/ {
    proxy_set_header    X-Forwarded-Host    $host;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    proxy_pass http://server:8009;
    rewrite ^/api/(.*)$ /$1 break;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.