前言* 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/ 

如下图:
https://img1.51cto.com/group_p_w_upload/201112131323751485261.png