How to: Redirect an URL in NGINX

How to: Redirect an URL in NGINX

One might need to redirect to another URL in a number of scenarios. I will cover two examples for scenarios I have come across and how I addresses those issues:

I. Redirect from a naked domain to the www subdomain:

server {
listen 80;
return 301 $scheme://$request_uri;

II. Redirect an Exchange Server Autodiscover subfolder to another domain:

#Redirect Autodiscover Requests
location ~* ^/Autodiscover
#               proxy_pass    ;
#               proxy_cache_bypass      1;
return 301 $scheme://$request_uri;

For this last example you can chose 1 of two approaches: The first one which is commented out does a proxy to your Exchange Server AutoDiscover service. The problem with this approach is that you need to have the correct SSL certificate for your domain installed or you will have a number of warnings thrown out at you. This approach is useful if you want to cache responses, etc. My recommended approach is the one that is not commented out which is a normal redirect as the one you saw in the first example. Doing a redirect like this will avoid having Outlook trying to hit your server every now and then to check for the AutoDiscover directives. This is useful if you are hosting your domain in a non-exchange server host (you lack the AutoDiscover folder there).

Enhanced by Zemanta

Leave a Reply