【问题标题】:NGINX modify the request uriNGINX 修改请求 uri
【发布时间】:2017-08-22 12:57:55
【问题描述】:

这是我第一次在这里发帖,希望你能帮我解决我的问题。

场景:我想通过使用 location 指令和 proxy_pass 指令在 nginx 中托管多个网站。例如,我有 2 个名为 www.webserver1.com 和 www.webserver2.com 的外部网络服务器。

在我的 nginx.conf 中:

server {
    listen 80;
    server_name my.nginx.proxy;

    location /webserver1 {
    proxy_pass http://www.webserver1.com/;
    }

    location /webserver2 {
    proxy_pass http://www.webserver2.com/;
    }
}

我的网络服务器正在响应,但是在向外部网络服务器请求时附加了位置指令中的 uri。例如,当我通过客户端浏览器访问时

my.nginx.proxy/webserver1

请求被传递给

http://www.webserver1.com/webserver1

在这种情况下 /webserver1 不存在,因此返回由我的网络服务器响应的 HTTP ERROR 404。我只想在没有 /webserver1 uri 的情况下重定向到 http://www.webserver1.com,但我的客户端浏览器中的 URL 显示为

my.nginx.proxy/webserver1

【问题讨论】:

    标签: nginx reverse-proxy nginx-location proxypass


    【解决方案1】:

    您基本上做对了,但也许您的 nginx 版本

    在 1.1.12 版本之前,如果指定 proxy_pass 时没有 URI,则 可能会传递原始请求 URI,而不是更改后的 URI 一些情况。

    只有在 nginx 1.1.12 及更高版本中,当您在 proxy_pass 参数中指定 url (/) 时,它们才启用从请求中剥离位置。

      location /webserver1 {
        # trailing slash at end of url should replace /webserver1 with /
        # if your version of nginx is > 1.1.12
        proxy_pass http://www.webserver1.com/; 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        # If you want to pass the original host name (my.nginx.proxy) up to 
        # webserver1, you can set the Host header.
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
      }
    

    尝试更新 nginx。

    http://nginx.org/en/docs/http/ngx_http_proxy_module.html?#proxy_pass

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2015-06-26
      • 1970-01-01
      • 2019-02-12
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      相关资源
      最近更新 更多