我've got a quite interesting behavior. I want to avoid trailing slashes in URL'在我的网站上 . 我把 rewrite ^/(.*)/$ /$1 permanent;
规则放到我的服务器块中,所以https://example.com/something/
,https://example.com/something////
重定向到https://example.com/something
;
和https://example.com/
重定向到https://example.com
但是 https://example.com////
被重定向到... https://enjoygifts.ru////
(实际上是_009136_ s 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
...
}
1 回答
https://example.com
并不存在,根URI是/
- 它如何's displayed in the browser'的地址栏依赖于浏览器 - 有些会自动显示单独的/
而其他人将删除单独的/
.因此,您无法从
https://example.com/
重定向到https://example.com
- 它将被静默解释为从https://example.com/
重定向到https://example.com/
.在评估
location
和rewrite
语句并生成$uri
变量时,Nginx使用normalized URI . 多次连续出现的/
被折叠成一个/
.虽然正则表达式
^/(.*)/$
与URI//
匹配,但语句永远不会看到它 . 因为Nginx已经将该URI规范化为/
,这与正则表达式不匹配 .如果具有多个
/
的根URI存在问题,请将正则表达式应用于$request_uri
变量,该变量在规范化之前包含原始URI,并且还包括查询字符串(如果有) .例如:
这可以放在
location / {...}
块内 . 有关if
的使用,请参阅this caution .