我正在使用nginx
进行负载平衡。我尝试过round_robin
和least_conn
.我有三台服务器在端口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 个不同root
的服务器名称,只是为了能够看到哪个服务器确实进行了回复。
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 #