我想实现以下目标:
请求主机:
http://example.com.proxy.myserver.com
应该重写为
并通过 nginx proxypass 传递到鱿鱼服务器。
server {
listen 80;
server_name ~^(?<subdub>.*)\.proxy\.myserver\.com$;
location / {
rewrite ^ $scheme://$subdub break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $scheme://$subdub;
proxy_set_header Request-URI $scheme://$subdub;
proxy_pass http://localhost:3128;
proxy_redirect off;
}
}
问题是,nginx 立即将此请求重定向到 http://example.com
有什么想法可以让它发挥作用吗?
最佳答案
301 重定向正是 nginx 对该重写规则所做的事情:因为您将 $scheme://$subdub 放在替换部分,nginx 将执行 301,忽略该“break”标志。
If the replacement string begins with http:// then the client will be redirected, and any further rewrite directives are terminated.
您是否正在尝试“重写”或“重定向”?如果只是为了重写,您可以删除该重写指令:
rewrite ^ $scheme://$subdub break;
它会起作用,因为您的上游服务器可以依赖 HOST header 来确定流量目标(虚拟主机)。
此外您发送到上游服务器的主机 header 是错误的。应该是
proxy_set_header Host $subdub;
$scheme 不应放在 Host header 中。
关于nginx - 将nginx主机和proxypass重写为squid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696908/