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 {
}
}
}
4>. node4节点测试:
[root@node4 ~]# for ((i=1;i<=10;i++));
> do
> curl 192.168.188.11;
> done
192.168.188.12
192.168.188.13
192.168.188.12
192.168.188.13
192.168.188.12
192.168.188.13
192.168.188.12
192.168.188.13
192.168.188.12
192.168.188.13
3. Nginx反向代理多虚拟主机节点服务器:
1>. 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
里面的节点
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
[root@node2 ~]# vim /etc/httpd/conf/httpd.conf
<IfModule log_config_module>
.......
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
combined
##将上行修改为:
LogFormat "%h %{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\"
\"%{User-Agent}i\"" combined
.......
</IfModule>
2. "proxy_set_header host $host"参数的作用:在代理向后端服务器发送的 http请求头中加入 host字段信息,用于当后端服务器配置有多个虚拟主机时,可以识别代理的是哪个虚拟主机。这是节点服务器多虚拟主机的关键配置。
2>. node2和node3节点上配置后端虚拟主机,这里使用子配置文件:
##node2和node3节点配置相同
[root@node2 ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name xian.yunjisuan.com;
location / {
root /usr/share/nginx/html/xian;
index index.html;
}
}
server {
listen 80;
server_name chengdu.yunjisuan.com;
location / {
root /usr/share/nginx/html/chengdu;
index index.html;
}
}
3>. 配置访问页面:
##node2和node3配置相同
[root@node2 html]# cd /usr/share/nginx/html/
[root@node2 html]# mkdir {xian,chengdu}
[root@node2 html]# echo "`hostname -I` xian.yunjisuan.com" > xian/index.html
[root@node2 html]# echo "`hostname -I` chengdu.yunjisuan.com" > chengdu/index.html
4>. 重启node1,node2,node3的nginx服务:
[root@node1 ~]# systemctl restart nginx.service
5>. node4节点配置域名解析,并测试:
[root@node4 ~]# more /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.188.11 xian.yunjisuan.com chengdu.yunjisuan.com
[root@node4 ~]# for ((i=1;i<=10;i++)); do curl xian.yunjisuan.com; done
192.168.188.12 xian.yunjisuan.com
192.168.188.13 xian.yunjisuan.com
192.168.188.12 xian.yunjisuan.com
192.168.188.13 xian.yunjisuan.com
192.168.188.12 xian.yunjisuan.com
192.168.188.13 xian.yunjisuan.com
192.168.188.12 xian.yunjisuan.com
192.168.188.13 xian.yunjisuan.com
192.168.188.12 xian.yunjisuan.com
192.168.188.13 xian.yunjisuan.com
[root@node4 ~]# for ((i=1;i<=10;i++)); do curl chengdu.yunjisuan.com; done
192.168.188.12 chengdu.yunjisuan.com
192.168.188.13 chengdu.yunjisuan.com
192.168.188.12 chengdu.yunjisuan.com
192.168.188.13 chengdu.yunjisuan.com
192.168.188.12 chengdu.yunjisuan.com
192.168.188.13 chengdu.yunjisuan.com
192.168.188.12 chengdu.yunjisuan.com
192.168.188.13 chengdu.yunjisuan.com
192.168.188.12 chengdu.yunjisuan.com
192.168.188.13 chengdu.yunjisuan.com
4. Nginx根据URL中的目录地址实现代理转发:
1>. 在node1节点配置Nginx负载均衡,这里使用子配置文件方式配置:
[root@node1 ~]# vim /etc/nginx/conf.d/nginxd.conf
upstream staticPools { ##staticPools为静态服务器池
server 192.168.188.12:80;
}
upstream updatePools { ##updatePools为动态服务器池
server 192.168.188.13:80;
}
upstream defaultPools { ##defaultPools为默认服务器池
server 192.168.188.14:80;
}
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
##将符合static的请求交给上传服务器池staticPools
##即将访问http://192.168.188.11/static/的请求转到192.168.188.12/static/上去
location /static/ {
proxy_pass http://staticPools;
proxy_set_header X-Forwarded-For $remote_addr;
}
##将符合upload的请求交给上传服务器池uploadPools
##即将访问http://192.168.188.11/update/的请求转到192.168.188.13/update/上去
location /update/ {
proxy_pass http://updatePools;
proxy_set_header X-Forwarded-For $remote_addr;
}
##不符合上述规则的请求,默认全部交给动态服务器池defaultPools
##即将不符合上述规则的请求转到192.168.188.14/上去
location / {
proxy_pass http://defaultPools;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
##重启Nginx:
[root@node1 ~]# systemctl restart nginx.service
2>. 配置后端的页面:
##node2节点配置:
[root@node2 ~]# cd /usr/share/nginx/html/
[root@node2 ~]# mkdir static
[root@node2 ~]# vim static/index.html
static_pools
##node3节点配置:
[root@node3 ~]# cd /usr/share/nginx/html/
[root@node3 ~]# mkdir update
[root@node3 ~]# vim update/index.html
update_pools
##node4节点配置:
[root@node4 ~]# yum install httpd
[root@node4 ~]# echo "default_pools" > /var/www/html/index.html
[root@node4 ~]# systemctl start httpd
3>. 配置windows的域名解析:
在" C:\Windows\System32\drivers\etc\hosts "中添加域名解析"192.168.188.11 xian.yunjisuan.com"。
4>. 通过浏览器访问:



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

3249

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



