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 nginxon 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
- 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.conffiles directly in/etc/nginx/conf.d/. Usels /etc/nginx/to see the layout. - 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. - Understand the
serverblock structure. A basic server block listens on a port and responds to a domain. You’ll be adding areturndirective or arewriterule inside it. - Set up a permanent (301) redirect. To redirect one URL to another permanently, add a
return 301line inside the server block. For example, to redirectold-pagetonew-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_uripreserves the path. - Set up a temporary (302) redirect. Use
return 302for temporary moves, like during maintenance. Syntax is identical to the 301 case. - 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.
- Redirect www to non-www (or vice versa). In your server block, add a
server_namefor the variant you want to redirect from, and usereturn 301to 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.comthat serves the content. - Use
rewritefor complex patterns (optional). For regex-based redirects, userewrite. Example:rewrite ^/old/(.*)$ /new/$1 permanent;– this redirects/old/anythingto/new/anythingwith a 301. Thepermanentflag is equivalent to 301; useredirectfor 302. - Test your configuration. Run
sudo nginx -tto check for syntax errors. If it says “syntax is ok� and “test is successful�, you’re good. If not, fix the errors before continuing. - Reload nginx. Apply the changes with
sudo systemctl reload nginx(orsudo service nginx reloadon older systems). This doesn’t drop connections, so it’s safe to do anytime. - Verify the redirect. Use
curl -I http://example.com/old-pageto see the HTTP status code and theLocationheader. You should see301 Moved Permanentlyand the new URL.
Common Mistakes
- Forgetting to test before reloading. A syntax error can bring down your site. Always run
nginx -tfirst. - Using
rewritewhenreturnis simpler. For simple redirects,returnis faster and less error-prone. Userewriteonly for regex-based path changes. - Not preserving the path. If you redirect the whole domain, include
$request_urito keep the path. Without it,example.com/pagewould go tonewdomain.cominstead ofnewdomain.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_nameand 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
-Iflag 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.comand 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.comto 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 withcurl -I http://example.comand look for theLocationheader. - Changes not taking effect. Did you reload nginx?
sudo systemctl reload nginxis required after every config change. Also, check if your browser is caching the old redirect – try an incognito window or usecurl.
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
- Use Microsoft Designer in Teams Chat
- Not Working on Mac? Fixes for Common Issues
- How to Put Windows 10 on a USB Flash Drive
- Zoom Error Code Fixes on Windows
- Remove Core Services UI Agent on Mac
- How to Use Live Text in Video on iPhone