sodcalculator

10 Tech How-To

Nginx Redirect Setup on a Linux VPS

Guide 10-15048 · Updated · Step by step

This guide walks you through setting up nginx redirects on a Linux VPS, covering permanent (301) and temporary (302) redirects, redirecting HTTP to HTTPS, and redirecting a domain to www or non-www. You’ll edit nginx configuration files, test your changes, and reload nginx. You’ll need root or sudo access to your VPS and a basic familiarity with the command line.

What You’ll Need

  • A Linux VPS with nginx already installed (if not, install it with your package manager, e.g., sudo apt install nginx on Ubuntu/Debian).
  • Root or sudo access to edit files in /etc/nginx/ and reload the service.
  • A text editor on the server (nano, vim, or similar).
  • Your domain name’s DNS pointing to the VPS’s IP address (for domain redirects).

Step-by-Step

  1. Locate your nginx configuration files. On most distributions, the main config is /etc/nginx/nginx.conf, and site-specific configs live in /etc/nginx/sites-available/ and are symlinked into /etc/nginx/sites-enabled/. On some systems (like CentOS/RHEL), you’ll find .conf files directly in /etc/nginx/conf.d/. Use ls /etc/nginx/ to see the layout.
  2. Open the server block you want to edit. For a domain, edit its config file, e.g., sudo nano /etc/nginx/sites-available/example.com. If you’re just testing, you can edit the default server block.
  3. Understand the server block structure. A basic server block listens on a port and responds to a domain. You’ll be adding a return directive or a rewrite rule inside it.
  4. Set up a permanent (301) redirect. To redirect one URL to another permanently, add a return 301 line inside the server block. For example, to redirect old-page to new-page:
    server {
        listen 80;
        server_name example.com;
        location = /old-page {
            return 301 https://example.com/new-page;
        }
    }

    Or redirect the entire domain to a new domain: return 301 https://newdomain.com$request_uri; – the $request_uri preserves the path.

  5. Set up a temporary (302) redirect. Use return 302 for temporary moves, like during maintenance. Syntax is identical to the 301 case.
  6. Redirect HTTP to HTTPS. Create a server block that listens on port 80 and redirects all traffic to HTTPS. Place this before your HTTPS server block:
    server {
        listen 80;
        server_name example.com www.example.com;
        return 301 https://example.com$request_uri;
    }

    Make sure your HTTPS server block (listening on 443) is configured with SSL certificates.

  7. Redirect www to non-www (or vice versa). In your server block, add a server_name for the variant you want to redirect from, and use return 301 to the preferred domain. For example, to redirect www to non-www:
    server {
        listen 80;
        server_name www.example.com;
        return 301 http://example.com$request_uri;
    }

    Then have a separate server block for example.com that serves the content.

  8. Use rewrite for complex patterns (optional). For regex-based redirects, use rewrite. Example: rewrite ^/old/(.*)$ /new/$1 permanent; – this redirects /old/anything to /new/anything with a 301. The permanent flag is equivalent to 301; use redirect for 302.
  9. Test your configuration. Run sudo nginx -t to check for syntax errors. If it says “syntax is ok� and “test is successful�, you’re good. If not, fix the errors before continuing.
  10. Reload nginx. Apply the changes with sudo systemctl reload nginx (or sudo service nginx reload on older systems). This doesn’t drop connections, so it’s safe to do anytime.
  11. Verify the redirect. Use curl -I http://example.com/old-page to see the HTTP status code and the Location header. You should see 301 Moved Permanently and the new URL.

Common Mistakes

  • Forgetting to test before reloading. A syntax error can bring down your site. Always run nginx -t first.
  • Using rewrite when return is simpler. For simple redirects, return is faster and less error-prone. Use rewrite only for regex-based path changes.
  • Not preserving the path. If you redirect the whole domain, include $request_uri to keep the path. Without it, example.com/page would go to newdomain.com instead of newdomain.com/page.
  • Creating a redirect loop. For example, if you redirect www to non-www in one server block and also redirect non-www to www in another, you’ll loop. Make sure your redirect target is the final destination.
  • Editing the wrong server block. If you have multiple server blocks, you might be editing one that isn’t being used. Check the server_name and the port.

Tips

  • Use 301 for permanent changes. Search engines will update their indexes. Use 302 for temporary changes to avoid losing SEO value.
  • Keep redirects organized. Comment your config files so you know why a redirect exists.
  • Use a separate server block for HTTP-to-HTTPS. This keeps your HTTPS config clean and makes it easy to add HSTS headers later.
  • Test with curl. The -I flag shows headers only, which is perfect for checking redirects without downloading content.
  • Back up your config. Before making changes, copy the file: sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/example.com.bak.

Troubleshooting

  • Redirect not working. Check that the server block you edited is actually the one handling the request. Use curl -I http://example.com and see if the response comes from your server. Also, check that nginx reloaded successfully – if you got an error, the old config might still be running.
  • “nginx: [emerg]� error during nginx -t. This indicates a syntax error. Read the error message carefully – it will tell you the file and line number. Common issues are missing semicolons, unmatched braces, or invalid directives.
  • Redirect loops. Use curl -I -L http://example.com to follow redirects and see where it cycles. Check your server blocks for conflicting redirects. Also, if you have a CDN or proxy in front, it might be adding its own redirects.
  • HTTPS redirect not working. Make sure your HTTPS server block exists and listens on port 443. If you don’t have SSL configured, the redirect to https:// will fail. Test with curl -I http://example.com and look for the Location header.
  • Changes not taking effect. Did you reload nginx? sudo systemctl reload nginx is required after every config change. Also, check if your browser is caching the old redirect – try an incognito window or use curl.

Frequently Asked Questions

What’s the difference between a 301 and 302 redirect?

A 301 is permanent – browsers and search engines will replace the old URL with the new one. A 302 is temporary – the old URL remains in place and the redirect is only for that request. Use 301 for permanent moves, 302 for temporary ones.

Can I redirect a single page without affecting the rest of the site?

Yes, use a location block with an exact match (location = /old-page) or a regex (location ~ ^/old/) and put the return inside it. This only affects those URLs.

How do I redirect all HTTP traffic to HTTPS?

Add a server block listening on port 80 with a return 301 https://yourdomain.com$request_uri;. This must be in addition to your HTTPS server block.

Should I use return or rewrite?

Use return for simple redirects – it’s faster and simpler. Use rewrite only when you need to change the URL path using regular expressions. For example, rewrite ^/old/(.*)$ /new/$1 permanent;.

How do I redirect a domain to www or non-www?

Create a server block for the domain you want to redirect from, and return 301 to the preferred domain with $request_uri. For example, to redirect www to non-www, use server_name www.example.com; return 301 http://example.com$request_uri;.

Why is my redirect not working after I reload nginx?

Check the nginx error log (/var/log/nginx/error.log) for any messages. Also, verify that the server block you edited is the one being used – maybe there’s another server block that matches the request first. Use curl -I to see the actual response.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →