Nginx multiple sites, redirecting by URL

nginx

Let's say I have several web UI sites used to monitor backend services.

I would like to make them accessible from the web (secured by Auth) but ideally bundled under one sub-domain, and accessible through various URLs like:

https://monitor.mydomain.com/my-service-monitor/

Using nginx, if i use a rewrite rule like

server {
    listen 443 ssl;
    server_name monitor.mydomain.com;
    # auth stuff here...

    location /my-service-monitor/ {
        rewrite                     ^/my-service-monitor(/.*)$ $1 break;
        proxy_pass                  http://my-service-monitor;
    }
} 

what happens is that the redirect works, but because all the static content refers to the root URL, it doesn't show as expected (because the back end site is outputting HTML looking at the root / url, not the relative URL monitor.mydomain.com/my-service-monitor/…

How can I fix this problem, or is there an easier solution to this?
(I could use multiple subdomains, but that's too much outside exposure IMO)

I was thinking of redirecting (with a return 301 …) to the sub-domain root URL but on a different port, and then add a server bloc listening on that port proxying to the server, but I feel like that's going to be trouble with auth.

Any insight would be appreciated.

Best Answer

You need to teach your backend application to generate correct URLs for the page. There are people who are, apparently, writing custom lua code to modify the returned HTML from within nginx, but honestly I'd recommend avoiding that because it is insane.

Related Topic