我有一个非常有趣的行为。我想避免在我的网站上的 URL 中出现尾随斜杠。我已经把rewrite ^/(.*)/$ /$1 permanent;
规则进入我的服务器块,所以
https://example.com/something/
,
https://example.com/something////
重定向到
https://example.com/something
;
and
https://example.com/
重定向到
https://example.com
But https://example.com////
被重定向到...https://enjoygifts.ru////
(实际上不重定向,是200代码)。为什么?
这是我的服务器块:
server {
listen 443 ssl;
...
... ssl directives
...
root /var/www/mysite.com;
index index.php;
server_name mysite.com;
rewrite ^/(.*)/$ /$1 permanent;
location / {
rewrite ^/.*$ /index.php last;
}
location ~ ^/index.php {
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ ^/storage/app/uploads/public { try_files $uri 404; }
...
... lot of similar location blocks
...
}
https://example.com
并不真正存在,根 URI 是/
- 它在浏览器地址栏中的显示方式取决于浏览器 - 有些会自动显示单独的/
而其他人则会移除一个单独的/
.
所以你不能重定向自https://example.com/
to https://example.com
- 它将被默默地解释为来自的重定向https://example.com/
to https://example.com/
.
Nginx 使用一个规范化的URI http://nginx.org/en/docs/http/ngx_http_core_module.html#location评估时location
and rewrite
语句,并生成$uri
多变的。连续多次出现/
被折叠成一个/
.
虽然正则表达式^/(.*)/$
匹配 URI//
,该语句永远不会看到它。因为 Nginx 已经将该 URI 标准化为/
,与正则表达式不匹配。
如果根 URI 具有多个/
s 是一个问题,应用正则表达式$request_uri
变量,其中包含规范化之前的原始 URI,还包括查询字符串(如果有)。
例如:
if ($request_uri ~ "^/{2,}(\?|$)") {
return 301 /$is_args$args;
}
这可以放置在您的location / {...}
堵塞。看这个警告 https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/关于使用if
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)