内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
<
风哥提示:
p>本文档介绍Web服务器集群的部署和配置方法。
Part01-Nginx负载均衡配置
1.1 安装Nginx
[root@lb-server ~]# dnf install -y nginx
Updating Subscription Management repositories.
Last metadata expiration check: 0:05:23 ago on Fri Apr 4 22:30:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
nginx x86_64 1:1.20.1-10.el9 appstream 500 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 500 k
Installed size: 1.5 M
Downloading Packages:
nginx-1.20.1-10.el9.x86_64.rpm 1.0 MB/s | 500 kB 00:00
——————————————————————————–
Total 1.0 MB/s | 500 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : nginx-1:1.20.1-10.el9.x86_64 1/1
Running scriptlet: nginx-1:1.20.1-10.el9.x86_64 1/1
Verifying : nginx-1:1.20.1-10.el9.x86_64 1/1
Installed:
nginx-1:1.20.1-10.el9.x86_64
Complete!
# 启动Nginx
[root@lb-server ~]# systemctl enable –now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
# 查看状态
[root@lb-server ~]# systemctl status nginx
● nginx.service – The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
Active: active (running) since Fri 2026-04-04 22:35:00 CST; 10s ago
Process: 12345 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 12346 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 12347 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 12348 (nginx)
Tasks: 3 (limit: 11232)
Memory: 5.0M
CGroup: /system.slice/nginx.service
├─12348 “nginx: master process /usr/sbin/nginx”
├─12349 “nginx: worker process”
└─12350 “nginx: worker process”
1.2 配置负载均衡
[root@lb-server ~]# cat > /etc/nginx/conf.d/loadbalancer.conf << 'EOF' upstream webcluster { # 负载均衡算法 least_conn; # 后端服务器 server 192.168.1.11:80 weight=3; server 192.168.1.12:80 weight=2; server 192.168.1.13:80 weight=1; # 健康检查 keepalive 32; } server { listen 80; server_name www.fgedu.net.cn; # 访问日志 access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log; # 反向代理配置 location / { proxy_pass http://webcluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 超时配置 proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; # 缓冲配置 proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 32k; } # 健康检查端点 location /health { access_log off; return 200 "OK\n"; } } EOF # 测试配置 [root@lb-server ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # 重载配置 [root@lb-server ~]# systemctl reload nginx # 查看后端服务器状态 [root@lb-server ~]# curl -s http://192.168.1.11/health OK [root@lb-server ~]# curl -s http://192.168.1.12/health OK [root@lb-server ~]# curl -s http://192.168.1.13/health OK
Part02-后端Web服务器配置
2.学习交流加群风哥微信: itpux-com1 配置Web服务器
[root@web1 ~]# dnf install -y nginx
# 配置Web服务器
[root@web1 ~]# cat > /etc/nginx/conf.d/web.conf << 'EOF'
server {
listen 80;
server_name _;
root /var/www/html;
index index.html index.php;
# 访问日志
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ =404;
}
# PHP配置
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 健康检查
location /health {
access_log off;
return 200 "OK\n";
}
}
EOF
# 创建测试页面
[root@web1 ~]# echo "Web Server 1 - $(hostname)" > /var/www/html/index.html
# 启动服务
[root@web1 ~]# systemctl enable –now nginx
# 配置防火墙
[root@web1 ~]# firewall-cmd –permanent –add-service=http
success
[root@web1 ~]# firewall-cmd –reload
success
# 测试负载均衡
[root@lb-server ~]# for i in {1..6}; do curl -s http://www.fgedu.更多视频教程www.fgedu.net.cnnet.cn; done
Web Server 1 – web1
Web Server 1 – web1
Web Server 1 – web1
Web Server 2 – web2
Web Server 2 – web2
Web Server 3 – web3
2.2 配置会话保持
[root@lb-server ~]# cat > /etc/nginx/conf.d/loadbalancer.conf << 'EOF' upstream webcluster { # IP Hash会话保持 ip_hash; server 192.168.1.11:80; server 192.168.1.12:80; server 192.168.更多学习教程公众号风哥教程itpux_com1.13:80; } server { listen 80; server_name www.fgedu.net.cn; location / { proxy_pass http://webcluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } EOF # 配置Cookie会话保持 [root@lb-server ~]# cat > /etc/nginx/conf.d/loadbalancer-cookie.conf << 'EOF' upstream webcluster { server 192.168.1.11:80; server 192.168.1.12:80; server 192.168.1.13:80; } server { listen 80; server_name www.fgedu.net.cn; location / { proxy_pass http://webcluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # 设置会话Cookie add_header Set-Cookie "SERVERID=$upstream_addr; Path=/"; } } EOF # 重载配置 [root@lb-server ~]# nginx -t && systemctl reload nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
- 使用健康检查监控后端服务器
- 配置合理的负载均衡算法
- 实施会话保持策略
- 配置SSL/TLS加密
- 监控集群性能指标
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
