环境:根据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反向代理、动静分离的更多相关文章

  1. linux+apache+nginx实现,反向代理动静分离

    在我们开发的过程中,一定会遇到,负载均衡方面的问题.下面我们,做一个小例子:使用nginx+apache实现反向代理,动静分离. 这里apache.php.nginx的安装就不做赘述了,不懂的朋友可以 ...

  2. [nginx] - 使用nginx实现反向代理,动静分离,负载均衡,session共享

    反向代理概念 先说正向代理,比如要访问youtube,但是不能直接访问,只能先找个FQ软件,通过FQ软件才能访问youtube. FQ软件就叫做正向代理.所谓的反向代理,指的是用户要访问youtube ...

  3. 15 nginx反向代理实现nginx+apache动静分离

    一:nginx反向代理实现nginx+apache动静分离-------------概念--------------------------- nginx反向代理服务器+负载均衡 用nginx做反向代 ...

  4. Nginx 反向代理功能-动静分离

    Nginx 反向代理功能-动静分离 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  5. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...

  6. Nginx反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...

  7. Nginx 反向代理、负载均衡、页面缓存、URL重写、读写分离及简单双机热备详解

    大纲 一.前言 二.环境准备 三.安装与配置Nginx  (windows下nginx安装.配置与使用) 四.Nginx之反向代理 五.Nginx之负载均衡  (负载均衡算法:nginx负载算法 up ...

  8. Nginx反向代理 负载均衡 页面缓存 URL重写及读写分离

    大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...

  9. [转载]Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...

  10. nginx 反向代理,支持跨域,前后分离

    前端开发往往涉及到跨域问题,其中解决方案很多: 1.jsonp 需要目标服务器配合一个callback函数. 2.window.name+iframe 需要目标服务器响应window.name. 3. ...

随机推荐

  1. BZOJ1804: [Ioi2007]Flood 洪水

    把点按坐标排序,每次找出最小的点,一定在最外层,再顺着把最外层的边删掉,经过了两次的边不会被冲毁. 其实不难写,但是写了很久. #include<bits/stdc++.h> #defin ...

  2. TextView 选择文字

    final CharSequence edit = text2.getText(); text2.setCursorVisible(true); text2.setOnTouchListener(ne ...

  3. js014-表单脚本

    js014-表单脚本 本章内容: 理解表单 文本框验证与交互 使用其他表单控制 14.1 表单的基础知识 在HTML中,表单时由<form>元素来表示的,在JS中,表单对应的时HTMLFo ...

  4. IIS------配置.Net 4.0

    转载: http://blog.csdn.net/mazhaojuan/article/details/7660657

  5. JQuery------prevAll(),nextAll(),attr()方法的使用

    $(this).nextAll(".Rec").find("input").attr("checked", false); $(this). ...

  6. mvn filter autoconfig 产生自动配置

    可以使用filter, 也可以使用autoconfig 详细请见 http://www.openwebx.org/docs/autoconfig.html

  7. Python之路【第四篇】:模块

    什么是模块: 模块就是一个功能的集合. 模块就和乐高积木差不多,你用这些模块组合出一个模型,然后也可以用这个模块加上其他的模块组合成一个新的模型 模块的种类: 1.内置模块(python自带的比如os ...

  8. Orchard源码分析(6):Shell相关

    概述在Orchard中,提出子站点(Tenant)的概念,目的是为了增加站点密度,即一个应用程序域可以有多个子站点. Shell是子站点(Tenant)级的单例,换句话说Shell代表了子站点.对比来 ...

  9. jquery报错Uncaught ReferenceError: $ is not defined

  10. XMLHttpRequestUtil

    //XMLHttpRequest类 function XMLHTTPRequestUtil() { this.Instance = null; this.GetXMLHttpRequest = fun ...