1. 环境规划:
| 主机名 | 主机地址 | 角色 |
| node1 | 192.168.188.11 | 负载调度器 |
| node2 | 192.168.188.12 | RS1 |
| node3 | 192.168.188.13 | RS2 |
| node4 | 192.168.188.14 | 测试主机 |
2. Nginx配置负载均衡加反向代理:
1>. node1,node2,node3安装Nginx:
[root@node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com
/repo/epel-7.repo
[root@node1 ~]# yum install nginx -y
2>. node2,node3配置nginx主页:
##node2,node3配置相同
[root@node2 ~]# echo "`hostname -I`" > /usr/share/nginx/html/index.html
[root@node2 ~]# systemctl start nginx.service
3>. node1配置Nginx的负载均衡和反向代理:
[root@node1 ~]# vim /etc/nginx/nginx.conf
http {
......
upstream wwwServerPools { ##"wwwServerPools"可以自定义,但是新版本不能再
使用下划线
server 192.168.188.12:80 weight=1;
server 192.168.188.13:80 weight=1;
server 127.0.0.1:80 backup; ##表示如果两台后端服务器全部宕机,则访问127.0.0.1:80
}
......
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://wwwServerPools; ##访问node1时将请求发给wwwServerPools
里面的节点
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

本文详细介绍了如何在Nginx中设置负载均衡和反向代理,包括环境规划、Nginx安装、配置负载均衡、反向代理多虚拟主机以及根据URL目录实现代理转发。同时讲解了`proxy_set_header host $host`参数的重要性,用于在代理请求中标识后端服务器的虚拟主机。
最低0.47元/天 解锁文章
2957

被折叠的 条评论
为什么被折叠?



