Nginx 301 vs 302 Redirects Explained

Understand the critical differences between 301 permanent and 302 temporary redirects in Nginx. Learn when to use each redirect type and SEO impact.

Routing

Detailed Explanation

Choosing between a 301 and 302 redirect in Nginx has significant implications for SEO rankings, browser caching behavior, and overall user experience. Understanding the differences ensures you select the right redirect type for each situation.

301 Permanent Redirect

A 301 tells browsers and search engines that the resource has permanently moved to a new URL. Search engines transfer the accumulated SEO value (also called link equity or page rank) from the old URL to the new destination.

server {
    listen 80;
    server_name old-domain.com;
    return 301 https://new-domain.com$request_uri;
}

location /old-page {
    return 301 /new-page;
}

302 Temporary Redirect

A 302 indicates the resource has temporarily moved to a different location. Search engines retain the old URL in their index and do not transfer link equity to the new URL. Browsers also avoid caching 302 redirects aggressively.

location /feature {
    return 302 /feature-beta;
}

When to Use 301

Use permanent redirects for these scenarios: domain migrations from one domain to another, URL structure changes during site redesigns, HTTP to HTTPS migration, removing trailing slashes or file extensions from URLs, and consolidating duplicate content such as www versus non-www variants.

When to Use 302

Use temporary redirects when: running A/B tests between different page versions, displaying temporary maintenance or under-construction pages, implementing geolocation-based redirects where users may access from different locations, promoting seasonal campaigns, or any situation where you plan to revert the redirect in the future.

Other Redirect Status Codes

Beyond 301 and 302, HTTP defines additional redirect codes for specific use cases:

  • 303 See Other: Used after a POST request to redirect to a GET page, preventing accidental form resubmission.
  • 307 Temporary Redirect: Identical to 302 but strictly guarantees the HTTP method is preserved during the redirect.
  • 308 Permanent Redirect: Identical to 301 but guarantees the HTTP method is preserved, which is important for API endpoints.
location /api/old-endpoint {
    return 308 /api/new-endpoint;
}

SEO Impact

Using a 302 when you intend a permanent move can dilute your search rankings because search engines may continue indexing the old URL instead of transferring authority to the new one. Conversely, using a 301 for a genuinely temporary change means search engines will drop the old URL from their index entirely, making the redirect very difficult to reverse later.

Browser Caching Warning

Browsers aggressively cache 301 redirects in their local history. If you deploy an incorrect 301 redirect, affected users may continue experiencing the incorrect redirect even after you fix the server configuration, until they clear their browser cache manually.

Use Case

You are restructuring your website URLs and need to choose the correct redirect type to preserve search engine rankings while properly guiding users to the new pages.

Try It — Nginx Config Generator

Open full tool