【问题标题】:nginx location rule interferencenginx位置规则干扰
【发布时间】:2017-05-21 21:55:44
【问题描述】:

我有这个 Nginx 规则:

location /fr {
  rewrite ^/(fr)(?:/([^/]+))?$ /$2?lang=$1 last;
}

我有如下页面:

example.com/en/some-page

example.com/fr/some-page

example.com/fr

...

但我也有一个以fr开头的页面:

example.com/free-plan // doesn't work

example.com/fr/free-plan // doesn't work either

如何修改我的 Nginx 规则,使 /fr 规则不会干扰我的 free-plan 页面?

Ps:我使用 PHP 和规则来服务页面,没有扩展:

location / {
  try_files $uri $uri.html $uri/ @extensionless-php;
}

location @extensionless-php {
  rewrite ^(.*)$ $1.php last;
}

【问题讨论】:

    标签: linux nginx centos nginx-location


    【解决方案1】:

    我建议您将/fr 视为特殊情况并使用location /fr/ 以避免以fr 开头的页面产生副作用。

    例如:

    location = /fr {
        rewrite ^/(.*)$ /?lang=$1 last;
    }
    location /fr/ {
        ...
    }
    

    详情请见this document。

    【讨论】:

    • 嗨,Richard,这是问题的固定部分。但是,当转到example.com/fr 时,它不会将fr 作为lang 变量。但是像example.com/fr/some-page 这样的页面可以工作。
    • 抱歉,我正在捕获领先的/。您可能可以使用rewrite ^ /?lang=fr last; 硬连线参数。我不确定您是否需要一个笼统的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 2019-07-21
    • 2017-08-11
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多