Nginx位置和proxy_pass

时间:2019-10-29 14:36:42

标签: nginx nginx-reverse-proxy jwilder-nginx-proxy

我的nginx.conf中有一条规则不起作用,我也不知道为什么。根据文档,它应该可以工作。配置的一部分看起来像这样。

端口8100上的第一条规则起作用,并将呼叫 http://example.com/api/domains 重定向到 https://localhost:8181/oan/resources/domains

# Working
server {
    listen                  8100 default_server;
    server_name             example.com;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        Host      $http_host;
    root                    /var/www/html/example;

    location /api {
       proxy_pass https://localhost:8181/oan/resources; break;
    }

    # For ReactJS to handle routes
    location / {
       if (!-e $request_filename) {
          rewrite ^(.*)$ / break;
       }
    }
}

# Not working
server {
    listen                  8200;
    server_name             api.example.com;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        Host      $http_host;

    location / {
       proxy_pass https://localhost:8181/oan/resources; break;
    }
}

对端口8200的最后一次呼叫: http://api.example.com:8200/domains 应该重定向到: https://localhost:8181/oan/resources/domains ,但不要这样做。

此配置有什么问题,如何获取端口8200上的最后一条规则,以执行正确的操作,请始终重定向到 https://localhost:8181/oan/resources/ $ uri

1 个答案:

答案 0 :(得分:1)

当您将proxy_pass前缀位置块中的可选URI 一起使用时,Nginx将通过执行纯文本替换来转换请求的URI。

在您的情况下,前缀位置值为/,可选的URI值为/oan/resources。因此,所请求的URI /foo将转换为/oan/resourcesfoo

为了正确操作,两个值都应以/结尾或都不以/结尾。

例如:

location / {
    proxy_pass https://localhost:8181/oan/resources/;
}

有关详细信息,请参见this document