Rewrite or return for simple redirection

I am trying to understand which option would result in more efficient
HTTP
redirection. I am trying to redirect ^/address page to ^/contact page.

Option 1:
rewrite ^/address /contact permanent;

Option 2:
location ~ ^/address {
return 301 $scheme://$host/contact
}

Which option should be preferred? Both options involve regex and I am
not
sure which one will be faster. Are there any other better options?

-N

Hello!

On Fri, Nov 21, 2014 at 11:21:40AM -0800, neubyr wrote:

Which option should be preferred? Both options involve regex and I am not
sure which one will be faster. Are there any other better options?

location = /address {
    return 301 /contact;
}


Maxim D.
http://nginx.org/

On Fri, Nov 21, 2014 at 11:33 AM, Maxim D. [email protected]
wrote:

}

Thank you Maxim!! That’s helpful. I should have used precise location
match
and relative redirect path.

Also, can something with following rewrite be converted to ‘location and
return’ combination?

rewrite ^/members/(.*) /users/$1

Or am I better with rewrite directive in this case? Appreciate any help.

-N

Hello!

On Fri, Nov 21, 2014 at 12:06:54PM -0800, neubyr wrote:

Option 1:
location = /address {

rewrite ^/members/(.*) /users/$1

Or am I better with rewrite directive in this case? Appreciate any help.

For such cases rewrite is better, IMHO. In some cases it may be a
good idea to additionally isolate it with prefix location, like
this:

location /members/ {
    rewrite ^/members/(.*) /users/$1 redirect;
}


Maxim D.
http://nginx.org/

On Fri, Nov 21, 2014 at 12:11 PM, Maxim D. [email protected]
wrote:

return 301 $scheme://$host/contact

Or am I better with rewrite directive in this case? Appreciate any help.

For such cases rewrite is better, IMHO. In some cases it may be a
good idea to additionally isolate it with prefix location, like
this:

location /members/ {
    rewrite ^/members/(.*) /users/$1 redirect;
}

Thank you Maxim. In what cases prefix might be a good idea??

  • N

Thank you Maxim! That’s helpful explanation.

-N

Hello!

On Fri, Nov 21, 2014 at 02:53:50PM -0800, neubyr wrote:

On Fri, Nov 21, 2014 at 12:11 PM, Maxim D. [email protected] wrote:

[…]

For such cases rewrite is better, IMHO. In some cases it may be a
good idea to additionally isolate it with prefix location, like
this:

location /members/ {
    rewrite ^/members/(.*) /users/$1 redirect;
}

Thank you Maxim. In what cases prefix might be a good idea??

This mostly depends on other configuration in the server{} and
expected workload. That is, it doesn’t make much sense to isolate
rewrite if you just have a bunch of rewrites already isolated from
other requests. E.g., consider the following configuration:

location / {
    rewrite ^/members/(.*) /users/$1  redirect;
    rewrite ^/images/(.*)  /static/$1 redirect;
    return 404;
}

location /users/ {
    ...
}

location /static/ {
    ...
}

This configuration assumes that all the traffic in “location /”
will be redirected, and most normal requests should be handled in
other locations (“/users/”, “/static/”). It doesn’t make much
sense to bother doing additional isolation with prefix locations
as it’s more or less already here.

On the other hand, consider the following configuration:

location / {
    rewrite ^/members/(.*) /users/$1  redirect;
    rewrite ^/images/(.*)  /static/$1 redirect;

    proxy_pass http://backend;
}

In this configuration all requests are handled in “location /”.
And for all normal requests to “/users/…” nginx will have to
check all the rewrites. This is just a waste of resources.
Moreover, such a configuration may (and likely will) become hard
to support eventually, and this is probably even more important
thing to consider. So it’s usually good idea to use something
like this instead:

location / {
    proxy_pass http://backend;
}

location /members/ {
    rewrite ^/members/(.*) /users/$1  redirect;
}

location /images/ {
    rewrite ^/images/(.*)  /static/$1 redirect;
}

See also this Igor’s talk for additional hints on how to write
scalable nginx configurations:


Maxim D.
http://nginx.org/