nginx url automatically adds slashes and 301 redirection

Guide: nginx url automatically adds slashes and 301 redirection, the URL points to a directory and does not contain a slash at the end, it will redirect to 301, add server_name or modify the access redirection.
 
nginx url automatically adds slashes and 301 redirection

The internal server uses nginx for website testing. Different domain names are distinguished by port numbers. For example, www uses the default port 80, and other domain names use 81, 82...

Sometimes when you tap the URL directly in the address bar, you will find that you jump to localhost.localdomain.

For example, there is an hx directory under port 858, so it can be accessed normally: http://192.168.1.158:858/hx/
But if there is one less /, such as: http://192.168.1.158:858/hx
will automatically jump Go to: http://localhost.localdomain:858/hx/

After analysis, it is the problem of nginx automatically adding slashes:
in some cases (for details, please refer to wiki.nginx.org), Nginx internal redirection rules will be activated.
For example, when the URL points to a directory and does not contain "/" at the end, Nginx will automatically do a 301 redirect internally. There are two situations:
 

1. server_name_in_redirect on (default), the URL redirection is: the first domain name in server_name + directory name + /;
2. server_name_in_redirect off, URL redirection is: the domain name in the original URL + directory name + /.
If server_name_in_redirect is on, then Nginx will use the first value of the server_name directive for redirects. If server_name_in_redirect is off, then nginx will use the requested Host header.

The original configuration, without adding server_name:
 

server {
    listen  858;
}

After modification:
 

server {
    listen  858;
    server_name 192.168.1.158;
}
或:
server {
    listen  858;
    server_name_in_redirect off;
}

This problem is solved. Visit http://192.168.1.158:858/hx to jump to http://192.168.1.158:858/hx/ normally.

Analysis:
The hostname of the server is localhost.localdomain. When server_name is not set, server_name becomes hostname.

The default is server_name_in_redirect on again, so when the original configuration accesses the hx directory, it will be redirected to localhost.localdomain/hx/.

The first modification method, adding server_name, then jump to server_name + directory name + /, yes.

The second modification of access, redirection is: URL of access + directory name + /, which is also correct.

Pan parsing configuration:
 

server{
    listen 80;
    server_name _;
}
 

If there is a phpcheck directory, and someone accidentally links a link like http://www.plchome.org/phpcheck, it will redirect to http://_/phpcheck/.
Therefore, in the case where server_name cannot be specified, server_name_in_redirect off should be added.
 

server{
    listen 80;
    server_name _;
    server_name_in_redirect off;
}
 

At this time, visit www.plchome.org/phpcheck, it will automatically and correctly jump to www.plchome.org/phpcheck/.

When upgrading the nginx version of a server at night, I see in changes:
 

Changes with nginx 0.8.48   03 Aug 2010

    *) Change: now the "server_name" directive default value is an empty 
       name "".
       Thanks to Gena Makhomed.

    *) Change: now the "server_name_in_redirect" directive default value is 
       "off".

Since nginx 0.8.48 server_name_in_redirect has been off by default and no longer needs to be specified.

  related articles
 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326561174&siteId=291194637