A
A
Alexander Vladimirovich2014-11-20 14:08:37
Nginx
Alexander Vladimirovich, 2014-11-20 14:08:37

What is the difference between rewrite ... permanent and return 301 ... ?

just started migrating from apache to nginx

if ($request_uri ~ "\.") {
    rewrite ^/(.*)/$ /$1 permanent;
}

if ($request_uri ~ "\.") {
    rewrite ^/(.*)/$ /$1;
    return 301;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Boris Benkovsky, 2014-11-20
@benbor

nginx.org/ru/docs/http/ngx_http_rewrite_module.html

permanent
returns a permanent 301 redirect.

Answer, nothing

L
Lynn "Coffee Man", 2014-11-21
@Lynn

In the first case, you will only have a redirect if the URI ends with a slash.
In the second, always and on an empty Location which the browser will interpret as the current one and you will get an endless cycle of redirects.
The return 301second parameter must be the address. In this case, one could write:

if ($request_uri ~ "\.") {
    rewrite ^/(.*)/$ /$1;
    return 301 $uri;
}

K
KPEBETKA, 2014-12-03
@KPEBETKA

Better use rewrite, because you can catch CRLF with return
If there is a construction like
location / {
return 302 https://$host$uri;
}
Then when following a link like https://example.com/CRLF%0d%0aSet-Cookie:%20BUG%3dHi, the attacker will inject an arbitrary header into the user.
This is a fairly common bug

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question