I have Nginx set up as a reverse proxy load balancing between two docker containers running all on the same server. When loading a page for the first time, the page loads but I get lots of 404 errors for all css and js files:
When refreshing, or opening a second tab, all these errors disappear and the page loads fine. When I reduce this to serving only one container it similarly works fine.
I had initially thought this was because the js and css was being requested from the same root url, and some aspect of the load balancers and containers was throwing up errors as a cluster of requests for a single user were balanced between the two servers. Through a little bit of experimenting I've tried using proxy_set_header Host $host
(looking here and here for answers) - my understanding would be that this should send a single user's subsequent requests to the same upstream server. This seemed to make the problem occur more rarely but hasn't eliminated it entirely.
My remaining questions (from a learning amateur!):
- Is this the correct use of
proxy_set_header
, and is it thus possible to serve js/css from the same upstream server to a single user, rather than load balancing these relatively minor requests across two servers? - Is this the likely root of the problem, that with the second container having to respond to some of the requests prompted by loading of page for first container these responses are somehow not lining up?
- My user base would be 100-200 users simultaneously accessing a remote url from a single location. I don't think
ip_hash
would then work as all requests would come from the same IP? Are there other ways of more efficiently tying a single user to a single server?
My nginx config file:
upstream backend {
least_conn;
server localhost:4000;
server localhost:4001;
}
server {
listen 80;
listen [::]:80;
server_name xxxxxxxxxx;
location / {
proxy_pass http://backend;
proxy_redirect http://backend/ $scheme://$host/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
proxy_buffering off;
}
}