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

最后都变了- 提交于 2021-01-26 23:39:11

问题


I have a 3rd-party ui server running in a docker container, exposed on port 8080.

It seems to expect to load resources with an absolute path: http://localhost:8080/index.html, http://localhost:8080/js/some_jsfiles etc.

I want to create a reverse proxy to it so it looks like it is coming from a different path:

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

first I tried

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/;
}

The index.html page loads, but the browser still tries to load the refered content without the additional path, so I get a 404 on all the javascripts etc referenced from index.html.

Then I tried to use referer to do the rewrite location / {

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

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

That didn't work, either. Is there a way to do this?


回答1:


I was having similar issues while setting up nginx reverse proxy for Storm-UI

After digging for sometime, I got it working.

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/;
    }       
}



回答2:


I'm not sure I fully understand. Does the HTML (e.g. index.html) from the UI server (running on localhost:8080) contain absolute URLs? If so, you have two options:

  1. Rewrite the HTML in the proxy server. I.e. change the URLs according to your needs.
  2. I'd imagine there must be some settings to configure your UI server (origin server) such that it doesn't use these absolute URLs to localhost:8080.

I can't answer #2 without more details on what is running on the backend.



来源:https://stackoverflow.com/questions/31104003/nginx-reverse-proxy-to-a-set-of-pages-with-an-additional-path-in-the-url-based-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!