重写nginx host和proxpass到squid



我想实现以下目标:

请求主持人:

http://example.com.proxy.myserver.com

应该重写为

http://example.com

并通过nginx proxpass传递到squid服务器。

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://$ subb放在替换部分,nginx将执行301,忽略"break"标志。

如果替换字符串以http://开头,则客户端将被重定向,并且任何进一步的重写指令都将终止。

您要"重写"还是"重定向"?如果只是为了重写,你可以删除重写指令:

    rewrite ^ $scheme://$subdub break;

,它将工作,因为你的上游服务器可以依靠主机头来确定流量目标(虚拟主机)。

你的主机头发送到上游服务器是错误的。应该是

    proxy_set_header  Host $subdub;

$scheme不应该放在Host头中

最新更新