Nginx 反向代理 -- 案例实战
- 前言
前言
本环境是基于 Centos 7.8 系统构建Nginx学习环境
具体构建,请参考 Nginx-1.18.0 环境部署
环境准备
role | host | ip | nginx-version | OS |
nginx proxy host | node01 | 192.168.5.11 | Nginx-1.18.0 | Centos 7.8 |
nginx web server1 | node02 | 192.168.5.12 | Nginx-1.18.0 | Centos 7.8 |
nginx web server2 | node02 | 192.168.5.13 | Nginx-1.18.0 | Centos 7.8 |
nginx client1 | node04 | 192.168.5.14 | ---- | Centos 7.8 |
nginx client2 | windows 7 Ultimate. | 192.168.5.7 | ---- | windows 7 Ultimate. |
配置后端 web服务
---node02
[root@node02 ~]# echo "`hostname -I`www" > /usr/share/nginx/html/www/index.html
[root@node02 ~]# echo "`hostname -I`bbs" > /usr/share/nginx/html/bbs/index.html
[root@node02 ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
[root@node02 ~]# systemctl restart nginx
---node03
[root@node03 ~]# mkdir /usr/share/nginx/html/{www,bbs}
[root@node03 ~]# echo "`hostname -I`www" > /usr/share/nginx/html/www/index.html
[root@node03 ~]# echo "`hostname -I`bbs" > /usr/share/nginx/html/bbs/index.html
[root@node03 ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
[root@node03 ~]# systemctl restart nginx
配置反向代理服务器
添加hosts解析
[root@node01 ~]# vim /etc/hosts
192.168.5.11 web.wan.org
192.168.5.12 www.yunjisuan.com bbs.yunjisuan.com
192.168.5.13 www.yunjisuan.com bbs.yunjisuan.com
[root@node01 ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name web.wan.org;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /bbs {
proxy_pass http://192.168.5.12/bbs/;
}
location /www {
proxy_pass http://192.168.5.12/www/;
}
}
[root@node01 ~]# systemctl restart nginx
node04测试
添加hosts解析
[root@node04 ~]# vim /etc/hosts
192.168.5.11 web.wan.org
192.168.5.12 www.yunjisuan.com bbs.yunjisuan.com
192.168.5.13 www.yunjisuan.com bbs.yunjisuan.com
windows 7 Ultimate. 测试添加hosts解析
浏览器访问:http://web.wan.org/bbs/
再次刷新
浏览器访问:http://web.wan.org/www/
再次刷新
手动测试后端web服务