![Nginx proxy_pass 重写 URL 和 Headers](https://linux22.com/image/761388/Nginx%20proxy_pass%20%E9%87%8D%E5%86%99%20URL%20%E5%92%8C%20Headers.png)
我想要请求
http://localhost/app/httpbin.org/anything?test=data@cookieValue
转发至
Get https://httpbin.org/anything?test=data
Cookie: myCookies=cookieValue
我尝试了以下方法,但它返回 404
location ~* ^/app/(.*?)@(.*){
resolver 8.8.8.8;
proxy_pass https://$1$is_args$args;
proxy_set_header Cookie myCookies=$2;
}
答案1
解决了,问题是 nginx 正则表达式仅匹配path
或
/httpbin.org/anything
不是参数或查询字符串
?test=data@cookieValue
为了解决这个问题,我将 URL 更改为
http://localhost/app/cookieValue/httpbin.org/anything?test=data
这是配置
location ~ ^/app/([^\/]+)/(.*){
resolver 8.8.8.8;
proxy_pass https://$2$is_args$args;
proxy_set_header Cookie myCookie=$1;
}