Nginx+Proxy_cache高速緩存配置

http://wgkgood.blog.51cto.com/1192594/773278/


前言* 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. wget http://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz  

  4. tar zxvf ngx_cache_purge-1.4.tar.gz  

  5. wget http://nginx.org/download/nginx-1.0.11.tar.gz  

  6. tar zxvf nginx-1.0.11.tar.gz  

  7. cd nginx-1.0.11/  

  8. ./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  

  9. make && make install  

  10. cd ../

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

  1. user www www;  

  2. worker_processes 8;  

  3. error_log /data/logs/nginx/error.log crit;  

  4. pid /usr/local/nginx/nginx.pid;  

  5. #Specifies the value for maximum file descriptors that can be opened by this process.  

  6. worker_rlimit_nofile 65535;  

  7. events  

  8. {  

  9. use epoll;  

  10. worker_connections 65535;  

  11. }  

  12. http  

  13. {  

  14. include mime.types;  

  15. default_type application/octet-stream;  

  16. charset utf-8;  

  17. server_names_hash_bucket_size 128;  

  18. client_header_buffer_size 32k;  

  19. large_client_header_buffers 4 32k;  

  20. client_max_body_size 300m;  

  21. sendfile on;  

  22. tcp_nopush on;  

  23. keepalive_timeout 60;  

  24. tcp_nodelay on;  

  25. client_body_buffer_size 512k;  

  26. proxy_connect_timeout 5;  

  27. proxy_read_timeout 60;  

  28. proxy_send_timeout 5;  

  29. proxy_buffer_size 16k;  

  30. proxy_buffers 4 64k;  

  31. proxy_busy_buffers_size 128k;  

  32. proxy_temp_file_write_size 128k;  

  33. gzip on;  

  34. gzip_min_length 1k;  

  35. gzip_buffers 4 16k;  

  36. gzip_http_version 1.1;  

  37. gzip_comp_level 2;  

  38. gzip_types text/plain application/x-javascript text/css application/xml;  

  39. gzip_vary on;  

  40. proxy_temp_path /data/proxy_temp_dir;  

  41. #設置Web緩存區名稱爲cache_one,內存緩存空間大小爲200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲30GB。  

  42. proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1dmax_size=30g;  

  43. upstream backend_server {  

  44. server 192.168.5.130:8080 weight=1max_fails=2fail_timeout=30s;  

  45. server 192.168.5.131:8080 weight=1max_fails=2fail_timeout=30s;  

  46. }  

  47. server  

  48. {  

  49. listen 80;  

  50. server_name www.abc.com 192.168.5.133;  

  51. index index.html index.htm;  

  52. root /data/webapps/www;  

  53. location /  

  54. {  

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

  56. proxy_next_upstream http_502 http_504 error timeout invalid_header;  

  57. proxy_cache cache_one;  

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

  59. proxy_cache_valid 200 304 12h;  

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

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

  62. proxy_set_header Host $host;  

  63. proxy_set_header X-Forwarded-For $remote_addr;  

  64. proxy_pass http://backend_server;  

  65. expires 1d;  

  66. }  

  67. location ~ /purge(/.*)  

  68. {  

  69. #設置只允許指定的IP或IP段輸入正確的密碼纔可以清除URL緩存。(Nginx限制目錄訪問,可以用apache

  70. 的htpasswd命令生成密鑰文件,如:htpasswd -c /usr/local/nginx/conf/htpasswd adm_cache ,提示輸入密碼即可。)  

  71. auth_basic “Please Insert User And Password”;  

  72. auth_basic_user_file /usr/local/nginx/conf/htpasswd;  

  73. allow 127.0.0.1;  

  74. allow 192.168.1.0/24;  

  75. deny all;  

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

  77. }  

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

  79. {  

  80. proxy_set_header Host $host;  

  81. proxy_set_header X-Forwarded-For $remote_addr;  

  82. proxy_pass http://backend_server;  

  83. }  

  84. log_format access ‘$remote_addr – $remote_user[$time_local] “$request” ‘  

  85. ‘$status $body_bytes_sent “$http_referer” ‘  

  86. ‘”$http_user_agent” $http_x_forwarded_for’;  

  87. access_log /data/logs/nginx/access.log access;  

  88.  }  

  89. }

三、 啓動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


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