Note: This article was originally published in 2013. Some steps, commands, or software versions may have changed. Check the current NginX documentation for the latest information.

In this step-by-step guide, you’ll learn redirect an url in nginx. Nginx is a high-performance HTTP server and reverse proxy, known for its stability, rich feature set, and low resource consumption.

Prerequisites

Before you begin, make sure you have:

  • A Linux server with Nginx installed
  • Root or sudo access to the server
  • Basic understanding of web server configuration

How to: Redirect an URL in (http://nginx.org/ “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; server_name test.com; return 301 $scheme://www.test.com$request_uri; }

II. Redirect an (http://www.microsoft.com/exchange “Microsoft Exchange Server”) Autodiscover subfolder to another domain:

#Redirect Autodiscover Requests location ~* ^/Autodiscover { # proxy_pass <https://outlook.office365.com:443>; # proxy_cache_bypass 1; return 301 $scheme://outlook.office365.com$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).

(http://img.zemanta.com/zemified_h.png?x-id=b15c0b5a-6eef-4fa7-ba5a-a15e4d093adf)](http://www.zemanta.com/?px “Enhanced by Zemanta”)

Summary

You’ve successfully learned redirect an url in nginx. If you run into any issues, double-check the prerequisites and ensure your NginX environment is properly configured.