【问题标题】:Problem with nginx location and proxy_passnginx位置和proxy_pass的问题
【发布时间】:2019-10-29 14:36:42
【问题描述】:

我的 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

【问题讨论】:

  • 试试:proxy_pass https://localhost:8181/oan/resources/;
  • 太棒了。这就像一个魅力!!!

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


【解决方案1】:

当您在 prefix 位置块中使用带有 可选 URI 的 proxy_pass 时,Nginx 将通过执行直接文本替换来转换请求的 URI。

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

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

例如:

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

详情请见this document。

【讨论】:

  • 在将 proxy_pass 与 set 结合使用时,我们遇到了无限重定向的问题。删除变量和 proxy_pass 中的匹配路径,并将路径留在该位置,并删除尾随的 / 以解决所有问题。您的回答帮助找到了这个,谢谢
  • 对于任何感兴趣的人来说,它看起来像这样: location /_plugin/kibana { set $kibana kibana-url.com; proxy_pass $kibana; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2018-08-06
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
相关资源
最近更新 更多