【问题标题】:Nginx rewrite part of URL with GeoIP variablesNginx 使用 GeoIP 变量重写部分 URL
【发布时间】:2019-02-05 08:30:21
【问题描述】:

我有一个这样的网址: https://example.com/home/anything/whatever

我需要重写此 URL 并将 /home/ 部分替换为来自 GeoIP 和 http-request-language 的变量: https://example.com/ch/de/anything/whatever

我在根目录上有一个位置匹配,它已经这样做了,这很有效:

location = / {
    rewrite ^ $location_uri$lang permanent;
}

现在我需要这个来处理上述情况,我使用了:

location ~ /home/(.*) {
    rewrite ^/home/(.*)$ /$location_uri$lang/$1/ permanent;
}

URL 的原始请求部分 ($1) 未添加到 URL 的末尾: https://example.com/ch/de//

如果我删除 2 个变量 $location_uri 和 $lang 并用修复文本 (/ch/de) 替换它们,它会起作用。

是否可以在重写中包含这些变量?

【问题讨论】:

    标签: variables nginx url-rewriting


    【解决方案1】:

    我设法通过 2 次重写来做到这一点:

    location ~ ^/home(.*)$ {
        rewrite ^/home(.*)$ /$location_uri$lang$request_uri permanent;
    }
    
    location ~ ^/ch/de/home(.*)$ {
        rewrite ^(.*)/home(.*)$ $1$2 permanent;
    }
    

    首先,我使用 GeoIP 变量重写 /home/,并添加包括 /home/ 在内的整个请求 URI。 然后我在目标 URI 上进行另一个位置匹配并删除 /home 部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      相关资源
      最近更新 更多