nginx反向代理、动静分离
环境:根据http://www.cnblogs.com/zzzhfo/p/6032095.html配置
方法一:根据目录实现动静分离
在web01创建image并上传一张图片作为静态页面
[root@web01 /]# cd /var/www/www/
[root@web01 www]# pwd
/var/www/www
[root@web01 www]# mkdir image
[root@web01 www]# ls
image index.html
[root@web01 www]# cd image/
[root@web01 image]# yum -y install lrzsz #用于文件上传工具
[root@web01 image]# ls
nginx.jpg #上传一张图片
测试web01请求页面是否能正常访问

在web02创建一个dynamic测试页作为动态请求页面
把web01上的image的内容同步到web02上
[root@web02 /]# cd /var/www/www/
[root@web02 www]# pwd
/var/www/www
[root@web02 www]# mkdir dynamic
[root@web02 www]# ls
dynamic index.html
[root@web02 www]# echo dynamic > dynamic/index.html
测试


修改LB的配置文件 /usr/local/nginx/conf/nginx.conf
upstream static_pools {
server 192.168.119.130:;
}
upstream dynamic_pools {
server 192.168.119.133:;
}
server {
listen ;
server_name www.test.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://dynamic_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /image {
proxy_pass http://static_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
[root@lb01 /]# nginx -s reload
在客户端测试
修改客户机/etc/hosts
C:\Windows\System32\drivers\etc\hosts
192.168.119.128 www.test.com
访问
静态页面

动态页面

查看web01和web02的访问日志
web01
[root@web01 /]# tail -f /etc/httpd/logs/www.test.com.
www.test.com.access_log www.test.com.access_log- www.test.com.error_log
[root@web01 /]# tail -f /etc/httpd/logs/www.test.com.access_log
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /image/nginx.jpg HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0
web02
[root@web02 www]# tail -f /etc/httpd/logs/www.test.com.access_log
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic/ HTTP/1.0" - "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
"192.168.119.136" - - [/Oct/::: +] "GET /dynamic HTTP/1.0" "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
把web01 httpd服务关闭
在访问http://www.test.com/image/nginx.jpg

方法二:根据扩展名实现动静分离
由于测试环境没有做lnmp所以不适用这个代码 使用下面的代码
upstream static_pools {
server 192.168.119.130:;
}
upstream dynamic_pools {
server 192.168.119.133:;
}
server {
listen ;
server_name www.test.com;
location / {
root html;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
proxy_pass http://static_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*.(php|php3|php5)$ {
proxy_pass http://dynamic_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
使用正则匹配(这里使用这个)
upstream static_pools {
server 192.168.119.130:;
}
upstream dynamic_pools {
server 192.168.119.133:;
}
server {
listen ;
server_name www.test.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://dynamic_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
proxy_pass http://static_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
测试


把web01 httpd服务关闭
在访问http://www.test.com/image/nginx.jpg

nginx反向代理、动静分离的更多相关文章
- linux+apache+nginx实现,反向代理动静分离
在我们开发的过程中,一定会遇到,负载均衡方面的问题.下面我们,做一个小例子:使用nginx+apache实现反向代理,动静分离. 这里apache.php.nginx的安装就不做赘述了,不懂的朋友可以 ...
- [nginx] - 使用nginx实现反向代理,动静分离,负载均衡,session共享
反向代理概念 先说正向代理,比如要访问youtube,但是不能直接访问,只能先找个FQ软件,通过FQ软件才能访问youtube. FQ软件就叫做正向代理.所谓的反向代理,指的是用户要访问youtube ...
- 15 nginx反向代理实现nginx+apache动静分离
一:nginx反向代理实现nginx+apache动静分离-------------概念--------------------------- nginx反向代理服务器+负载均衡 用nginx做反向代 ...
- Nginx 反向代理功能-动静分离
Nginx 反向代理功能-动静分离 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...
- Nginx反向代理、负载均衡、页面缓存、URL重写及读写分离详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- Nginx 反向代理、负载均衡、页面缓存、URL重写、读写分离及简单双机热备详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx (windows下nginx安装.配置与使用) 四.Nginx之反向代理 五.Nginx之负载均衡 (负载均衡算法:nginx负载算法 up ...
- Nginx反向代理 负载均衡 页面缓存 URL重写及读写分离
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- [转载]Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- nginx 反向代理,支持跨域,前后分离
前端开发往往涉及到跨域问题,其中解决方案很多: 1.jsonp 需要目标服务器配合一个callback函数. 2.window.name+iframe 需要目标服务器响应window.name. 3. ...
随机推荐
- hdu 2005 - 第几天?
题意:判断是否为闰年 解法:这题需要注意一下用scanf能直接读入year,month,day 附上代码: 1: #include<stdlib.h> 2: #include<str ...
- mainBundle和CustomBundle
iOS中NSBundle类 An NSBundle object represents a location in the file system that groups code and resou ...
- 刨根问底Objective-C Runtime
http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...
- FIREFOX A tool for easily making HTTP requests (GET/PUT/POST/DELETE)
https://addons.mozilla.org/zh-CN/firefox/addon/httprequester/
- hibernate实现有两种配置,xml配置与注释配置。
(1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) <?xml version='1.0' encoding= ...
- JavaWeb学习笔记——XML简介
- NGUI架构和Draw Call合并原理
http://bbs.9ria.com/thread-282804-1-1.html http://www.unitymanual.com/blog-97-238.html
- yourphp点击刷新验证码
加入css <script type="text/javascript" src="./Public/Js/my.js"></script&g ...
- python 二分法查找实例(递归、循环)
二分法,主要应用于有序序列中,原理是每次查找都将原序列折半,逐渐缩小查找范围的一种算法. 需求 要求在一个有序序列中,例如[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 ...
- Robot Framework--12 RFS+AutoItLibrary测试web对话框
转自:http://blog.csdn.net/tulituqi/article/details/21871247 Selenium2library在我们实际测试web页面的时候基本上已经够用了,不过 ...