【问题标题】:Nginx map module regex from include file not working来自包含文件的 Nginx 映射模块正则表达式不起作用
【发布时间】:2019-12-01 01:41:08
【问题描述】:

我正在尝试将一些 url 重定向到不同的文档路径。我的配置文件如下

###### /etc/nginx/conf.d/site.conf
    map_hash_max_size 2048;
    map_hash_bucket_size 128;
    map $uri $new {
            include list_4;
    }
    resolver  127.0.0.1;
    server {
            listen         81;
            server_name abcexample.com;
            access_log /var/log/nginx/abcexample-access.log main;
            error_log  /var/log/nginx/abcexample-error.log;
            location / {
                    if ($new) {
                      rewrite ^ $new redirect;
                    }
                  proxy_pass http://127.0.0.1:8000;
            }
     }
########### /etc/nginx/list_4
/abc/1.html /abc/hello;
/max/1.html /max/;
~^/xyz/(?<abc>.*)$ /xyz/123;
~^/kkkk/abcdef(?<abc>.*)$ /tttt/bbbbb/jjjj$abc;
~^/kaka/(?<abc>.*)$ /tata/$abc;

注意: 第 1,2 行和第 3 行重定向工作正常。 但是第 4 行和第 5 行不起作用。

root@Hell1:~# curl -I abcexample.com/kkkk/abcef111.html 
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Tue, 04 Apr 2017 07:08:47 GMT
Content-Type: text/html
Content-Length: 154
Location: http://abcdexample.com/tttt/bbbbb/jjjj$abc
Connection: keep-alive

我的问题是: 我必须在 list_4 文件中进行哪些更改才能获得如下结果

Location: http://abcdexample.com/news/bbbbb/jjjj111.html 

【问题讨论】:

  • 虽然map 能够使用命名捕获,但您不能使用它们来定义生成的常量值(这是一种耻辱)。详情请见this document
  • 在 nginx/1.11.13 上测试,结果为阳性。命名捕获按预期工作。
  • @Deadooshka:谢谢 :) 我在 CentOS 7.3 上使用 nginx-1.10.2。当然,我可以/不会在我的所有服务器上升级 nginx。所以我将继续编辑 nginx conf 文件。

标签: regex nginx url-rewriting url-mapping


【解决方案1】:

组合变量和文本仅在 1.11.0 版本后可用。

结果值可以包含文本、变量 (0.9.0) 及其组合 (1.11.0)。

http://nginx.org/en/docs/http/ngx_http_map_module.html#map

nginx 1.11.0 的变化 --- 2016 年 5 月 24 日

*) 特性:“map”指令支持多个变量的组合作为结果值。

http://nginx.org/en/CHANGES

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多