nginx cache 使用
1.编译安装
安装cache_purge模块,可以参考我写的:http://bbs.linuxtone.org/thread-6875-1-1.html
nginx的server配置:
- server
- {
- listen 80;
- server_name 54yancheng.com www.54yancheng.com;
- #purge cache files
- location ~ /purge(/.*)
- {
- #设置只允许指定的IP或IP段才可以清除URL缓存,加入你的squid服务器的ip地址。
- allow 114.244.0.0/16;
- allow 192.168.0.0/16;
- deny all;
- proxy_cache_purge cache_one $host$1$is_args$args;
- error_page 405 =200 /purge$1; #处理squidclient purge的时候出现的405错误
- }
- #用if判断$request_method是PURGE时,转到/purge/这个location里去处理
- if ( $request_method = "PURGE" ) {
- rewrite ^(.*)$ /purge$1 last;
- }
- #pass files
- location /
- {
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- proxy_pass http://54yancheng;
- }
- #cache files
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
- {
- add_header X-Cache HIT_from_www.54yancheng.com;
- proxy_cache cache_one;
- proxy_cache_valid 200 304 12h;
- proxy_cache_valid 301 302 1m;
- proxy_cache_valid any 1m;
- proxy_cache_key $host$uri$is_args$args;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- proxy_pass http://54yancheng;
- expires 365d;
- access_log off;
- }
- #web log
- #access_log off;
- access_log /data/logs/access_www.54yancheng.com.log combined;
- }
2.squidclient清除示例:
- # squidclient -h 192.168.0.111 -p 80 -m PURGE -v http://www.54yancheng.com/ads/6739984.gif
- headers: ‘PURGE http://www.54yancheng.com/ads/6739984.gif HTTP/1.0
- Accept: */*
- ‘
- HTTP/1.1 200 OK
- Server: Nginx
- Date: Mon, 13 Sep 2010 03:05:02 GMT
- Content-Type: text/html
- Content-Length: 290
- Connection: close
- <html>
- <head><title>Successful purge</title></head>
- <body bgcolor="white">
- <center><h1>Successful purge</h1>
- <br>Key : www.54yancheng.com/ads/6739984.gif
- <br>Path: /data/cache/nginx_cache/3/6d/e70fbe8e578b27440e8f2e60269966d3
- </center>
- <hr><center>Apache</center>
- </body>
- </html>
3.脚本清查多台同配置的nginx cache,并输出状态
在实际生产中,业务用多台nginx的cache做集群实现负载均衡,这个脚本就是实现清除多nginx服务器cahce的功能。
- #!/bin/sh
- #利用squidclient的PURGE清除多nginx服务器cache脚本
- #bbs:www.linuxtone.org
- #by:hamgua
- echo "nginx purge $1"
- echo "----------------------------"
- for i in {1..3};do
- echo "host:192.168.0.$i"
- echo "status:`/usr/local/squid2/bin/squidclient -h 192.168.0.$i -p 80 -m PURGE -v $1 2>&1 |grep "^<center>"|awk -F‘[><]‘ ‘{print $5}‘`"
- echo "----------------------------"
- done
脚本使用示例:
- # ./purge.sh http://www.54yancheng.com/ads/6739984.gif
- nginx purge http://www.54yancheng.com/ads/6739984.gif
- ----------------------------
- host:192.168.0.1
- status:Successful purge
- ----------------------------
- host:192.168.0.2
- status:404 Not Found
- ----------------------------
- host:192.168.0.3
- status:404 Not Found
- ----------------------------
参考文章:
安装配置参考我写的:http://bbs.linuxtone.org/thread-6875-1-1.html
squidclient清除nginx cache,参考sudone的文章:http://sudone.com/nginx/nginx_cache_purge.html
linuxtone网站服务器nginx版块:http://bbs.linuxtone.org/forumdisplay.php?fid=22&filter=type&typeid=1
张宴 使用Nginx的proxy_cache缓存功能取代Squid[原创] http://blog.s135.com/nginx_cache/
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。