Score:0

NGINX reverse proxy to IIS with relative paths to static files

lt flag

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?

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.