为什么在带有 nginx 的负载均衡器中,它找不到其他服务器中的文件?

why in load-balancer with nginx, it doesnt find files in other servers?

我用 nginx 和 apache 设置了负载平衡。 nginx 是反向代理,在 2 个独立的系统中有 2 个 apache web 服务器。

在我的机器上,当我请求 localhost 时,它工作正常。但是如果我请求一个文件(例如 info.php)在其他 2 个服务器上可用,它就找不到它并且它总是显示本地 info.php 文件并且从不显示 info.php 其他服务器上的文件。 如果我从我的机器上删除这个文件 (info.php),它会显示 404 错误。

这是我的 nginx 设置:

upstream localhost  {
    ip_hash;
    server 1.2.3.4;
    server 1.2.3.5;    
}
server {
        listen 80;
        listen [::]:80;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name localhost;
        location / {
            try_files $uri $uri/ =404;
            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_redirect off;
            proxy_pass  http://localhost;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
    deny all;
}

}

文件 info.php 在另外 2 个服务器上可用。

首先,不要使用 localhost 作为您的上游名称。为什么要将 localhost(当前机器)与上游本地主机混淆。使用下面

upstream myservers  {
    ip_hash;
    server 1.2.3.4;
    server 1.2.3.5;    
}
server {
        listen 80;
        listen [::]:80;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name localhost;
        location / {
            try_files $uri $uri/ =404;
            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_redirect off;
            proxy_pass  http://myservers;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
    deny all;
}

}