【问题标题】:Rewrite nginx host and proxypass to squid将nginx主机和proxypass重写为squid
【发布时间】:2013-03-19 18:43:59
【问题描述】:

我想实现以下目标:

请求主机:

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

应该改写成

http://example.com

并通过 nginx proxypass 传递给 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

任何想法如何让它工作?

【问题讨论】:

    标签: nginx rewrite squid proxypass


    【解决方案1】:

    301 重定向正是 nginx 对该重写规则所做的:因为您将 $scheme://$subdub 放在替换部分,所以 nginx 将执行 301,忽略该“中断”标志。

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

    您是在尝试“重写”还是“重定向”?如果只是为了重写,您可以删除该重写指令:

        rewrite ^ $scheme://$subdub break;
    

    它会起作用,因为您的上游服务器可以依赖 HOST 标头来确定流量目标(虚拟主机)。

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

        proxy_set_header  Host $subdub;
    

    $scheme 不应放在 Host 标头中。

    【讨论】:

      猜你喜欢
      • 2015-10-08
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 2022-11-03
      相关资源
      最近更新 更多