歡迎您光臨本站 註冊首頁

nginx的cache使用兩例分享

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

nginx的cache使用兩例分享

nginx的cache使用兩例分享


補加:多謝netseek的提醒,加入了cache頭部驗證信息:

01.建議將CAHCE部分添加HEADER信息,方便分析是否真的緩存:

02.add_header X-Cache HIT-LT;
複製代碼添加的內容:

01.add_header      X-Cache   HIT-LinuxTone;
複製代碼



nginx的cache功能可以把URL及相關組合當作Key,用md5編碼哈希后保存在硬碟上,配置靈活,功能好不亞於squid。

下載:

01.wget http://labs.frickle.com/files/ngx_cache_purge-1.1.tar.gz

02.wget http://nginx.org/download/nginx-0.8.44.tar.gz
複製代碼
編譯:
01../configure \

02.    "--user=www" \

03.    "--group=www" \

04.    "--add-module=../ngx_cache_purge-1.1" \

05.    "--prefix=/usr/local/nginx/" \

06.    "--with-http_stub_status_module" \

07.    "--with-http_ssl_module" \

08.    "--with-md5=/usr/lib" \

09.    "--with-sha1=/usr/lib"複製代碼

下面我分享我在生產實用中的兩個實例:
1。門戶cms,靜態頁面為主,採用phpcms做內容發布系統!web為多台,利用nginx做了集群。
1.1負載均衡伺服器:nginx.conf

01.        upstream  www.linuxtone.org  {

02.          #ip_hash;

03.          server   192.168.0.110:80 weight=8 max_fails=2 fail_timeout=30s;

04.          server   192.168.0.111:80 weight=8 max_fails=2 fail_timeout=30s;

05.          server   192.168.0.112:80 weight=8 max_fails=2 fail_timeout=30s;

06.         }
複製代碼1.2nginx的cache伺服器配置(幾台cache伺服器配置都一樣):
nginx.conf

01.       #nginx cache

02.       proxy_temp_path            /data/cache/nginx_temp;

03.       proxy_cache_path  /data/cache/nginx_cache  levels=1:2   keys_zone=cache_one:400m inactive=1d max_size=60g;

04.

05.        upstream real_server {

06.          #cache的源伺服器,生產中後台發布系統是一獨立伺服器不參加web負載

07.           server   192.168.0.113:80;

08.        }複製代碼server.conf01.server

02.       {

03.               listen       80;

04.               server_name  www.linuxtone.org;

05.               root /data/www/wwwroot/phpcms;

06.               index index.html index.htm index.php;

07.

08.               location /

09.                  {

10.                #如果後端的伺服器返回502、504、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另一台伺服器,實現故障轉移。

11.                proxy_next_upstream http_502 http_504 error timeout invalid_header;

12.                proxy_cache cache_one;

13.                #對不同的HTTP狀態碼設置不同的緩存時間

14.                proxy_cache_valid  200 304 12h;

15.                #以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內

16.                proxy_cache_key $host$uri$is_args$args;

17.                proxy_set_header Host  $host;

18.                proxy_set_header X-Forwarded-For  $remote_addr;

19.                proxy_pass http://real_server;

20.

21.                add_header      X-Cache   HIT-LinuxTone;

22.                expires      1d;

23.                  }

24.      

25.               #清除緩存

26.               location ~ /purge(/.*)

27.                  {

28.                   #設置只允許指定的IP或IP段才可以清除URL緩存。

29.                   allow            59.108.67.131;

30.                   allow            192.168.0.0/16;

31.                   deny            all;

32.                   proxy_cache_purge    cache_one   $host$1$is_args$args;

33.                  }   

34.

35.               #擴展名以.php、.jsp、.cgi結尾的動態應用程序不緩存。

36.               location ~ .*\.(php|jsp|cgi)?$

37.                  {

38.                proxy_set_header Host  $host;

39.                proxy_set_header X-Forwarded-For  $remote_addr;

40.                proxy_pass http://real_server;

41.                  }

42.

43.      access_log  off;

44.  }複製代碼

2。discuzx,啟用url偽靜態,cache伺服器跟real_server都是本機器,跑的是不同埠(實用於discuz的其他所有產品)nginx.conf

01.       #nginx cache

02.       proxy_temp_path            /data/cache/nginx_temp;

03.       proxy_cache_path           /data/cache/nginx_cache  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;

04.

05.       upstream real_server {

06.           server    127.0.0.1:81;

07.        }複製代碼cache_server.conf01.server

02.       {

03.               listen       80;

04.               server_name bbs.linuxtone.org;

05.

06.               #purge cache files

07.               location ~ /purge(/.*)

08.               {

09.               #設置只允許指定的IP或IP段才可以清除URL緩存。

10.               allow            59.108.67.131;

11.               allow            192.168.0.0/16;

12.               deny            all;

13.               proxy_cache_purge    cache_one   $host$1$is_args$args;

14.               }

15.

16.              #pass files

17.              location /

18.               {

19.                        proxy_set_header Host  $host;

20.                        proxy_set_header X-Forwarded-For  $remote_addr;

21.                        proxy_pass http://real_server;

22.               }

23.

24.               #cache files

25.               location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$

26.               {

27.                        proxy_cache cache_one;

28.               

29.                        proxy_cache_valid  200 304 12h;

30.                        proxy_cache_valid  301 302 1m;

31.                        proxy_cache_valid  any 1m;

32.               

33.                        proxy_cache_key $host$uri$is_args$args;

34.                        proxy_set_header Host  $host;

35.                        proxy_set_header X-Forwarded-For  $remote_addr;

36.                        proxy_pass http://real_server;

37.

38.                        add_header      X-Cache   HIT-LinuxTone;

39.                        expires      1y;

40.               }

41.

42.               #web log

43.               access_log off;

44.       }複製代碼real_server.conf01.server

02.       {

03.               listen       81;

04.               server_name bbs.linuxtone.org;

05.               index  index.html index.htm index.php;

06.               root  /data/www/wwwroot/bbs.linuxtone.org;

07.               error_page 403 404 =200 /404.html;

08.               error_page 500 502 503 504 =200 /500.html;

09.               #access_log /data/logs/access_bbs.linuxtone.org  combined;

10.

11.               #attachment php file deny

12.               location ~* ^/(static|data)/(image|attachment)/.*\.(php|php5)$

13.               {

14.                    return 404;

15.               }

16.

17.               #php file resolve

18.               location ~ .*\.php?$

19.               {

20.                      include /usr/local/nginx/conf/enable_php5.conf;

21.               }

22.

23.               #discuzx  rewrite

24.               rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;

25.               rewrite ^([^\.]*)/article-(+)-(+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;

26.               rewrite ^([^\.]*)/forum-(\w+)-(+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

27.               rewrite ^([^\.]*)/thread-(+)-(+)-(+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;

28.               rewrite ^([^\.]*)/group-(+)-(+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;

29.               rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;

30.               rewrite ^([^\.]*)/(+)-(.+)\.html$ $1/$2.php?rewrite=$3 last;

31.               #rewrite ^([^\.]*)/a/?$ /b/ last;

32.               if (!-e $request_filename) {

33.                return 404;

34.               }         

35.       }複製代碼

3。cache的驗證
過濾的php文件:

cache的png文件:



以上是我生產中經過測試的兩個站點的nginx_cache使用,希望對大家使用有幫助!

參考:
linuxtone 網站伺服器板塊nginx文檔 http://bbs.linuxtone.org/forum-22-1.html
張宴 使用Nginx的proxy_cache緩存功能取代Squid[原創] http://blog.s135.com/nginx_cache/

[火星人 ] nginx的cache使用兩例分享已經有844次圍觀

http://coctec.com/docs/service/show-post-1635.html