centos下配置Nginx反向代理负载均衡服务器教程

作者:袖梨 2022-06-30

准备:

Proxy-Server:1.1.1.22(负载均衡服务器)
Nginx-Server23:1.1.1.23(web23)
Nginx-Server24:1.1.1.24(web24)

环境版本:

Linux Nginx-Proxy 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

①:安装配置完成Nginx

WEB服务器预配置:

[root@Nginx-Server23 ~]# echo "1.1.1.23-server23" > /usr/local/nginx/html/index.html
[root@Nging-Server24 ~]# echo "1.1.1.24-server24" > /usr/local/nginx/html/index.html

负载均衡服务器配置:

[root@Nginx-Proxy conf]# egrep -v "#|^$" /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www.111com.net {
server 1.1.1.23:80 weight=20; #weight值决定分配值,对此负载服务器大约访问两次1.1.1.23,访问一次1.1.1.24
server 1.1.1.24:80 weight=10;
}
server {
listen 80;
server_name www.111com.net;
location / {
proxy_pass https://www.111cn.net;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

备注:server 1.1.1.23:80 weight=20; 等同于 server 1.1.1.23:80 weight=20 max_fails=1 fail_timeout=10s;

测试:

[root@Nginx-Proxy conf]# curl 1.1.1.22
1.1.1.23-server23
[root@Nginx-Proxy conf]# curl 1.1.1.22
1.1.1.23-server23
[root@Nginx-Proxy conf]# curl 1.1.1.22
1.1.1.24-server24
[root@Nginx-Proxy conf]# curl 1.1.1.22
1.1.1.23-server23
[root@Nginx-Proxy conf]# curl 1.1.1.22
1.1.1.23-server23
[root@Nginx-Proxy conf]# curl 1.1.1.22
1.1.1.24-server24


备注:当其中一台WebServer宕机以后,大概需要约10s后此台服务器将被剔除

相关文章

精彩推荐