Correct proxy path in nginx.conf

前端 未结 3 1077
自闭症患者
自闭症患者 2020-12-05 14:10

we have two servers, A and B. Server A is accessed worldwide. He has nginx installed. That\'s what I have in conf:

location /test {
  proxy_pass http://local         


        
相关标签:
3条回答
  • 2020-12-05 14:28

    That should work. Nginx should strip the '/test' path on the upstream local server. So what I can say is that is not the cause. To make it a bit better, try this:

    location /test/ {
      proxy_pass http://localserver.com/;
    }
    

    The 2 slashes I added at the first 2 lines will avoid mistakenly match '/testABC' and send the wrong request to the upstream local server, for example.

    Do you have a

    proxy_redirect
    

    line in the same location block? If your upstream local server has redirects, then a mistake on that line will cause an issue like you described.

    [UPDATE] Found the root cause why the original config didn't work and mine works: nginx does NOT replace URI path part if the proxy_pass directive does not have a URI path itself. So my fix of adding a slash (slash is treated as a URI path) at the end triggers the URI path replacement.

    Reference: http://wiki.nginx.org/HttpProxyModule#proxy_pass

    If it is necessary to transmit URI in the unprocessed form then directive proxy_pass should be used without URI part

    location  /some/path/ {
      proxy_pass   http://127.0.0.1;
    }
    
    0 讨论(0)
  • 2020-12-05 14:29

    try rewrite

    location /test {
        rewrite ^ $scheme://$host/;
        proxy_pass http://localserver.com;
    }
    

    some helpful links...

    http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

    http://wiki.nginx.org/Pitfalls

    0 讨论(0)
  • 2020-12-05 14:33

    try to add as specified here http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass:

          proxy_pass http://localserver.com/;
    
    0 讨论(0)
提交回复
热议问题