加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

带有正则表达式的nginx映射吃了我的URI

发布时间:2020-12-14 06:27:30 所属栏目:百科 来源:网络整理
导读:一些地图杀了我的URI,我不明白为什么: map $http_cookie $redir_scheme { default http; ~some=value https; # here is the SSL cookie}server { listen 8888; server_name redir.*; expires -1; add_header Last-Modified ""; location / { rewrite ^/(.*)
一些地图杀了我的URI,我不明白为什么:
map $http_cookie $redir_scheme {
    default http;
    ~some=value https; # here is the SSL cookie
}
server {
    listen       8888;
    server_name  redir.*;

    expires -1;
    add_header Last-Modified "";

    location / {
        rewrite ^/(.*)$$redir_scheme://example.com/$1 redirect;
    }
}

Curl提供没有URI的重定向:

$curl -giH 'Host: redir.somedomain.com' 'localhost:8888/some/path/with/meaningful/data' -H 'Cookie: some=value'
(...)
Location: https://example.com/
(...)

但是当我将配置更改为:

map $http_cookie $redir_scheme {
    default http;
    some=value https; # here is the SSL cookie
}
server {
    listen       8888;
    server_name  redir.*;

    expires -1;
    add_header Last-Modified "";

    location / {
        rewrite ^/(.*)$$redir_scheme://example.com/$1 redirect;
    }
}

Curl提供了一个带有URI的重定向:

$curl -giH 'Host: redir.somedomain.com' 'localhost:8888/some/path/with/meaningful/data' -H 'Cookie: some=value'
(...)
Location: https://example.com/some/path/with/meaningful/data
(...)

我想第一个解决方案真的很蠢,但我不明白为什么.
你有光吗?

这是因为$1来自最后执行的正则表达式.由于map {}的检查晚于重写中的正则表达式,因此$1来自地图中指定的正则表达式(并且它是空的). nginx trac中有一个关于此的 ticket 564 – 虽然行为是正式的,但它显然是反直觉的,需要改变.

作为一种解决方法,您可以使用命名捕获:

rewrite ^/(?<rest>.*)$$redir_scheme://example.com/$rest redirect;

或者,更好的是,只需使用$request_uri返回:

return 302 $redir_scheme://example.com$request_uri;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读