nginx请求发送到同一服务器

nginx请求发送到同一服务器,nginx,Nginx,我正在使用nginx进行负载平衡。我已经尝试了循环和最少连接。我有三台服务器在端口3001、3002和3003上本地运行。但所有请求总是只发送到3001服务器。以下是我的配置: worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile

我正在使用
nginx
进行负载平衡。我已经尝试了
循环
最少连接
。我有三台服务器在端口
3001
3002
3003
上本地运行。但所有请求总是只发送到
3001
服务器。以下是我的配置:

worker_processes 1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    upstream my_http_servers {
        least_conn;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
        server 127.0.0.1:3003;
    }
    server {
        listen       3000;
        server_name  localhost;

        location / {
            proxy_pass         http://my_http_servers;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

我不确定为什么连接总是只连接到一台服务器。

将此添加到
/etc/hosts
时:

127.0.0.1       localhost
127.0.0.2       localhost1
127.0.0.3       localhost2
127.0.0.4       localhost3
此配置可用于:

worker_processes 1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    upstream my_http_servers {
        least_conn;
        server localhost1:3001;
        server localhost2:3002;
        server localhost3:3003;
    }
    server {
        listen       3000;
        server_name  localhost;

        location / {
            proxy_pass         http://my_http_servers;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       3001;
        server_name  localhost1;
        root html/3001;

        location / {
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       3002;
        server_name  localhost2;
        root html/3002;

        location / {
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       3003;
        server_name  localhost3;
        root html/3003;

        location / {
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
我将3个服务器名添加到了一个不同的
,只是为了能够看到哪个服务器进行回复

opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3003
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3001
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3002
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3003
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3001
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3002
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3003
opensuse1:/var/log/nginx # curl -s -q  "http://localhost:3000/"  |grep 300
                192.168.168.251 3001
opensuse1:/var/log/nginx #

你只有一个。我认为您应该将其更改为3或更高版本,或者更改为“自动”(请参阅文档)。感谢我尝试了3和
auto
这两个选项,但它仍然只连接到1台服务器