Rewrite or Redirect URL with NGINX
This is an article that originally appeared on RedirectURL, a site I built a while ago. Find out the history of that site here
With a few steps, you can create redirects using your NGINX server configuration. Let’s see how.
Locate Your NGINX Configuration File
We’re going to make our changes to the
nginx.conf file. Depending on your operating system, this might be located in one of many places:
Note where the file is. We’ll make changes to it soon.
Configuring the Server Redirect
For this example, we’re going to stick with a very common scenario that’s very simple.
We want to redirect pets.com to https://bigboxstore.com/pets with a permanent 301 HTTP code.
Writing the Rule and Placing it Properly
nginx.conf file you found in the previous section. You’ll need to find the
server block that matches your site’s domain name.
Inside of the
server block, you can write the following:
rewrite ^/(.*)$ https://bigboxstore.com/pets permanent;
Finally, you need to reload your NGINX server after the config changes. Depending on your service install and configuration, you will need to run one of these commands (depending on your operating system):
sudo nginx -s reload
sudo /usr/local/nginx/sbin -s reload
sudo /etc/init.d/nginx reload