之前也用过nginx的负载均衡,但是一直都没时间去记录一下,今天又实际操作了一次,就抽个时间简单写一下吧
我的/ect/nginx/nginx.conf是我的配置文件
1 | vim /ect/nginx/nginx.conf |
配置文件都一样,我就直接说重点部分了
weight是设置权重1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
upstream todaysay{
server 127.0.0.1:11001 weight=4;
server 127.0.0.1:11002 weight=3;
server 127.0.0.1:11003 weight=2;
server 127.0.0.1:11004 weight=1;
}
server {
listen 11000;
#access_log on;
location / {
#proxy_redirect on;
proxy_pass http://todaysay;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
}
}
保存退出,先检查一下文件是否正确1
2
3$ ginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
一般nginx是80端口,可以先杀掉nginx
1 | sudo fuser -k 80/tcp |
加载配置文件
1 | ginx -c /etc/nginx/nginx.conf |
重启服务
1 | service nginx start |
检查状态1
2$ service nginx status
* nginx is running
到这里你就可以用11000端口nginx,nginx会自动分配到你指定的某个端口上来处理请求
希望可以帮到你,Best wishes