国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

使用Nginx作緩存服務(wù)器以及刪除其緩存文件的方法

2024-08-30 12:27:47
字體:
供稿:網(wǎng)友

這篇文章主要介紹了使用Nginx作緩存服務(wù)器以及刪除其緩存文件的方法,作cache時(shí)需要注意一下磁盤的IO瓶頸,需要的朋友可以參考下

使用nginx做cache服務(wù)器

需求就是緩存android的軟件包,后綴名是apk。話不多說,直接上配置,供參考:

 

 
  1. a-->nginx.conf 
  2. user www www; 
  3. worker_processes 8; 
  4. error_log /data/logs/nginx_error.log crit; 
  5. pid /usr/local/nginx/nginx.pid; 
  6. worker_rlimit_nofile 204800; 
  7.  
  8. events 
  9. use epoll; 
  10. worker_connections 204800; 
  11.  
  12. http 
  13. include mime.types; 
  14. #apk 文件類型 
  15. #default_type application/vnd.android.package-archive; 
  16. default_type application/octet-stream; 
  17.  
  18. charset utf-8; 
  19.  
  20. server_names_hash_bucket_size 128; 
  21. client_header_buffer_size 2k; 
  22. large_client_header_buffers 4 4k; 
  23. client_max_body_size 8m; 
  24.  
  25. sendfile on; 
  26. tcp_nopush on; 
  27.  
  28. keepalive_timeout 60; 
  29. open_file_cache max=204800 inactive=20s; 
  30. open_file_cache_min_uses 1; 
  31. open_file_cache_valid 30s; 
  32.  
  33.  
  34.  
  35. tcp_nodelay on; 
  36. client_body_buffer_size 512k; 
  37.  
  38. #跟后端服務(wù)器連接的超時(shí)時(shí)間_發(fā)起握手等候響應(yīng)超時(shí)時(shí)間 
  39. proxy_connect_timeout 600; 
  40. #連接成功后_等候后端服務(wù)器響應(yīng)的時(shí)間_其實(shí)已經(jīng)進(jìn)入后端的排隊(duì)之中等候處理 
  41. proxy_read_timeout 600; 
  42. #后端服務(wù)器數(shù)據(jù)回傳時(shí)間_就是在規(guī)定時(shí)間內(nèi)后端服務(wù)器必須傳完所有數(shù)據(jù) 
  43. proxy_send_timeout 600; 
  44. #代理請(qǐng)求緩存區(qū)_這個(gè)緩存區(qū)間會(huì)保存用戶的頭信息以供Nginx進(jìn)行規(guī)則處理_一般只要能保存下頭信息即可 
  45. proxy_buffer_size 16k; 
  46. #同上 告訴Nginx保存單個(gè)用的幾個(gè)Buffer最大用多大空間 
  47. proxy_buffers 4 64k; 
  48. #如果系統(tǒng)很忙的時(shí)候可以申請(qǐng)更大的proxy_buffers 官方推薦*2 
  49. proxy_busy_buffers_size 128k; 
  50. #proxy緩存臨時(shí)文件的大小 
  51. proxy_temp_file_write_size 128k; 
  52.  
  53. gzip on; 
  54. gzip_proxied expired no-cache no-store private auth; 
  55. gzip_min_length 1k; 
  56. gzip_buffers 4 16k; 
  57. gzip_http_version 1.1; 
  58. gzip_comp_level 3; 
  59. gzip_types text/plain application/x-javascript text/css application/xml; 
  60. gzip_disable "MSIE [1-6]/."
  61. gzip_vary on; 
  62.  
  63. #log_format access '$remote_addr - $remote_user [$time_local] ' 
  64. # '"$request" $status $body_bytes_sent ' 
  65. # '"$http_referer" "$http_user_agent" ' 
  66. # '$host $request_time $http_x_forwarded_for'; 
  67. #access_log /data/logs/http.a.log; 
  68. #error_log /data/logs/http.e.log; 
  69.  
  70. include vhosts/cache.peiqiang.net.conf; 
  71.  
  72. upstream source_site { 
  73. server 192.168.1.1:80 weight=7 max_fails=2 fail_timeout=30s; 
  74. server 192.168.1.2:80 weight=4 max_fails=2 fail_timeout=30s; 
  75.  
  76. b-->cache.peiqiang.net.conf 
  77. #用于指定本地目錄來緩沖較大的代理請(qǐng)求 
  78. proxy_temp_path /data/soft/temp; 
  79. #設(shè)置web緩存區(qū)名為cache_one,內(nèi)存緩存空間大小為12000M,自動(dòng)清除超過15天沒有被訪問過的緩存數(shù)據(jù),硬盤緩存空間大小200g 
  80. proxy_cache_path /data/soft/cache levels=1:2 keys_zone=cache_one:12000m inactive=15d max_size=200g; 
  81.  
  82. server { 
  83. listen 80; 
  84. server_name cache.peiqiang.net; 
  85. access_log /data/logs/a.log; 
  86. error_log /data/logs/e.log notice; 
  87.  
  88. # PHP Scripts is NOT allowed within this site! 
  89. location ~* /.(php|php5|jsp|asp|aspx)$ { 
  90. deny all; 
  91.  
  92.  
  93. location / { 
  94.  
  95. proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; 
  96. proxy_cache cache_one; 
  97. proxy_cache_valid 200 304 12h; 
  98. proxy_cache_key $uri$is_args$args; 
  99.  
  100. #反向代理,訪問后端內(nèi)容源服務(wù)器 
  101. proxy_set_header Host $host; 
  102. proxy_set_header X-Forwarded-For $remote_addr; 
  103. proxy_pass http://source_site; 
  104.  
  105. location ~* .*/.(apk)$ { 
  106. error_page 302 404 = @fallback; 
  107.  
  108. #如果后端的服務(wù)器返回500、502、503、504執(zhí)行超時(shí)等錯(cuò)誤、自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)故障轉(zhuǎn)移 
  109. proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; 
  110.  
  111. #使用web緩存區(qū)cache_one 
  112. proxy_cache cache_one; 
  113.  
  114. #對(duì)不同的HTTP狀態(tài)碼緩存設(shè)置不同的緩存時(shí)間 
  115. proxy_cache_valid 200 304 12h; 
  116.  
  117. #設(shè)置Web緩存的Key值,Nginx根據(jù)Key值md5哈希存儲(chǔ)緩存,這里根據(jù)"域名、URI、參數(shù)"組合成key 
  118. proxy_cache_key $uri$is_args$args; 
  119.  
  120.  
  121. #反向代理,訪問后端內(nèi)容源服務(wù)器 
  122. proxy_set_header Host $host; 
  123. proxy_set_header X-Forwarded-For $remote_addr; 
  124. proxy_pass http://source_site; 
  125. expires 1d; 
  126.  
  127. location @fallback { 
  128. rewrite ^ $scheme://apke.peiqiang.net$uri redirect; 
  129. expires -1; 

說明:其實(shí)按這個(gè)配置location /這個(gè)匹配是多余的,因?yàn)檫^來一個(gè)后綴名為apk的軟件包location ~* .*/.(apk)$已經(jīng)給匹配上了,不會(huì)再到location /了,不過由于我們還會(huì)緩存些其他后綴名的文件,所以location /就是必須的。

 

 
  1. c-->/etc/rc.local 
  2. #!/bin/sh 
  3. # 
  4. # This script will be executed *after* all the other init scripts. 
  5. # You can put your own initialization stuff in here if you don't 
  6. # want to do the full Sys V style init stuff. 
  7.  
  8. touch /var/lock/subsys/local 
  9. ulimit -HSn 65535 
  10. /usr/local/nginx/sbin/nginx 

刪除nginx緩存文件

一:腳本

a shell版

 

 
  1. #!/bin/bash 
  2.  
  3. #Date: 2013-06-27 
  4. #Auther: budong 
  5.  
  6. ####################################################### 
  7. #說明: 
  8. # 1.本腳本用于清除nginx緩存文件 
  9. # 2.查看你的nginx是根據(jù)什么作為key來hash的,我的設(shè)置是 proxy_cache_key $uri$is_args$args; 
  10. # 因此nginx會(huì)根據(jù)$uri$is_args$args作為key進(jìn)行hash,因此可以模擬nginx對(duì)一個(gè)key進(jìn)行再 
  11. # hash找到相應(yīng)的文件路徑,刪除(具體可隨意找個(gè)緩存文件 more 一下看看) 
  12. # 3.緩存設(shè)置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g; 
  13. # 根據(jù)相應(yīng)的配置,請(qǐng)做相應(yīng)修改測試 
  14. # 4.uri格式請(qǐng)按照同級(jí)目錄下rm_apk_list.txt中填寫 
  15. ##################################################### 
  16.  
  17. while read -r line 
  18. do 
  19. md5uri=`echo -n $line | md5sum | awk '{ print $1 }'
  20. filepath=`echo "$md5uri" | awk '{print "/data/mumayi/cache/"substr($0,length($0),1)"/"substr($0,length($0)-2,2)"/"$0}'
  21. rm -rf $filepath 
  22. done < /root/sbin/rm_apk_list.txt 
  23. b python版 
  24.  
  25. #!/usr/local/python2.7/bin/python2.7 
  26. # -*- coding:utf8 -*- 
  27.  
  28. #Date: 2013-06-26 
  29. #Name: budong 
  30.  
  31. ''
  32. 說明: 
  33. 1.本腳本用于清除nginx緩存文件 
  34. 2.查看你的nginx是根據(jù)什么作為key來hash的,我的設(shè)置是 proxy_cache_key $uri$is_args$args; 
  35. 因此nginx會(huì)根據(jù)$uri$is_args$args作為key進(jìn)行hash,因此可以模擬nginx對(duì)一個(gè)key進(jìn)行再 
  36. hash找到相應(yīng)的文件路徑,刪除(具體可隨意找個(gè)緩存文件 more 一下看看) 
  37. 3.緩存設(shè)置 proxy_cache_path /data/mumayi/cache levels=1:2 keys_zone=cache_one:6000m inactive=15d max_size=200g; 
  38. 根據(jù)相應(yīng)的配置,請(qǐng)做相應(yīng)修改測試 
  39. 4.uri格式請(qǐng)按照同級(jí)目錄下rm_apk_list.txt中填寫 
  40. ''
  41. import os 
  42. import sys 
  43. try
  44. from hashlib import md5 
  45. except: 
  46. from md5 import md5 
  47.  
  48. reload( sys ) 
  49. sys.setdefaultencoding('utf-8'
  50.  
  51. PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) 
  52. URI_FILE = ''.join([PROJECT_ROOT,'/rm_apk_list.txt']) 
  53.  
  54. def nginx_purge(uri): 
  55. m = md5() 
  56. m.update("%s" % uri) 
  57. md5uri=m.hexdigest() 
  58. md5uri_len=len(md5uri) 
  59. dir1=md5uri[md5uri_len-1:] 
  60. dir2=md5uri[md5uri_len-3:md5uri_len-1] 
  61. file_path=("/data/mumayi/cache/%s/%s/%s" % (dir1, dir2, md5uri)) 
  62. if os.path.exists(file_path): 
  63. os.remove(file_path) 
  64.  
  65. with open("%s" % URI_FILE,'r') as uri_file: 
  66. for line in uri_file: 
  67. line = line.rstrip() 
  68. nginx_purge(line) 

c ngx_cache_purge不做考慮,據(jù)說已經(jīng)停止開發(fā)了

說明:

1 我的 /root/sbin/rm_apk_list.txt 文件

 

  
  1. [root@budong ~]# cat /root/sbin/rm_apk_list.txt  
  2. /2013/08/15/38/382272/shuazanzhushou_V1.8.16_mumayi_95a91.apk 

2 查看一個(gè)緩存對(duì)象,應(yīng)該有些明白了吧

 

 
  1. [root@budong ~]# more /data/mumayi/cache/0/00/db9327b60a6b3c164516117f90d9d000  

?

  1. KEY: /2013/10/23/43/432816/dinuochongwuDinoPets_V1.1.1_mumayi_0b399.apk 
  2. HTTP/1.1 200 OK 
  3. Server: nginx/1.2.6 
  4. Date: Sun, 15 Dec 2013 19:51:22 GMT 
  5. Content-Type: application/vnd.android.package-archive 
  6. Content-Length: 37466293 
  7. Connection: close 
  8. Last-Modified: Wed, 23 Oct 2013 06:15:06 GMT 
  9. Expires: Wed, 18 Dec 2013 17:35:07 GMT 
  10. Cache-Control: max-age=604800 
  11. Accept-Ranges: bytes 

1

2

3

4

5

6

7

8

9

10

11

 

 



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五华县| 保靖县| 金坛市| 科技| 武功县| 乐清市| 长宁县| 沾化县| 琼结县| 密山市| 大同市| 阳谷县| 凤山市| 顺昌县| 建昌县| 石景山区| 磐石市| 读书| 阿尔山市| 富平县| 绍兴县| 盐池县| 余姚市| 南溪县| 垫江县| 白银市| 大余县| 会同县| 阿坝县| 波密县| 沈丘县| 鹿邑县| 寿宁县| 永康市| 灵武市| 博白县| 永德县| 宜兴市| 独山县| 澳门| 泗水县|