带有尾部斜杠的奇怪 Nginx 行为 [英] Strange Nginx behavior with trailing slashes

查看:109
本文介绍了带有尾部斜杠的奇怪 Nginx 行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常有趣的行为.我想避免在我网站上的 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////(实际上不t 重定向,它是 200 代码).为什么?

这是我的服务器块:

<前>服务器 {听 443 ssl;...... ssl 指令...根/var/www/mysite.com;索引 index.php;server_name mysite.com;重写 ^/(.*)/$/$1 永久;地点/{最后重写^/.*$/index.php;}位置 ~ ^/index.php {try_files $uri =404;包括/etc/nginx/fastcgi.conf;fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;}位置 ~ ^/storage/app/uploads/public { try_files $uri 404;}......许多类似的位置块...}

解决方案

https://example.com 并不存在,根 URI 是 / -它在浏览器地址栏中的显示方式取决于浏览器 - 有些会自动显示单独的 / 而另一些会删除单独的 /.

因此您不能从 https://example.com/ 重定向到 https://example.com - 它将被默默地解释为来自 https://example.com/https://example.com/.

Nginx 在评估 location 时使用 规范化 URIrewrite 语句,并生成 $uri 变量.多个连续出现的 / 被折叠成一个 /.

尽管正则表达式 ^/(.*)/$ 与 URI // 匹配,但语句永远不会看到它.因为 Nginx 已经将该 URI 规范化为 /,这与正则表达式不匹配.

<小时>

如果具有多个 / 的根 URI 有问题,请对 $request_uri 变量应用正则表达式,该变量包含规范化前的原始 URI,还包括查询字符串(如果有).

例如:

if ($request_uri ~ "^/{2,}(\?|$)") {返回 301/$is_args$args;}

这可以放在您的 location/{...} 块中.请参阅此注意事项关于的使用,如果.

I've got a quite interesting behavior. I want to avoid trailing slashes in URL's on my site. I've put rewrite ^/(.*)/$ /$1 permanent; rule into my server block, so
https://example.com/something/,
https://example.com/something////
redirect to
https://example.com/something;
and
https://example.com/
redirects to
https://example.com

But https://example.com//// is redirected to ... https://enjoygifts.ru//// (actually don't redirected, it's 200 code). Why?

Here is my server block:


    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 doesn't really exist, the root URI is / - how it's displayed in the browser's address bar is browser dependent - some will automatically show the solitary / whereas others will remove a solitary /.

So you cannot redirect from https://example.com/ to https://example.com - it will be silently interpreted as a redirect from https://example.com/ to https://example.com/.

Nginx uses a normalized URI when evaluating location and rewrite statements, and generating the $uri variable. Multiple consecutive occurrences of / are folded into a single /.

Although the regular expression ^/(.*)/$ matches the URI //, the statement will never see it. Because Nginx has already normalised that URI to /, which does not match the regular expression.


If a root URI with multiple /s is a problem, apply a regular expression to the $request_uri variable, which contains the original URI before normalization and also includes the query string (if any).

For example:

if ($request_uri ~ "^/{2,}(\?|$)") { 
    return 301 /$is_args$args; 
}

This can be placed inside your location / {...} block. See this caution on the use of if.

这篇关于带有尾部斜杠的奇怪 Nginx 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆