How to Create NGINX Redirects

NGINX Redirects on NGINX Web Server

NGINX can be used as a web server (in lieu of Apache) or proxy server. For those using it as a web server, creating NGINX redirects requires editing server configuration files. This is different from Apache which also checks .htaccess files during web server requests. NGINX doesn’t use .htaccess. Need to know how to redirect HTTP to HTTPS in NGINX? Don’t worry. It’s a simple process, as easy as hiding your NGINX server version. Below we’ll cover how to:

If you don’t need cPanel, don't pay for it. Only pay for what you need with our scalable Cloud VPS Hosting.

check markCentOS, Debian, or Ubuntu check markNo Bloatware check markSSH and Root Access

Create NGINX Redirects

SSH into your NGINX web server as user root. Edit your NGINX configuration file using Nano, Vim, Emacs, etc. We’ll be editing domain configuration files in the sites-enabled directory as it is the easiest method to create NGINX redirects. The file may state default or take the name of the domain.

nano /etc/nginx/sites-enabled/default
vim /etc/nginx/sites-enabled/example.com

Ignoring the comment lines, the default file will resemble the code block below:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }
}

Most changes below can all be done within existing code. Just remember to restart NGINX after making changes for them to take effect.

systemctl restart nginx

To save time troubleshooting, negate caching issues by testing changes in a private browsing session.

Redirect HTTP to HTTPS

To force encrypted connections, add the following to a new line under server_name _;:

return 301 https://$host$request_uri;

You can install a free domain-validated SSL certificate with Certbot.

Redirect to Force www

Deciding to explicitly use “www” or not for your website improves search engine optimization (SEO). First, add the following above the existing server { line:

server {
        listen       80;
        server_name  www.domain.com;
}

If you want to force HTTPS, add this line above the closing } bracket:

return 301 https://www.domain.com$request_uri;

Finally, add the following to a new line under server_name _;:

return 301 http://www.domain.com$request_uri;

Redirect a Domain to Another Domain

This is useful when migrating your website to a newer domain or subdomain (e.g. /blog or /forum). Add the following to a new line under server_name _;:

return 301 https://newdomain.com$request_uri;

Permanent Redirect for a Single File

To redirect a single page or file URL, add the following to a new line under server_name _;:

rewrite ^/page1.html$ /page2.html permanent;

Learn more from our Cloud Server Hosting Product Guide.

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+