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

1. 关于nginx自动跳转

站点目录结构:

根目录:/var/wwwroot/test/

当访问地址为 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部分 

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/

点击查看更多文章

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用nginx伪静态隐藏.php后缀并在URL后补斜杠的方法是通过在nginx的配置文件中添相应的规则来实现。 首先,我们需要打开nginx的配置文件,一般位于/etc/nginx/nginx.conf。在该文件中,我们可以找到一个名为`server`的块,这是我们需要进行修改的地方。 首先,我们需要在`server`块中添一个`location`块,用于匹配URL中以.php结尾的请求。可以使用正则表达式进行匹配,如以下所示: ``` location ~ \.php$ { try_files $uri/ =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/run/php/php7.4-fpm.sock; #指定php-fpm的sock文件路径 fastcgi_index index.php; include fastcgi_params; } ``` 上面的配置中,通过`location`和正则表达式`~ \.php$`来匹配URL以.php结尾的请求。然后,使用`try_files`指令来尝试去寻找以URL后补斜杠的方式来访问文件,若找不到,则返回404错误。 接下来,我们使用`fastcgi_split_path_info`指令将请求分离为两部分,分别为PHP脚本文件路径和路径信息。然后,使用`fastcgi_pass`指令指定php-fpm的sock文件路径,以及`fastcgi_index`指令指定默认的PHP脚本文件名为index.php。 最后,我们将`fastcgi_params`包含进来,以确保正确地传递PHP脚本执行所需的参数。 保存配置文件后,重新nginx配置,可以使用`sudo nginx -s reload`命令。 这样,当访问URL时,nginx自动隐藏.php后缀,并在URL后面自动补上斜杠。例如,如果原始URL为`http://example.com/index.php`,则通过上述配置后,可以直接访问`http://example.com/index/`来访问相应的页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值