nginx - 将nginx主机和proxypass重写为squid

标签 nginx url-rewriting squid proxypass

我想实现以下目标:

请求主机:

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

应该重写为

http://example.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/

相关文章:

Nginx 阻止用户代理 "-"

.net - Azure Web 应用程序中的 SSL 握手重用

java - 为 tomcat 重写 URL

.htaccess - 多个域名到一个网站

asp.net - URL 重写和查询字符串参数

squid3 无法访问 google.com 或 bing.com

java - HttpClient 4.2.2 和带用户名/密码的代理

Nginx 规则添加 x-robots-tag header

nginx - 如何使用查询字符串生成带有子域的 URL 到新 URL?

git - 在 docker 容器中获取代理后面的 git