V
V
Vlad Sklyar2018-08-24 14:08:53
Nginx
Vlad Sklyar, 2018-08-24 14:08:53

NginX: rewrite or return?

When setting up the NginX server, we all add rules for the URL output we need: output only HTTPS and no WWW, for example.
This is done in two ways:
1) Return:

return 301 https://www.domain.com$request_uri;
return 301 $scheme://domain.com$request_uri;

2) Rewrite:
rewrite ^(.*) https://$host$1 permanent;
rewrite ^ https://$host$request_uri? permanent;

According to the Official NginX Documentation , Return is preferable as it is less expensive, doesn't require regular expression parsing, and doesn't require custom variables to be created. At the same time, there is no difference between the methods either in terms of interpretation by the browser or in terms of the same SEO. However, with return it is possible to catch a CRLF injection if the configuration contains a construction of the form:
return 302 https://$host$uri;
So what is the correct and safer use in the end?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question