Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 728
adapt-dev Avatar asked Jun 28 '15 19:06

adapt-dev


People also ask

Can NGINX be used as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

How do I use NGINX as an https forward proxy?

The following steps briefly outlines the process. 1) The client sends an HTTP CONNECT request to the proxy server. 2) The proxy server uses the host and port information in the HTTP CONNECT request to establish a TCP connection with the target server. 3) The proxy server returns an HTTP 200 response to the client.

What is proxy_set_header in NGINX?

Passing Headers to Handle Proxied Requests. Apart from proxy_pass, NGINX offers many other directives to handle requests to your server blocks. One of these directives is proxy_set_header, which lets you pass/rewrite headers to handle proxied requests.

How does NGINX work as a reverse proxy?

Nginx automatically modifies the request headers it receives from the client when it proxies a request: Nginx removes any empty headers. By default, Nginx removes any header containing underscores from the proxy request. Set the underscores_in_headers directive to on to include them in headers.


1 Answers

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.

like image 91
Leif Hedstrom Avatar answered Oct 03 '22 04:10

Leif Hedstrom