【问题标题】:Problem with nginx container to reverse proxy rancher container with rewrite urlnginx 容器的问题与重写 url 反向代理牧场主容器
【发布时间】:2019-07-20 21:57:10
【问题描述】:

在我的 docker 主机上。使用 nginx 容器,我尝试通过使用 url 名称来反向代理多个服务来识别正确的服务(portainer、rancher)。

来自https://host1/rancher 的流量 => https/rancher-container

来自https://host1/portainer 的流量 => http://portainer-container:9000

我将 nginx 配置为使用 url rewrite 来转换 url,然后再将其发送到良好的服务。 它适用于服务搬运工。但它不适用于 rancher 2 服务。

这是我的配置:

    location /rancher/ {
       rewrite ^/rancher^/ /$1 break;
       proxy_pass         https://rancher-container;
       proxy_redirect     off;
       proxy_set_header   Host $host;
       proxy_set_header   X-Real-IP $remote_addr;
       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header   X-Forwarded-Host $server_name;
       proxy_set_header   Upgrade $http_upgrade;
       proxy_set_header   Connection "upgrade";
       proxy_http_version 1.1;
     }

    location /portainer/ {
      rewrite ^/portainer^/ /$1 break;
      proxy_pass         http://portainer-container:9000/;
      proxy_redirect     off;
      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   X-Forwarded-Host $server_name;
      proxy_set_header   Upgrade $http_upgrade;
      proxy_set_header   Connection "upgrade";
    }

当我从我的 chromeDevTools 中查看时,我看到了:

GET https://host1/rancher/ 200 OK(并像这样返回索引页面): <!DOCTYPE html> <html> ... <link id="vendor" rel="stylesheet" href="/assets/vendor.css"> ... <script src="/assets/vendor-ebb1f9e6b4381d69a55448a2a5d7e4c9.js"></script> <script src="/assets/ui-72ee502ee50a84d0f416c3164137307d.js"></script> ... 获取https://host1/assets/vendor.cssnet::ERR_ABORTED 404 获取https://host1/assets/vendor-ebb1f9e6b4381d69a55448a2a5d7e4c9.jsnet::ERR_ABORTED 404 获取https://host1/assets/ui-72ee502ee50a84d0f416c3164137307d.js

我认为我的网络浏览器会尝试在 html 页面上使用“/assets/”到“https://host1/assets”等相对 URL 定义资源(css、js、img ..etc)。或者好的 url 是'https://host1/racher/assets'。

有解决办法吗?

【问题讨论】:

    标签: docker nginx reverse-proxy rancher


    【解决方案1】:

    检查网站。可能在 HTML 代码中,您的网站不知道它不是在额外路径上运行,而是假定它在 root 上。也许有一些开关可以让您为网站设置一个基本路径以知道它在哪里。

    不幸的是,这是一个相当普遍的问题。默认代理标头不发送原始路径,因此隐藏在后面的服务通常不知道请求是针对 / 以外的其他内容。

    如果代理背后的服务是您的,您可以传递一些额外的标头,例如 x-proxy-urlpath 并通知服务它应该呈现包含该路径的链接。

    如果没有,并且您无法在网站中设置基本 url - 最好的解决方案是使用子域和基于主机的代理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-17
      • 2021-05-04
      • 2018-03-20
      • 2014-02-01
      • 1970-01-01
      • 2017-03-21
      • 2020-02-26
      • 2020-09-10
      相关资源
      最近更新 更多