0
0
NginxComparisonBeginner · 4 min read

Rewrite vs Return in Nginx: Key Differences and Usage

In Nginx, rewrite changes the URL and can continue processing other rules or locations, while return immediately sends a response or redirect to the client, stopping further processing. Use rewrite for internal URL changes and return for quick redirects or responses.
⚖️

Quick Comparison

This table summarizes the main differences between rewrite and return directives in Nginx.

Factorrewritereturn
PurposeModify URL and optionally redirectSend immediate response or redirect
ProcessingContinues processing after rewriteStops processing immediately
Use CaseInternal URL changes, complex rewritesSimple redirects or status responses
Syntax ComplexitySupports regex and variablesSimple status code and URL
PerformanceSlightly slower due to processingFaster, direct response
Effect on ClientMay or may not redirect clientAlways sends response to client
⚖️

Key Differences

The rewrite directive in Nginx is used to change the requested URL based on patterns. It supports regular expressions and variables, allowing complex URL transformations. After rewriting, Nginx can continue processing other directives or locations, which means the client might not see a redirect if last or break flags are not used.

On the other hand, return immediately stops processing and sends a response to the client. This response can be a status code like 301 or 404, optionally with a URL for redirection. Because it ends processing, it is simpler and faster for straightforward redirects or error responses.

In summary, use rewrite when you need to internally change URLs or apply complex rules, and use return when you want to quickly send a redirect or status code without further processing.

💻

Rewrite Example

nginx
server {
    listen 80;
    server_name example.com;

    location /oldpath/ {
        rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
    }
}
Output
Client receives a 301 redirect from /oldpath/something to /newpath/something
↔️

Return Equivalent

nginx
server {
    listen 80;
    server_name example.com;

    location /oldpath/ {
        return 301 /newpath/$1;
    }
}
Output
Client receives a 301 redirect to /newpath/<original request URI>
🎯

When to Use Which

Choose rewrite when you need to change URLs internally or apply complex pattern matching before deciding the final URL. It is useful for legacy URL support or URL restructuring without immediate client redirection.

Choose return when you want to quickly send a redirect or status code to the client, such as permanent redirects (301), temporary redirects (302), or error responses. It is simpler, faster, and clearer for straightforward responses.

Key Takeaways

rewrite modifies URLs and can continue processing other rules.
return immediately sends a response or redirect and stops processing.
Use rewrite for complex URL changes and internal routing.
Use return for simple, fast redirects or status responses.
return is generally faster and clearer for client redirects.