How to Create NGINX Redirects Updated on November 7, 2025 by Carrie Smaha 2 Minutes, 22 Seconds to Read 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. 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.comnano /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; } }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 nginxsystemctl 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;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; }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;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;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;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;rewrite ^/page1.html$ /page2.html permanent; Share this Article Carrie Smaha Senior Manager Marketing Operations Carrie enjoys working on demand generation and product marketing projects that tap into multi-touch campaign design, technical SEO, content marketing, software design, and business operations. More Articles by Carrie Related Articles HTTP Strict Transport Security (HSTS) in NGINX Basic NGINX Commands How to Manage NGINX How to Create NGINX Redirects Advanced NGINX Stack Configuration for VPS and Dedicated Servers How to Remove NGINX From Your Server What is NGINX? How to Fix Nextcloud Not Working With NGINX Hide Your NGINX Server Version How to Install NGINX on cPanel