nginx - nginx请求去同一台服务器
问题描述
我正在使用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 #
推荐阅读
- antlr4 - ANTLR4 - 如何更改源代码并将其取回?
- python - 如果 pip3 在安装后无法正常工作,我该如何解决?
- amazon-web-services - AWS ETL 作业创建一个新列,该列是现有列的子字符串
- javascript - 什么时候必须在 useReducer 中使用扩展运算符?
- shopify - 按产品 SKU 对集合页面进行排序?
- reactjs - 升级reactjs后无法构建三星node_modules/babel-preset-react-app/index.js$0的未知版本4
- pandas - 从具有分组列的数据框中获取随机样本?
- javascript - 调用不带括号的函数?
- python - Python中的MongoDB:从聚合查询的结果中获取特定值
- mysql - sql不正确日期输出不正确