nginx 反向代理到一组页面,在 URL 中基于 http referer 有一个额外的路径?

nginx reverse proxy to a set of pages with an additional path in the URL based on http referer?

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

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

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

https://myserver.com/stormui/index.htmlhttps://myserver.com/stormui/js/...

第一次尝试

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

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

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

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

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

那也没用。有办法吗?

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

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

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

我在为 Storm-UI

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

经过一段时间的挖掘,我开始工作了。

server {
    listen  80;

    server_name  example.com;

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

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

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

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

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

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