Is there a way to get nginx to treat a custom variable the same as it treats the $scheme in a rewrite rule? For instance:
rewrite ^/redirect http://example.com;
# and
rewrite ^/redirect_with_scheme $scheme://example.com;
will both result in a 302 redirect being sent to the client. However, this:
set $real_scheme $scheme;
rewrite ^/redirect_with_real_scheme $real_scheme://example.com;
will result in a server side rewrite to a file that doesn't necessarily exist and then a 404 to the client. For what it's worth, this:
set $real_scheme $scheme;
rewrite ^/redirect_with_real_scheme $real_scheme://example.com redirect;
will give the desired result of sending a 302 to the client. Unfortunately, I have a lot of redirects set up this way and I would rather not update all of them with the redirect
directive if it's not necessary.
If it's helpful, I'm using Nginx 1.6.2.
Best Answer
The documentation states:
If you start the URL with some other variable, you will need to express the intention to
redirect
explicitly.