NGINX rewrite always returns 302

nginx, url-rewriting

Solution

This behaviour is actually well described in the rewrite documentation:

If a replacement string starts with “http://” or “https://”, the processing stops and the redirect is returned to a client.

Remove the scheme and host part and rewrite to only the absolute path instead. Also replace `break` with `last`:

last stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;

rewrite /alt/digest/rewrite /digest/wms last;

Problem

I am completely stumped by the behavior of nginx rewrite. I have the following 2 location blocks - the first one simply rewrites so that the second one gets called. ``` location /alt/digest/rewrite { rewrite /alt/digest/rewrite http://$server_name/digest/wms break; } location /digest/wms { ... } ``` But I always get the following back on my curl app, when I hit http://server_name/alt/digest/rewrite ``` <html> <head><title>302 Found</title></head> <body bgcolor="white"> <center><h1>302 Found</h1></center> <hr><center>nginx/1.6.2</center> </body> </html> ``` Any help would be appreciated.

Original source