nginx负载均衡
在5机器设置web集群负载均衡
1、配置nginx的yum源
[root@lb-5 ~]#cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
2、安装nginx
[root@slb-5 ~]# yum clean all
[root@slb-5 ~]# yum install nginx -y
3、放入反向代理参数
[root@slb-5 ~]# cat /etc/nginx/proxy_params.conf
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
实战nginx反向代理
5机器
[root@slb-5 /etc/nginx/conf.d]#cat proxy.conf
server{
listen 80;
server_name wordpress.laoliu.cc;
location / {
proxy_pass http://172.16.1.8:12345;
include /etc/nginx/proxy_params.conf;
}
}
重启
[root@slb-5 /etc/nginx/conf.d]#systemctl start nginx
web机器(8)
[root@web-8 /etc/nginx/conf.d]#cat wordpress.conf
server{
listen 12345;
server_name wordpress.laoliu.cc;
# 静态请求,资源存放路径
root /code/wordpress;
index index.php index.html;
# 动态请求处理
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
做好dns解析
日志访问
tail -f /var/log/nginx/access.log
负载均衡策略
wordpress的权重
5机器配置文件
[root@slb-5 /etc/nginx/conf.d]#cat proxy.conf
upstream web-pools{
server 172.16.1.7:12345 weight=4;
server 172.16.1.8:12345 weight=1;
}
server{
listen 80;
server_name wordpress.laoliu.cc;
location / {
proxy_pass http://web-pools;
include /etc/nginx/proxy_params.conf;
}
}
[root@slb-5 /etc/nginx/conf.d]#systemctl restart nginx
web机器(7、8)
[root@web-7 /etc/nginx/conf.d]#cat wordpress.conf
server{
listen 12345;
server_name wordpress.laoliu.cc;
# 静态请求,资源存放路径
root /code/wordpress;
index index.php index.html;
# 动态请求处理
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web-7 /etc/nginx/conf.d]#systemctl restart nginx
[root@web-8 /etc/nginx/conf.d]#cat wordpress.conf
server{
listen 12345;
server_name wordpress.laoliu.cc;
# 静态请求,资源存放路径
root /code/wordpress;
index index.php index.html;
# 动态请求处理
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
wecenter
51机器
[root@slb-5 /etc/nginx/conf.d]#cat proxy2.conf
upstream web-dsss {
server 172.16.1.7:23456 weight=4;
server 172.16.1.8:23456 weight=1;
}
server{
listen 80;
server_name wecenter.laoliu.cc;
location / {
proxy_pass http://web-dsss;
include /etc/nginx/proxy_params.conf;
}
}
[root@slb-5 /etc/nginx/conf.d]#systemctl restart nginx
web机器(7、8)
[root@web-7 /etc/nginx/conf.d]#cat wecenter.conf
server{
listen 23456;
server_name wecenter.laoliu.cc;
# 静态请求,资源存放路径
root /code/wecenter;
index index.php index.html;
# 动态请求处理
location ~ \.php$ {
root /code/wecenter;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web-7 /etc/nginx/conf.d]#systemctl restart nginx
[root@web-8 /etc/nginx/conf.d]#cat wecenter.conf
server{
listen 23456;
server_name wecenter.laoliu.cc;
# 静态请求,资源存放路径
root /code/wecenter;
index index.php index.html;
# 动态请求处理
location ~ \.php$ {
root /code/wecenter;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
做好backup
将web-8作为备份机,请求只发给web7,当7宕机时,8机器直接接替工作
[root@slb-5 /etc/nginx/conf.d]#cat proxy.conf
upstream web-pools{
server 172.16.1.7:12345 weight=4;
server 172.16.1.8:12345 weight=1 backyp;
}
server{
listen 80;
server_name wordpress.laoliu.cc;
location / {
proxy_pass http://web-pools;
include /etc/nginx/proxy_params.conf;
}
}
[root@slb-5 /etc/nginx/conf.d]#cat proxy2.conf
upstream web-dsss {
server 172.16.1.7:23456 weight=4;
server 172.16.1.8:23456 weight=1 backup;
}
server{
listen 80;
server_name wecenter.laoliu.cc;
location / {
proxy_pass http://web-dsss;
include /etc/nginx/proxy_params.conf;
}
}
重启
systemctl restart nginx
七没挂时
七挂了,八接替工作
根据用户client_agent判断转发
wordpress.yuchaoit.cc
51机器
[root@slb-5 /etc/nginx/conf.d]#cat wordpress.yuchaoit.conf
upstream android {
server 172.16.1.7:22333;
}
upstream iphone {
server 172.16.1.8:22333;
}
upstream pc {
server 172.16.1.7:22222;
server 172.16.1.8:22222;
}
server {
listen 80;
server_name wordpress.yuchaoit.cc;
location / {
#默认页面,交给pc
proxy_pass http://pc;
include proxy_params.conf;
#判断时iphone用户,基于正则且不区分大小写
if ($http_user_agent ~* "iphone") {
proxy_pass http://iphone;
}
#判断是安卓用户,与iphone一样
if ($http_user_agent ~* "android") {
proxy_pass http://android;
}
#如果是IE,禁止访问
if ($http_user_agent ~* "msie") {
return 403 "禁止访问//IE已经淘汰,你out了老哥!!\n";
}
}
}
[root@slb-5 /etc/nginx/conf.d]#systemctl restart nginx
创建web(7、8)
创建pc页面
[root@web-7 /etc/nginx/conf.d]#vim pc.conf
[root@web-7 /etc/nginx/conf.d]#mkdir -p /pc/wordpress
[root@web-7 /etc/nginx/conf.d]#echo "我是7机器的 PC页面 啦啦啦!!!!" > /pc/wordpress/index.html
[root@web-7 /etc/nginx/conf.d]#cat pc.conf
server {
listen 22222;
server_name _;
charset utf-8;
location / {
root /pc/wordpress;
index index.html;
}
}
[root@web-7 /etc/nginx/conf.d]#systemctl restart nginx
[root@web-8 /etc/nginx/conf.d]#cat pc.conf
server {
listen 22222;
server_name _;
charset utf-8;
location / {
root /pc/wordpress;
index index.html;
}
}
[root@web-8 /etc/nginx/conf.d]#mkdir -p /pc/wordpress
[root@web-8 /etc/nginx/conf.d]#echo "我是8机器的 PC页面 噢噢噢!!!!" > /pc/wordpress/index.html
访问
创建web组的移动页面
web-7
[root@web-7 /etc/nginx/conf.d]#vim android.conf
[root@web-7 /etc/nginx/conf.d]#cat android.conf
server {
listen 22333;
server_name _;
charset utf-8;
location / {
root /android/wordpress;
index index.html;
}
}
[root@web-7 /etc/nginx/conf.d]#mkdir -p /android/wordpress
[root@web-7 /etc/nginx/conf.d]#echo "我是7机器的 移动端页面 啦啦啦!!!!" > /android/wordpress/index.html
[root@web-7 /etc/nginx/conf.d]#systemctl restart nginx
web-8
[root@web-8 /etc/nginx/conf.d]#vim iphone.conf
[root@web-8 /etc/nginx/conf.d]#cat iphone.conf
server {
listen 22333;
server_name _;
charset utf-8;
location / {
root /iphone/wordpress;
index index.html;
}
}
[root@web-8 /etc/nginx/conf.d]#mkdir -p /iphone/wordpress
[root@web-8 /etc/nginx/conf.d]#echo "我是8机器的 移动页面IPHONE 噢噢噢!!!!" > /iphone/wordpress/index.html
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
访问移动端
测试ie浏览器
wecenter.yuchaoit.cc
51机器
[root@slb-5 /etc/nginx/conf.d]#cat wencenter.yuchaoit.conf
upstream androidr {
server 172.16.1.7:33333;
}
upstream iphones {
server 172.16.1.8:33333;
}
upstream pcc {
server 172.16.1.7:33322;
server 172.16.1.8:33322;
}
server {
listen 80;
server_name wecenter.yuchaoit.cc;
location / {
#默认页面,交给pc
proxy_pass http://pcc;
include proxy_params.conf;
#判断时iphone用户,基于正则且不区分大小写
if ($http_user_agent ~* "iphone") {
proxy_pass http://iphone;
}
#判断是安卓用户,与iphone一样
if ($http_user_agent ~* "android") {
proxy_pass http://android;
}
#如果是IE,禁止访问
if ($http_user_agent ~* "msie") {
return 403 "禁止访问//IE已经淘汰,你out了老哥!!\n";
}
}
}
[root@slb-5 /etc/nginx/conf.d]#systemctl restart nginx
创建web(7、8)
创建pc页面
[root@web-7 /etc/nginx/conf.d]#vim pcc.conf
[root@web-7 /etc/nginx/conf.d]#cat pcc.conf
server {
listen 33322;
server_name _;
charset utf-8;
location / {
root /pc/wecenter;
index index.html;
}
}
[root@web-7 /etc/nginx/conf.d]#mkdir -p /pc/wecenter
[root@web-7 /etc/nginx/conf.d]#echo "我是7机器的 PC页面 啦啦啦!!!!" > /pc/wordpress/index.html
[root@web-7 /etc/nginx/conf.d]#echo "我是7机器的 PC页面 啦啦啦!!!!" > /pc/wecenter/index.html
[root@web-7 /etc/nginx/conf.d]#systemctl restart nginx
[root@web-8 /etc/nginx/conf.d]#vim pcc.conf
[root@web-8 /etc/nginx/conf.d]#cat pcc.conf
server {
listen 33322;
server_name _;
charset utf-8;
location / {
root /pc/wecenter;
index index.html;
}
}
[root@web-8 /etc/nginx/conf.d]#mkdir -p /pc/wecenter
[root@web-8 /etc/nginx/conf.d]#echo "我是8机器的 PC页面 噢噢噢!!!!" > /pc/wecenter/index.html
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
访问
移动端
[root@web-7 /etc/nginx/conf.d]#vim androidr.conf
[root@web-7 /etc/nginx/conf.d]#cat androidr.conf
server {
listen 33333;
server_name _;
charset utf-8;
location / {
root /android/wecenter;
index index.html;
}
}
[root@web-7 /etc/nginx/conf.d]#mkdir -p /android/wecenter
[root@web-7 /etc/nginx/conf.d]#echo "我是7机器的 移动端页面 啦啦啦!!!!" > /android/wecenter/index.html
[root@web-7 /etc/nginx/conf.d]#systemctl restart nginx
[root@web-8 /etc/nginx/conf.d]#vim iphones.conf
[root@web-8 /etc/nginx/conf.d]#cp iphone.conf iphones.conf
[root@web-8 /etc/nginx/conf.d]#vim iphones.conf
[root@web-8 /etc/nginx/conf.d]#cat iphones.conf
server {
listen 33333;
server_name _;
charset utf-8;
location / {
root /iphone/wecenter;
index index.html;
}
}
[root@web-8 /etc/nginx/conf.d]#mkdir -p /iphone/wecenter
[root@web-8 /etc/nginx/conf.d]#echo "我是8机器的 移动页面IPHONE 噢噢噢!!!!" > /iphone/wecenter/index.html
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx
测试IE
wordpress与wecenter动静态转换
51机器
[root@slb-5 /etc/nginx/conf.d]#cat wordpress.conf
upstream android {
server 172.16.1.7:2233;
}
upstream iphone {
server 172.16.1.8:2233;
}
upstream pc {
server 172.16.1.7:22234;
server 172.16.1.8:22234;
}
server {
listen 80;
server_name huya.laoliu.cn;
location / {
#默认页面,交给pc
proxy_pass http://pc;
include proxy_params.conf;
#判断时iphone用户,基于正则且不区分大小写
if ($http_user_agent ~* "iphone") {
proxy_pass http://iphone;
}
#判断是安卓用户,与iphone一样
if ($http_user_agent ~* "android") {
proxy_pass http://android;
}
#如果是IE,禁止访问
if ($http_user_agent ~* "msie") {
return 403 "禁止访问//IE已经淘汰,你out了老哥!!\n";
}
}
}
web-7
[root@web-7 /etc/nginx/conf.d]#cat wecenter.conf
server{
listen 22234;
server_name wecenter.laoliu.cc;
# 静态请求,资源存放路径
root /code/wecenter;
index index.php index.html;
# 动态请求处理
location ~ \.php$ {
root /code/wecenter;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web-7 /etc/nginx/conf.d]#systemctl restart nginx
web-8
[root@web-8 /etc/nginx/conf.d]#cat wordpress.conf
server{
listen 22234;
server_name wordpress.laoliu.cc;
# 静态请求,资源存放路径
root /code/wordpress;
index index.php index.html;
# 动态请求处理
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?