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 | node04 | 192.168.5.13 | httpd-2.4.6 | Centos 7.8 |
nginx client | windows 7 Ultimate. | 192.168.5.7 | ---- | windows 7 Ultimate. |
配置后端 web服务
---node02
[root@node02 ~]# mkdir /usr/share/nginx/html/static/
[root@node02 ~]# ll /usr/share/nginx/html/static/
total 448
-rw-r--r-- 1 root root 365133 Mar 4 15:07 宇宙.png
-rw-r--r-- 1 root root 59378 Mar 4 15:07 松鼠.gif
-rw-r--r-- 1 root root 23022 Mar 4 15:07 美女.jpg
[root@node02 ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name static.wan.com;
location / {
root /usr/share/nginx/html/static;
index index.html index.htm;
}
}
[root@node02 ~]# systemctl restart nginx
---node03
[root@node03 ~]# mkdir /var/www/html/upload/
[root@node03 ~]# vim /var/www/html/upload/hello.php
<?php
echo 'hello world'
?>
[root@node03 ~]# vim /var/www/html/upload/phpinfo.php
<?php
phpinfo();
?>
[root@node03 ~]# systemctl restart httpd
配置proxy host
[root@node01 ~]# vim /etc/nginx/conf.d/vhost.conf
upstream static_pools {
server 192.168.5.12 weight=1;
}
upstream upload-pools {
server 192.168.5.13 weight=1;
}
upstream default_pools {
server 192.168.5.14 weight=1;
}
server {
listen 80;
server_name web.wan.org;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
proxy_pass http://static_pools;
}
location ~ .*\.(php|php3|php5|python)$ {
proxy_pass http://upload-pools;
}
}
[root@node01 ~]# systemctl restart nginx
windows 7 Ultimate. 测试
浏览器测试:http://web.wan.org/
添加hosts解析
静态站点
浏览器访问:http://web.wan.org/宇宙.png
浏览器访问:http://web.wan.org/松鼠.gif
浏览器访问:http://web.wan.org/美女.jpg
动态站点
浏览器访问:http://web.wan.org/phpinfo.php
浏览器访问:http://web.wan.org/hello.php