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:80; } upstream dynamic_pools { server 192.168.119.133:80; } server { listen 80; 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-20161007 www.test.com.error_log [root@web01 /]# tail -f /etc/httpd/logs/www.test.com.access_log "192.168.119.136" - - [07/Oct/2016:16:14:37 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:14:37 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:14:37 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:14:37 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:14:38 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:14:38 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:14:38 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:14:38 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:14:38 +0800] "GET /image/nginx.jpg HTTP/1.0" 200 21633 "-" "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" - - [07/Oct/2016:16:17:52 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:53 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:53 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:53 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:54 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:54 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:54 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:54 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:54 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:17:55 +0800] "GET /dynamic/ HTTP/1.0" 304 - "-" "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" - - [07/Oct/2016:16:25:54 +0800] "GET /dynamic HTTP/1.0" 301 314 "-" "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:80; } upstream dynamic_pools { server 192.168.119.133:80; } server { listen 80; 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:80; } upstream dynamic_pools { server 192.168.119.133:80; } server { listen 80; 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