I have a problem with nginx proxy_pass
not passing asset requests [css, js, fonts, etc] to the upstream server. Details about my configuration are below.
I have the following configuration for nginx on my application server set up:
upstream webapp {
server 127.0.0.1:10000;
}
server {
server_name wma.xxxxx.io;
include includes/_general.conf;
location / {
access_log /var/log/nginx/webapp/access.log upstream_logging;
error_log /var/log/nginx/webapp/error.log warn;
proxy_pass http://webapp/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_redirect http://$host:10000 https://$http_host;
#index index.html;
}
listen 5555;
}
Now, when I navigate to the url http://wma.xxxxx.io, the page /loads/ – but none of the resources associated with it, do. So, no css, no js files, etc.
When I request a document from the server that is behind the proxy, it works:
root@appserver [/etc/nginx/sites-enabled] # curl -I http://localhost:10000/service-worker.js
HTTP/1.0 200 Document follows
Date: Fri, 9 Aug 2024 05:54:45 GMT
Server: MiniServ
Content-type: application/javascript
Last-Modified: Wed, 24 Jul 2024 04:58:46 GMT
Expires: Fri, 16 Aug 2024 05:54:45 GMT
Cache-Control: public; max-age=604800
Content-length: 94
Connection: close
However, performing the same request to the nginx
proxy:
root@appserver [/etc/nginx/sites-enabled] # curl -I http://localhost:5555/service-worker.js
HTTP/1.1 404 Not Found
Server: nginx/1.20.1
Date: Fri, 09 Aug 2024 05:54:39 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 153
Connection: keep-alive
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Referrer-Policy: no-referrer-when-downgrade
Content-Security-Policy: default-src * data: 'unsafe-eval' 'unsafe-inline'
We get that 404 error. This error occurs for all resources: fonts, css, images, javascript, and so forth.
The _general.conf file contains the following:
#set_real_ip_from 10.2.46.0/24;
#real_ip_header proxy_protocol;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
# . files
#location /.well-known/acme-challenge/ {
# try_files $uri /dev/null =404;
#}
location ~ /. {
deny all;
}
# assets, media
location ~* .(?:css(.map)?|js(.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
expires 7d;
access_log off;
}
# svg, fonts
location ~* .(?:svgz?|ttf|ttc|otf|eot|woff|woff2)$ {
add_header Access-Control-Allow-Origin "*";
expires 7d;
access_log off;
}
# gzip rules
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
I am stumped – it seems as though nginx is looking for the assets locally instead of proxy_pass
ing them to the upstream server. If that is the case, how do I make it pass all the requests to the upstream server?