Marc
Marc

Reputation: 14313

How to Rewrite Internally in NGINX

I want to rewrite a URL location /private/ping to another location /ping once I've done some basic_auth checks.

When I call http://localhost:8080/ping I get OK. When I call http://localhost:8080/private/ping I get OK only with the #3 configuration. The other 2 configurations return 404.

I know NGINX is rewriting /private/ping to /ping successfully because the #3 configuration returns OK but this generates an extra unnecessary HTTP request.

Why does NGINX not reprocess it's own rewrites checking them against existing locations or how can I make it do so?

server {

  listen 8080;

  location = /ping {
    return 200 'OK';
  }

  location /private/ {
    # basic_auth stuff here
    #rewrite ^/private/(.*) /$1 break; #1 No
    rewrite ^/private/(.*) /ping break; #2 No
    #proxy_pass http://localhost:8080/; #3 Yes
  }

Upvotes: 1

Views: 1635

Answers (1)

Danila Vershinin
Danila Vershinin

Reputation: 9885

Use rewrite ... last to restart the search for another location based on rewritten URI.

Upvotes: 1

Related Questions