首页 > 解决方案 > Chrome ignores Nginx upstreams (loads only first)

问题描述

I have simple setup of 3 servers (in containers) - 2 "app" servers (whoami services - so by response I can acknowledge server) and nginx server.
I've launched nginx with simple load-balancing configuration:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    upstream myapp1 {
        server w1:8000 weight=1;
        server w2:8000 weight=1;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1/;
        }

    }
}

The problem is that it doesn't work in Chrome - it always loads only first server. I've tried to turn off cache in Dev console + reload via CTRL+F5 but nothing helped.
If I try to curl nginx server - I get responses in round robin manner (as expected).

Here is my containers setup:

docker network create testnw
docker run -dit --name w1 --network testnw jwilder/whoami # app1
docker run -dit --name w2 --network testnw jwilder/whoami # app2
docker run -dit --name ng --network testnw -p 8989:80 -v ${PWD}/my.conf:/etc/nginx/nginx.conf nginx # LB server
curl localhost:8989 # will get response from w1
curl localhost:8989 # will get response from w2
curl localhost:8989 # will get response from w1
...

标签: google-chromenginxload-balancingproxypass

解决方案


编辑 3:发现了一个有趣的问题。在 Chrome 中,每次我访问我的网站时,它都会进行两次调用,无论它们被调用到我的网站/我的网站和我的网站的 /favicon.ico。我没有/favicon.ico。我认为正在发生的事情

  1. 当 Nginx 收到我网站的请求时,它正在上游加载第一台服务器。
  2. 当 chrome 从我的网站加载 / 时,它还会调用我网站的 /favicon.ico,这会导致对 Nginx 进行新的调用,因此它会从上游的下一个服务器加载 .ico 文件。
  3. 发生这种情况时,服务器 1、2、3 按顺序加载 1(来自 2 的 ico 文件)、3(来自 1 的 ico 文件)、2(来自 3 的 ico 文件)。并循环重复。

一旦我停止在 Nginx 中加载 /favicon.ico,我的三个上游服务器 1、2、3 将按循环的 1、2、3 顺序加载。

我把它放在带有上游的服务器中,以禁用从 Nginx 加载 favicon.ico。

location = /favicon.ico {
  log_not_found off;
}

希望任何有这个问题的人都觉得这很有用。

编辑 2:找出问题所在,负载平衡在 Nginx conf 文件中的静态文件和静态服务器上运行良好。但是我的应用程序正在由节点加载,因此必须在启动所有节点服务器后启动 Nginx。当我在 Nginx 运行时重新启动应用程序服务器时,问题再次出现。现在没有问题会很快更新

编辑1:这对我来说不再有效,昨天有效,今天继续使用相同的配置,问题再次出现。

我的设置也有同样的问题。经过大量代理设置、VirtualBox 设置和网络编辑后,什么对我有用。

在 HTTP 块中添加一个额外的服务器块。

server{
}

并重新加载 Nginx 服务。

它对我有用,在 chrome 和 firefox 按给定顺序加载服务器后重新加载后,我删除了服务器块,它仍在工作。

不知道为什么首先提出这个问题。希望这有助于解决您的问题。


推荐阅读