How to Set Up Nginx Redirects

You've moved a page, merged two domains, or finally switched to HTTPS — and now you need the old URLs to land somewhere useful instead of throwing a 404. Someone tells you "just add a redirect in Nginx," you open the config file, and you're staring at server and location blocks with no idea which directive is safe to touch.

The good news is that Nginx redirects are one of the more forgiving parts of server configuration once you know the two directives that do almost all the work. You don't need to understand the full Nginx module system — you need one correct block, a test command, and a reload.

Quick Answer

To set up an Nginx redirect, add a return 301 directive inside the relevant server or location block pointing to the new URL, save the config file, run nginx -t to check for syntax errors, then reload Nginx with systemctl reload nginx. Use return 301 for permanent moves and return 302 only for temporary ones.

What is an Nginx redirect?

An Nginx redirect is a directive placed inside a server or location block that tells a visitor's browser (or a search engine crawler) to request a different URL than the one it asked for, along with an HTTP status code explaining why.

Everything else — domain migrations, HTTPS enforcement, www normalization — is a variation on those two directives applied to a specific server_name or path.

Why getting redirects right matters

A redirect that's misconfigured doesn't always fail loudly — it often just quietly costs you traffic or rankings:

📊 Quick stat Nginx's own documentation recommends return over rewrite for any redirect that doesn't need pattern matching, specifically because rewrite is evaluated on every request and is a more common source of misconfiguration and redirect loops.

Step-by-step: setting up a redirect

  1. Identify the old URL and the new URL. Write down exactly what should redirect to what, including whether it's a single page, a whole path, or an entire domain.
  2. Open the site's Nginx config file. This is usually at /etc/nginx/sites-available/your-site — edit it with sudo nano or your preferred editor.
  3. Find or create the relevant server block. For a single page, add the redirect inside a location block; for a whole domain or protocol change, add it inside the server block itself.
  4. Add the return directive. For example: location /old-page { return 301 /new-page; } for a single path, or return 301 https://$host$request_uri; for an HTTP-to-HTTPS redirect.
  5. Test the configuration. Run sudo nginx -t — this checks the syntax without applying anything, and will point to the exact line if something's wrong.
  6. Reload Nginx. Run sudo systemctl reload nginx (or sudo nginx -s reload) to apply the change without dropping active connections.
  7. Verify the redirect. Run curl -I https://yoursite.com/old-page and confirm the response shows the correct status code and a Location header pointing to the new URL.
# Example: single page redirect location = /old-page { return 301 /new-page; } # Example: HTTP to HTTPS, entire domain server { listen 80; server_name yoursite.com www.yoursite.com; return 301 https://$host$request_uri; }
Try the Rebrixe Nginx Redirect Generator — free Fill in your old and new URLs, get a ready-to-paste server block. No syntax memorizing required.
Generate Redirect Rule →

Common mistakes

1. Using rewrite when return would do the job

rewrite is built for pattern matching with regular expressions. For a straightforward one-to-one redirect, it adds unnecessary processing and unnecessary complexity — return is faster and harder to get wrong.

2. Creating a redirect loop

This usually happens when the destination URL falls inside the same location block that's issuing the redirect, or when an HTTPS redirect sits behind a proxy that doesn't correctly forward the original protocol, so Nginx thinks every request is still on HTTP and keeps redirecting it.

3. Reaching for 302 on a permanent move

A 302 tells search engines the old URL might come back, so they keep it in the index instead of transferring its ranking signals. If the move is permanent, the status code should say so.

4. Editing the config and forgetting to reload

Saving the file doesn't apply anything on its own. Without running nginx -t and then reloading, the live server keeps serving the old configuration until it's explicitly told to pick up the change.

5. Mismatched trailing slashes

location /old-page and location /old-page/ are not the same match in Nginx. A redirect rule written for one won't necessarily catch requests to the other, which can leave some incoming links unredirected.

💡 Pro tip Always run nginx -t before every reload, even for a change that looks trivial. It takes a second and it's the difference between a clean reload and taking your entire site down over a missing semicolon.

Real-world examples

How the same two directives cover most situations you'll actually run into:

Single page moved
Path-to-path redirect
return 301
An old blog URL redirects to its new location inside a location block matching the exact old path.
Protocol upgrade
HTTP to HTTPS
Separate port 80 block
A dedicated server block listening on port 80 redirects every request to the same path on HTTPS.
Domain rebrand
Old domain to new domain
$request_uri preserved
Every path on the retired domain maps to the identical path on the new domain in one rule.
Canonical host
www to non-www
One server_name match
A server block matching the www subdomain redirects to the bare domain to avoid duplicate content.

return vs rewrite vs map compared

Nginx offers more than one way to redirect traffic. Here's how the main approaches differ.

Method Performance Flexibility Best for
return Fastest, no regex evaluated Fixed, one-to-one paths Single pages, domain moves, HTTPS enforcement
rewrite Slower, regex on every request Pattern-based, capture groups Bulk URL restructures with a shared pattern
map Fast lookup table Many-to-many, easy to scale Large lists of unrelated old-to-new URL pairs
error_page 404 Not a true redirect Same status code for all Not recommended for SEO-relevant redirects

Generate your Nginx redirect rule right now — free

The Rebrixe Nginx Redirect Generator builds a clean, ready-to-paste server or location block for the most common redirect scenarios — single page, domain move, www normalization, and HTTPS enforcement. No account needed, just fill in the URLs and copy the result.

Free Nginx Redirect Generator Enter your old and new URL, copy the config block.
Open Redirect Generator →

Frequently asked questions

return sends an immediate redirect response without evaluating the request further, making it faster and simpler for straightforward one-to-one redirects. rewrite evaluates the URL against a regular expression and can rewrite it internally or externally, which is more powerful but also more resource-intensive and easier to misconfigure.
Use a 301 (return 301) when the move is permanent, such as after a URL restructure or domain change, since it passes link equity to the new URL and tells search engines to update their index. Use a 302 (return 302) only for temporary moves, like a short maintenance redirect, since it does not transfer ranking signals the same way.
Run nginx -t after editing the config file to check for syntax errors, then reload Nginx and use a command like curl -I on the old URL to confirm it returns the correct status code and Location header pointing to the new URL.
A redirect loop usually happens when the destination URL matches the same location block that triggered the redirect, so the server keeps redirecting to itself. This is common with HTTPS redirects behind a proxy that doesn't correctly report the original protocol, or with rewrite rules that don't exclude the target path.
A full restart isn't necessary. Running sudo nginx -t to validate the config followed by sudo systemctl reload nginx (or nginx -s reload) applies the new redirect without dropping active connections.
Yes. A server block matching the old domain's server_name can use a single return 301 directive pointing to the new domain, with $request_uri appended so every path on the old domain maps to the same path on the new one.
Create a separate server block listening on port 80 for the domain, and inside it use a single return 301 https://$host$request_uri directive. Keeping HTTP and HTTPS in separate server blocks avoids conditional logic and is the configuration Nginx's own documentation recommends.

Generate your Nginx redirect rule in seconds

The Rebrixe Nginx Redirect Generator builds a clean, ready-to-paste config block for the most common redirect scenarios — no account needed, just a copy-ready server or location block.

Launch the Redirect Generator →
← Back to blogs