URL结尾不带斜杠NGINX自动301带上斜杠

1. 关于nginx自动跳转

站点目录结构:

根目录:/var/wwwroot/test/

URL结尾不带斜杠NGINX自动301带上斜杠_第1张图片

当访问地址为 http://www.test.com/abc 时,因为nginx未找到abc这个文件,而是找到了abc目录,自动在地址后面加上了斜杠,所以自动发生 301 跳转到 http://www.test.com/abc/

2. nginx自动跳转相关的3个配置

Nginx中存在一三个跳转的相关配置:

1. absolute_redirect

Syntax:	absolute_redirect on | off;
Default:	absolute_redirect on;
Context:	http, server, location
This directive appeared in version 1.11.8.

If disabled, redirects issued by nginx will be relative.

See also server_name_in_redirect and port_in_redirect directives.

默认为开启。

2. server_name_in_redirect

Syntax:	server_name_in_redirect on | off;
Default:	server_name_in_redirect off;
Context:	http, server, location
Enables or disables the use of the primary server name, specified by the server_name directive, in absolute redirects issued by nginx. When the use of the primary server name is disabled, the name from the “Host” request header field is used. If this field is not present, the IP address of the server is used.

The use of a port in redirects is controlled by the port_in_redirect directive.

默认关闭

3. port_in_redirect

Syntax:	port_in_redirect on | off;
Default:	port_in_redirect on;
Context:	http, server, location
Enables or disables specifying the port in absolute redirects issued by nginx.

The use of the primary server name in redirects is controlled by the server_name_in_redirect directive.

默认开启。

3. 举例

访问地址为:http://192.168.1.232/abc

Nginx情况一:

server {
    listen 80;
    server_name test.com;

    absolute_redirect off; // 关闭了它,不管 server_name_in_redirect, port_in_redirect 怎么设置都没有用
    server_name_in_redirect on;
    port_in_redirect on;
}

发生 301 跳转,跳转后的地址为:http://192.168.1.232/abc/

 官方说是使用 请求头中的 Host 字段,作为url跳转后host部分 

URL结尾不带斜杠NGINX自动301带上斜杠_第2张图片

Nginx情况二: 

server {
    listen 80;
    server_name test.com;

    absolute_redirect on; // 默认为on, Nginx配置中没有这个字段其实值是on, server_name_in_redirect, port_in_redirect 会根据配置起作用
    server_name_in_redirect on; // 发生跳转时使用 server 中配置的域名
    port_in_redirect on; // 发生跳转时使用 server 中配置的端口
}

发生 301 跳转,跳转后的地址为:http://test.com/abc/

点击查看更多文章

更新精彩文章请关注微信公众号:大胡几哥哥

你可能感兴趣的:(nginx,nginx,运维,服务器)