【问题标题】:nginx reverse proxy to a set of pages with an additional path in the URL based on http referer?nginx反向代理到一组页面,在基于http引用的URL中有一个附加路径?
【发布时间】:2015-09-15 05:27:03
【问题描述】:

我有一个在 docker 容器中运行的第 3 方 ui 服务器,暴露在端口 8080 上。

似乎期望用绝对路径加载资源:http://localhost:8080/index.html,http://localhost:8080/js/some_jsfiles 等等

我想为它创建一个反向代理,让它看起来像是来自不同的路径:

https://myserver.com/stormui/index.html, https://myserver.com/stormui/js/...

第一次尝试

location /stormui/  {
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         #rewrite ^/stormui/(.*) /$1  break;
         proxy_pass http://127.0.0.1:8080/;
}

index.html 页面加载,但浏览器仍然尝试在没有附加路径的情况下加载引用的内容,所以我在 index.html 引用的所有 javascripts 等上得到 404。

然后我尝试使用referer进行重写 位置 / {

    if ($http_referer ~ "^/stormui/.*") {
        rewrite ^/(.*) /stormui/$1  break;
    }

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    ...
}

那也没用。有没有办法做到这一点?

【问题讨论】:

  • 生成的 index.html 是否包含 js/css 的绝对 URL?

标签: nginx proxy reverse-proxy


【解决方案1】:

我不确定我是否完全理解。来自 UI 服务器(在 localhost:8080 上运行)的 HTML(例如 index.html)是否包含绝对 URL?如果是这样,您有两种选择:

  1. 在代理服务器中重写 HTML。 IE。根据您的需要更改网址。
  2. 我想必​​须有一些设置来配置您的 UI 服务器(源服务器),使其不使用这些绝对 URL 到 localhost:8080。

如果没有关于后端运行的更多详细信息,我无法回答 #2。

【讨论】:

  • UI 是 Apache Storm 的 UI,在 docker 中运行。我深入研究了他们的源代码,确实它看起来像是从他们的 index.html /all /have /absolute /paths 引用的资源。很简陋。我想我唯一的选择是为 nginx 获取 html 重写插件。
  • 您应该努力/说服 Apache Storm 社区修复他们的代码。不允许修改 URL 的“基础”确实很糟糕。理想情况下,他们会尽可能使用相对 URL。事实上,由于无法修改基本 URL,即使不是不可能,也很难将其中一些资源置于 CDN 风格的服务提供商之后。
  • 此外,即使它们是绝对的(但没有 http://),您也可以向 nginx 添加适当的 location / proxy_pass 规则。
【解决方案2】:

我在为 Storm-UI

设置 nginx 反向代理时遇到了类似的问题

经过一段时间的挖掘,我得到了它的工作。

server {
    listen  80;

    server_name  example.com;

    location ^~ /css/ {
        rewrite /(.*) /storm-ui/$1;
    }

    location ^~ /js/ {
        rewrite /(.*) /storm-ui/$1;
    }

    location ^~ /templates/ {
            rewrite /(.*) /storm-ui/$1;
    }

    location ^~ /api/ {
            rewrite /(.*) /storm-ui/$1;
    }

    location ~ ^/topology(.*) {
            rewrite /(.*) /storm-ui/$1;
    }

    location  /storm-ui/ {
        proxy_redirect  /  /storm-ui/;
        #proxy_pass  http://<STORM_MASTER_IP/HOSTNAME>:<PORT>/;
        proxy_pass  http://10.14.23.10:8080/;
    }       
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 2020-10-15
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 2019-08-05
    相关资源
    最近更新 更多