nginx反向代理到一组页面,该页面在基于http referer的URL中具有附加路径?

nginx反向代理到一组页面,该页面在基于http referer的URL中具有附加路径?,nginx,proxy,reverse-proxy,Nginx,Proxy,Reverse Proxy,我有一个运行在docker容器中的第三方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 /st

我有一个运行在docker容器中的第三方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引用的所有Javascript等上得到了404

然后我试着用referer来重写 地点/{

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

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

这也不起作用。有没有办法做到这一点?

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

  • 在代理服务器中重写HTML,即根据需要更改URL
  • 我认为必须有一些设置来配置UI服务器(源服务器),这样它就不会使用这些指向localhost:8080的绝对URL

  • 如果没有关于后端运行内容的更多详细信息,我无法回答#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/;
        }       
    }
    
    服务器{
    听80;
    server_name example.com;
    地点^~/css/{
    重写/(.*)/storm ui/$1;
    }
    地点^~/js/{
    重写/(.*)/storm ui/$1;
    }
    位置^~/模板/{
    重写/(.*)/storm ui/$1;
    }
    地点^~/api/{
    重写/(.*)/storm ui/$1;
    }
    位置~^/拓扑结构(.*){
    重写/(.*)/storm ui/$1;
    }
    位置/风暴界面/{
    代理重定向//风暴用户界面/;
    #代理传递http://::;
    代理通行证http://10.14.23.10:8080/;
    }       
    }
    
    生成的index.html是否包含指向js/css的绝对URL?该UI是运行在docker中的Apache storm的UI。我深入研究了他们的源代码,确实看起来像是从index.html/all/have/absolute/path引用的资源。非常脆弱。我想我唯一的选择是为nginx获得html重写插件。你知道吗你应该努力/说服Apache Storm社区修复他们的代码。不允许修改“基础”大多数URL确实很糟糕。理想情况下,它们会在可能的情况下使用相对URL。事实上,如果不能修改基本URL,则很难(如果不是不可能的话)将其中一些资源放在CDN样式的服务提供商后面。此外,即使它们是绝对的(但没有http://),您可以向nginx添加适当的位置/代理\u传递规则。