Setting up an nginx redirect wrong doesn’t just frustrate users. It tanks your SEO rankings, creates redirect loops that crash browsers, and leaves old URLs returning 200s when they shouldn’t. Get the directives right the first time.
What Is an Nginx Redirect?
An nginx redirect is a server-side configuration that sends visitors and search engines from one URL to another. Nginx evaluates incoming requests against server blocks and location blocks, then decides whether to serve content, proxy it, or redirect it to a new destination.
301 vs 302: Which One to Use
301 is permanent. Use it when a URL has moved forever and you want search engines to transfer ranking signals to the new location. 302 is temporary. Use it for maintenance windows, A/B testing, or situations where the original URL will return. Browsers cache 301s aggressively, so don’t use them for anything you might reverse.
Prerequisites and File Locations
You need root or sudo access to edit nginx configuration files. The main config lives at /etc/nginx/nginx.conf. Site-specific configs go in /etc/nginx/sites-available/, with symlinks in /etc/nginx/sites-enabled/ to activate them. Always back up before editing:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakHow to Configure Nginx Redirects
Nginx offers three main directives for redirects: return, rewrite, and try_files. Each has a specific use case.
Using the return Directive
The return directive is the most efficient option. It stops processing immediately and sends the status code and URL to the client. Syntax:
return [status_code] [URL];Example: redirect all traffic from an old domain to a new one:
server { listen 80; server_name old-domain.com; return 301 https://new-domain.com$request_uri; }The $request_uri variable preserves the path and query string, so /blog/post becomes https://new-domain.com/blog/post.
Using the rewrite Directive
The rewrite directive uses regex to match and transform URLs. It’s more flexible than return but slower and harder to debug:
rewrite ^/old-path(.*)$ /new-path$1 permanent;The permanent flag makes it a 301. Use redirect for 302. If you don’t need regex matching, use return instead. Rewrite rules evaluate in sequence and can trigger multiple rewrites per request.
Using try_files for Fallback Redirects
try_files checks for files in order and falls back to a specified location if none exist:
location / { try_files $uri $uri/ /index.php?$query_string; }This isn’t a traditional redirect but handles cases where clean URLs need to fall back to a front controller.
Common Nginx Redirect Use Cases
Redirect HTTP to HTTPS
The standard pattern for forcing HTTPS:
server { listen 80; server_name example.com www.example.com; return 301 https://example.com$request_uri; }Redirect www to Non-www
Canonical redirect to strip the www subdomain:
server { listen 443 ssl; server_name www.example.com; return 301 https://example.com$request_uri; }Redirect a Specific URL or Path
Single URL redirect using a location block:
location = /old-page { return 301 /new-page; }The = modifier makes an exact match, which is faster than prefix or regex matching.
Redirect an Entire Domain
Catch-all redirect for domain migrations:
server { listen 80; server_name old-site.com; return 301 https://new-site.com$request_uri; }Nginx Redirect Best Practices
Avoiding Redirect Chains and Loops
A redirect chain is when URL A redirects to B, which redirects to C. Each hop adds latency and can confuse crawlers. Keep chains to a maximum of one redirect. A redirect loop is when A redirects to B and B redirects back to A, which kills the request entirely. Always test with curl:
curl -I http://example.comTesting Your Configuration
Before reloading nginx, validate the config:
sudo nginx -tIf it passes, reload without dropping connections:
sudo systemctl reload nginxNever skip validation. A syntax error in the config file brings down the entire server.
FAQ: Nginx Redirect
return is faster because it stops processing immediately. rewrite runs through the full nginx processing cycle and supports regex transformations. Use return for simple redirects and rewrite only when you need regex-based URL manipulation.
Add a server block listening on port 80 with: return 301 https://yourdomain.com$request_uri; That sends all HTTP traffic to the HTTPS equivalent permanently.
Create a server block for www.yourdomain.com and return 301 https://yourdomain.com$request_uri; Make sure your SSL certificate covers both the www and non-www versions.
Run sudo nginx -t to validate syntax. Then use curl -IL https://yourdomain.com/path to trace the full redirect chain before reloading the live server.