0

I'm having an issue with NGINX setup as a reverse proxy, with a proxy_pass to a IIS hosted website. The index.html of the web app is loaded without a problem, but the HTML page has relative URLS's for static css and js files setup like: '/assets/index-d526a0c5.css'

NGINX then tries to find these files on 'C:\nginx/html/assets/index-d526a0c5.css' which makes sense. I could of course change the URL's to absolute paths, but I would prefer to leave them relative. This is my config:

server {
    listen       80;
    listen       [::]:80;
    server_name  _;

    # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
    return 301 https://$host$request_uri;
}

# HTTPS server
#
server {
    listen       443 ssl;
    server_name  _;
        
    ...

    underscores_in_headers on;        
    port_in_redirect off;

    location /webapp/ {
        proxy_pass http://127.0.0.1:8080/;

        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }

The error I get is:

[error] 7184#11204: *1 CreateFile() "C:\nginx/html/assets/index-d526a0c5.css" failed (3: The system cannot find the path specified), client: , server: _, request: "GET /assets/index-d526a0c5.css HTTP/1.1", host: "internal.host.com", referrer: "https://internal.host.com/webapp/"

I got it to work using sub_filters, but I am not sure if this is the best solution:

        location /webapp/ {
            proxy_pass http://127.0.0.1:8080/;

            ...                

            sub_filter 'action="/'  'action="/webapp';
            sub_filter 'href="/'  'href="/webapp/';
            sub_filter 'src="/'  'src="/webapp/';
            sub_filter_once off;
        }

Is there a better solution?

0 Answers0