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/

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