Nginx URL automatically add slash and 301 redirect Problem _nginx

Source: Internet
Author: User

Nginx URL automatically slashes the problem and 301 redirects, the URL points to a directory and in the end does not contain a slash, will 301 redirect jumps, add server_name or modify access redirection.

Nginx URL automatic slash problem and 301 redirect

The internal server uses Nginx, does the website test use. Different domain names use port number distinction, such as WWW with default 80 port, other domain name with 81,82 ...

Sometimes hit the URL directly in the address bar, you will find the jump to localhost.localdomain situation.

For example, 858 port under the HX directory, so normal access: http://192.168.1.158:858/hx/

But if a few dozen A/, such as: HTTP://192.168.1.158:858/HX

will automatically jump to: http://localhost.localdomain:858/hx/

After analysis is the problem of Nginx automatic slash:

In some cases (specific reference wiki.nginx.org), Nginx internal redirection rules are started.

For example, when a URL points to a directory and does not contain "/" at the end, the Nginx automatically makes a 301 redirect, and there are two scenarios:

1, Server_name_in_redirect on (default), URL Redirect to: server_name in the first domain name + directory name +/;

2, Server_name_in_redirect Off,url Redirect to: The original URL in the domain name + directory name +/.

If Server_name_in_redirect is on, then Nginx'll use the ' the ' of the ' the ' server_name directive for redirects. If Server_name_in_redirect is off, then Nginx would use the requested Host header.

Original configuration, no Add server_name:

server {
listen 858;
}

After modification:

server {
Listen 858;
server_name 192.168.1.158;
}

Or:

server {
Listen 858;
Server_name_in_redirect off;

This problem is resolved. Access Http://192.168.1.158:858/hx can jump to http://192.168.1.158:858/hx/normal.

Analysis:

The server's hostname is localhost.localdomain, and when server_name is not set, server_name becomes hostname.

The default is server_name_in_redirect on, so when the original configuration accesses the HX directory, it redirects to localhost.localdomain/hx/.

The first way to modify, add server_name, then jump to the server_name + directory name +/, right.

The second modified access, redirected to: Access to the url+ directory name +/, also right.

Pan-resolution configuration:

server{
Listen;
server_name _;
}

If there is a phpcheck directory, someone accidentally linked http://www.plchome.org/phpcheck such a link, will be redirected to http://_/phpcheck/.

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

server{
Listen;
server_name _;
Server_name_in_redirect off;

At this point, access to Www.plchome.org/phpcheck, will automatically and correctly jump to the www.plchome.org/phpcheck/.

When you upgrade the Nginx version of a server at night, see in changes:

Changes with Nginx 0.8.48 Aug
*) change:now the ' server_name ' directive default value is a empty 
name "".
To Gena makhomed.
*) Change:now the ' server_name_in_redirect ' directive default value is 
' off '.

From Nginx 0.8.48 Server_name_in_redirect has been defaulted to off and no longer needs to be specified.

The above is a small set up to introduce the Nginx URL to automatically add slashes and 301 redirect problem, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.