nginx url auto slash and 301 redirect issues

  • 2020-05-12 06:51:11
  • OfStack

nginx url auto slash issues and 301 redirects, URL points to a directory and does not include slashes at the end, will redirect to 301, add server_name or modify access redirects.

nginx url auto slash problem and 301 redirection

Internal servers use nginx for site testing. Different domain names are distinguished by port Numbers, such as www with the default port of 80, other domain names with 81,82...

Sometimes you'll find yourself jumping to localhost.localdomain by tapping the url directly into the address bar.

Such a hx directory under the port 858, normal access: http: / / 192.168.1.158:858 / hx /

But if less hit 1 /, such as: http: / / 192.168.1.158:858 / hx

Will automatically jump to: http: / / localhost localdomain: 858 / hx /

The analysis is that nginx automatically adds the slash problem:

In some cases (see wiki.nginx.org for details), the Nginx internal redirection rule is started.

For example, when URL points to a directory and does not include "/" at the end, Nginx will automatically do a 301 redirection inside. There are two situations:

1, server_name_in_redirect on (default), URL redirection is: server_name the first domain + directory name + /;

2. server_name_in_redirect off, URL redirection: domain name + directory name + / in the original URL.

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.

Original configuration, no server_name added:


server {
listen 858;
}

Revised:


server {
listen 858;
server_name 192.168.1.158;
}

Or:


server {
listen 858;
server_name_in_redirect off;
}

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

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 hx directory is accessed in the original configuration, it will be redirected to localhost.localdomain /hx/.

The first modification, server_name, jumps to server_name + directory name + /, that's right.

The second modification access is redirected to: access URL+ directory name +/, also correct.

Pan-parse configuration:


server{
listen 80;
server_name _;
}

If a phpcheck directory, someone accidentally chain http: / / www plchome. org/phpcheck such a link, it will be redirected to the http: / / _ / phpcheck /.

So in this case where you can't specify server_name, you add server_name_in_redirect off.


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

At this time, the access www. plchome. org/phpcheck, automatically and jump right to www. plchome. org phpcheck /.

When upgrading the nginx version of one server at night, I saw 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".

From nginx 0.8.48, server_name_in_redirect is already off by default and no longer needs to be specified.


Related articles: