Nginx url rewrite for static assets

My issue: I need some help with url rewrite in nginx. Currently I am using nginx for reverse proxy. I have created below location directives in nginx.conf file for url rewrite for different tools and it works. However in order to add any new tool I need to know the location for all static files required by the tool to load the ui correctly. Is there a way nginx can handle this automatically without me pointing the rewrite for each folder. I cannot change the location of static file since I dont own source code for any of these tools

Example location /stable- , location ~ ^/(static|api|kernel|lsp|kernelspecs)/ and location ~ ^/stata/(css|js|icons)/ . How can I do rewrite so that previous 3 locations directives are not required.

How I encountered the problem: I have created the conf file myself. When I was adding any tool my browser was give 404 for most of the static files

Solutions I’ve tried: I have read several post online but didn’t find a way to do it other than what I have done

My config:

server {
        listen 80;
        server_name _;
        
        error_log /opt/openresty/nginx/logs/error.log info;
        error_log /opt/openresty/nginx/logs/error.log error;
        # root /opt/openresty/nginx

        location / {
                return 404;
            }

        location @proxy_backend_vscode {
            proxy_pass http://localhost:3000;  # Proxy to the dynamically set backend
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection upgrade;
            proxy_set_header Accept-Encoding gzip;
            proxy_http_version 1.1;
        }

        location /stable- {
            rewrite ^/stable-(.*)$ /vscode/stable-$1 last;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection upgrade;
            proxy_set_header Accept-Encoding gzip;
            proxy_http_version 1.1;
        
        }

        location @proxy_backend_jupyter_lab {    
                proxy_pass http://localhost:8888;  # Forward to Jupyter backend
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Accept-Encoding gzip;
                proxy_http_version 1.1;
        }
        location ~ ^/(static|api|kernel|lsp|kernelspecs)/ {
                proxy_pass http://localhost:8888;
                proxy_set_header Host $host;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Accept-Encoding gzip;
                proxy_http_version 1.1;
        }

        location @proxy_backend_stata {
            proxy_pass http://localhost:9999;  # Proxy to the dynamically set backend
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection upgrade;
            proxy_set_header Accept-Encoding gzip;
            proxy_http_version 1.1;
        }

        location ~ ^/stata/(css|js|icons)/ {
            rewrite ^/stata/(css|js|icons)/(.*)$ /$1/$2 break;
            proxy_pass http://localhost:9999;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection upgrade;
            proxy_set_header Accept-Encoding gzip;
            proxy_http_version 1.1;
        }
    }
2 Likes

Can you share some more information about the applications you are trying to proxy? It might be easiest to break out the applications into separate server blocks to simplify the config. Also, I am not seeing a root location defined for serving the static assets, but instead it seems that all traffic end up being proxied to localhost, is that how you expect the files to be served?