Answer the question
In order to leave comments, you need to log in
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
nginx.org/ru/docs/http/ngx_http_rewrite_module.html
permanent
returns a permanent 301 redirect.
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 301
second parameter must be the address. In this case, one could write:
if ($request_uri ~ "\.") {
rewrite ^/(.*)/$ /$1;
return 301 $uri;
}
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 questionAsk a Question
731 491 924 answers to any question