【发布时间】: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