【问题标题】:Nginx does redirect, not proxyNginx 确实重定向,而不是代理
【发布时间】:2014-08-22 14:52:42
【问题描述】:

我想将 Nginx 设置为 https 服务的反向代理,因为我们有一个特殊的用例,我们需要“取消 https”连接:

http://nginx_server:8080/myserver ==> https://mysecureservice

但是实际的 https 服务没有被代理。 Nginx 确实将我重定向到实际服务,因此浏览器中的 URL 会更改。我想与 Nginx 交互,因为它是实际的服务,只是没有 https。

这就是我所拥有的:

server {
    listen 0.0.0.0:8080 default_server;
    location /myserver {
        proxy_pass https://myserver/;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
    }
}

【问题讨论】:

    标签: ssl nginx https proxy reverse-proxy


    【解决方案1】:

    我遇到了类似的问题。就我而言,我可以通过在proxy_pass URL 中添加斜杠来解决问题:

    之前

    server {
      location / {
        proxy_pass http://example.com/path/to/some/folder;
      }
    }
    

    之后

    server {
      location / {
        # added trailing slash
        proxy_pass http://example.com/path/to/some/folder/;
      }
    }
    

    【讨论】:

      【解决方案2】:

      对我来说,这个配置就足够了:

      events {
      }
      
      http {
          server {
              location / {
                  resolver 8.8.8.8;
                  proxy_pass https://www.example.com$request_uri;
              }
          }
      }
      

      (注意resolver指令与OP中的问题无关,我只需要它能够代理外部域,例如example.com

      对我来说,问题只是我在www.example.com 中缺少www.。在 Firefox 开发人员的控制台中,我可以看到对 localhost 的 GET 请求返回了 301,因此我认为 NGINX 发出的是 301,而不仅仅是镜像 example.com。不是这样:事实上,问题在于example.com 正在返回 301 以重定向到 www.example.com,NGINX 尽职尽责地镜像那些 301,然后 Firefox 直接从 localhost“更改 URL”(跟随重定向)到 @987654331 @。

      【讨论】:

      • 这里也忘记了www,真的很容易忘记。
      • 谢谢!这也解决了我的问题。
      【解决方案3】:

      如果你不希望服务器做重定向,你可以这样设置 nginx:

      server
      {
          listen 80;
          server_name YOUR.OWN.DOMAIN.URL;
          location / {
              proxy_pass http://THE.SITE.URL.YOU.WANT.TO.DELEGAGE/;
              proxy_redirect off;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          }
      }
      

      【讨论】:

        【解决方案4】:

        您必须使用proxy_redirect 来处理重定向。

         Sets the text that should be changed in the “Location” and “Refresh” header fields of a 
         proxied server response. Suppose a proxied server returned the header field 
         “Location:https://myserver/uri/”. The directive
         will rewrite this string to “Location: http://nginx_server:8080/uri/”. 
        

        例子:

         proxy_redirect https://myserver/ http://nginx_server:8080/;
        

        来源:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

        【讨论】:

        • 您好,感谢您的回复。即使对于非重定向 URL,它也不起作用。我总是被重定向,不管实际的 https 服务器是否发送 Location 标头:proxy_redirect https://mysecureservice/ http://$host:$server_port/myserver/;
        • 重定向从何而来?如果它来自 https 服务器 html,你必须 subs_filter 它。如果有 cookie,您可能需要重写它。
        • 重定向肯定来自Nginx。我没有使用 HTML,我使用的是 JSON。如果我这样做curl https://mysecureserver/api/v3/user -I(直接调用实际服务器),我会得到 200 OK。如果我对 Nginx 执行相同操作:curl http://nginx_server:8080/myserver/api/v3/user -I,我会得到 301 Moved Permanently
        • proxy_set_header Host $host; 中的 HTTP 主机是否正确?它必须是 mysecureserver。
        • 有了这么多的赞成票,我肯定不明白我们想要什么。我知道 mitchkman 想要代理 NOT 重定向,因为重定向是已经发生的事情。这个答案说明了如何重定向,这与所要求的相反。
        猜你喜欢
        • 2019-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-12
        • 1970-01-01
        • 2020-05-31
        • 2020-11-20
        相关资源
        最近更新 更多