Nginx+proxy_cache高速緩存配置

 前言* Nginx已經具備Squid所擁有的Web緩存加速功能、清除指定URL緩存的功能。而在性能上,Nginx對多核CPU的利用,勝過Squid不少。另外,在反向代理、負載均衡、健康檢查、後端服務器故障轉移、Rewrite重寫、易用性上,Nginx也比Squid強大得多。這使得一臺Nginx可以同時作爲“負載均衡服務器”與“Web緩存服務器”來使用。


一、 安裝nginx和ngx-purge

  1. ulimit -SHn 65535  
  2. yum install pcre pcre-devel -y 安裝pcre  
  3.  
  4. wget http://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz   
  5. tar zxvf ngx_cache_purge-1.4.tar.gz  
  6. wget http://nginx.org/download/nginx-1.0.11.tar.gz   
  7. tar zxvf nginx-1.0.11.tar.gz  
  8. cd nginx-1.0.11/  
  9. ./configure --user=www --group=www --add-module=../ngx_cache_purge-1.4 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  
  10. make && make install  
  11. cd ../

二、 配置nginx.conf文件如下配置文件

  1. user www www;  
  2.  
  3. worker_processes 8;  
  4.  
  5. error_log /data/logs/nginx/error.log crit;  
  6.  
  7. pid /usr/local/nginx/nginx.pid;  
  8.  
  9. #Specifies the value for maximum file descriptors that can be opened by this process.  
  10. worker_rlimit_nofile 65535;  
  11.  
  12. events  
  13. {  
  14. use epoll;  
  15. worker_connections 65535;  
  16. }  
  17.  
  18. http  
  19. {  
  20. include mime.types;  
  21. default_type application/octet-stream;  
  22.  
  23. charset utf-8;  
  24.  
  25. server_names_hash_bucket_size 128;  
  26. client_header_buffer_size 32k;  
  27. large_client_header_buffers 4 32k;  
  28. client_max_body_size 300m;  
  29.  
  30. sendfile on;  
  31. tcp_nopush on;  
  32.  
  33. keepalive_timeout 60;  
  34.  
  35. tcp_nodelay on;  
  36.  
  37. client_body_buffer_size 512k;  
  38. proxy_connect_timeout 5;  
  39. proxy_read_timeout 60;  
  40. proxy_send_timeout 5;  
  41. proxy_buffer_size 16k;  
  42. proxy_buffers 4 64k;  
  43. proxy_busy_buffers_size 128k;  
  44. proxy_temp_file_write_size 128k;  
  45.  
  46. gzip on;  
  47. gzip_min_length 1k;  
  48. gzip_buffers 4 16k;  
  49. gzip_http_version 1.1;  
  50. gzip_comp_level 2;  
  51. gzip_types text/plain application/x-javascript text/css application/xml;  
  52. gzip_vary on;  
  53.  
  54. proxy_temp_path /data/proxy_temp_dir;  
  55. #設置Web緩存區名稱爲cache_one,內存緩存空間大小爲200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲30GB。  
  56. proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;  
  57.  
  58. upstream backend_server {  
  59. server 192.168.5.130:8080 weight=1 max_fails=2 fail_timeout=30s;  
  60. server 192.168.5.131:8080 weight=1 max_fails=2 fail_timeout=30s;  
  61. }  
  62.  
  63. server  
  64. {  
  65. listen 80;  
  66. server_name www.abc.com 192.168.5.133;  
  67. index index.html index.htm;  
  68. root /data/webapps/www;  
  69.  
  70. location /  
  71. {  
  72. #如果後端的服務器返回502、504、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另一臺服務器,實現故障轉移。  
  73. proxy_next_upstream http_502 http_504 error timeout invalid_header;  
  74. proxy_cache cache_one;  
  75. #對不同的HTTP狀態碼設置不同的緩存時間  
  76. proxy_cache_valid 200 304 12h;  
  77. #以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內  
  78. proxy_cache_key $host$uri$is_args$args;  
  79. proxy_set_header Host $host;  
  80. proxy_set_header X-Forwarded-For $remote_addr;  
  81. proxy_pass http://backend_server;  
  82. expires 1d;  
  83. }  
  84.  
  85. location ~ /purge(/.*)  
  86. {  
  87. #設置只允許指定的IP或IP段輸入正確的密碼纔可以清除URL緩存。(Nginx限制目錄訪問,可以用apache
  88. 的htpasswd命令生成密鑰文件,如:htpasswd -c /usr/local/nginx/conf/htpasswd adm_cache ,提示輸入密碼即可。)  
  89. auth_basic “Please Insert User And Password”;  
  90. auth_basic_user_file /usr/local/nginx/conf/htpasswd;  
  91. allow 127.0.0.1;  
  92. allow 192.168.1.0/24;  
  93. deny all;  
  94. proxy_cache_purge cache_one $host$1$is_args$args;  
  95. }  
  96.  
  97. location ~ .*\.(php|jsp|cgi)?$  
  98. {  
  99. proxy_set_header Host $host;  
  100. proxy_set_header X-Forwarded-For $remote_addr;  
  101. proxy_pass http://backend_server;  
  102. }  
  103. log_format access ‘$remote_addr – $remote_user[$time_local] “$request” ‘  
  104. ‘$status $body_bytes_sent “$http_referer” ‘  
  105. ‘”$http_user_agent” $http_x_forwarded_for’;  
  106. access_log /data/logs/nginx/access.log access;  
  107.   }  

三、 啓動nginx即可

  1. /usr/local/nginx/sbin/nginx 即可  
  2. 然後配置好resin端口設置爲8080  
  3. 如果需要刷新緩存的url地址爲:  
  4. http://192.168.5.133/purge/ 

如下圖:
http://img1.51cto.com/group_attachment/201112131323751485261.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章