Score:3

Nginx not always serving js and css when load balancing between docker containers - works on refresh

us flag

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:

404 errors from page load

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;
        }
}
ru flag
Look at logs for nginx. Looks like 404 is not nginx response, but backend response.
Andy Baxter avatar
us flag
Thanks for this. Yes does seem to be that the request is being sent upstream fine, but the docker container server is hit-and-miss with its response. I've now tried `ip_hash` to add persistence to each server and testing from two IPs I'm getting perfect responses from both. It just seems to be when one device sequentially sends queries to both that problems arise.
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.