Ilan
Ilan

Reputation: 579

Nginx - Redirect all to specific endpoint

I have some trouble to configure my Nginx to rewrite all the requests to a specific endpoint.

I have a Nodejs app running in port 8080 and I would like all the http://SERVER_IP/* requests to show http://127.0.0.1:8080/statuscheck

I tried (in the /etc/nginx/sites-available/default file):

location / {
    rewrite ^.*$ /statuscheck last;
    proxy_pass http://127.0.0.1:8080;
}

But I got 500. The error logs show:

[error] 25756#0: *115803 rewrite or internal redirection cycle while processing "/statuscheck" 

Thanks for the help.

Upvotes: 1

Views: 932

Answers (1)

Richard Smith
Richard Smith

Reputation: 49692

The regular expression in your rewrite directive also matches /statuscheck so any rewrite or redirect that causes the expression to be evaluated more than once will cause a loop. That includes the flag parameters: last, redirect and permanent.

However, the break flag parameter stops processing rewrite directives and continues processing the URL within the current location. For example:

location / {
    rewrite ^ /statuscheck break;
    proxy_pass http://127.0.0.1:8080;
}

See this document for details.

Upvotes: 1

Related Questions