Search code examples
nginxurl-rewritingsubdomainreverse-proxy

NGINX proxy_pass rewrite asset uri


I'm trying to do a basic NGINX reverse proxy by subdomian, to localhost/folder and am stumped getting it to rewrite my assets+links.

My http://localhost:8080/myapp/ works like a charm, but via NGINX+subdomain it fails on the subfolder assets.

I believe I'm stumped on the 'rewrite' clause for NGINX.

How can I rewrite the HTML going to the client browser to drop the /myapp/ context?

server {
    listen       443 ssl;
    server_name  app1.domain.com;
    location / {
        rewrite ^/myapp/(.*) /$1 break; # this line seems to do nothing
        proxy_pass http://localhost:8080/myapp/;
    }
}

I'm expecting my resultant HTML (via https://app1.domain.com) to be rewritten without the subfolder /myapp/, so when assets are requested they can be found instead of a 404 against https://app1.domain.com/myapp/assets/. It should just be https://app1.domain.com/assets/ (which if I manually go there they work)

--thanks.


Solution

  • Feeding from Ivan's response and finalizing my solution as:

    server {
        listen       443 ssl;
        server_name  app1.domain.com;
        location / {
            sub_filter '/myapp/'  '/'; # rewrites HTML strings to remove context
            sub_filter_once off; # ensures it loops through the whole HTML (required)
            proxy_pass http://localhost:8080/myapp/;
        }
    }