将nginx同时作为负载均衡和Web缓存服务器

      Nginx从0.7.48版本开始,支持了类似Squid的缓存功能。这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302这样的非200状态码。虽然目前官方的Nginx Web缓存服务只能为指定URL或状态码设置过期时间,不支持类似Squid的PURGE指令,手动清除指定缓存页面,但是,通过一个第三方的Nginx模块,可以清除指定URL的缓存。

      Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。

     Nginx 0.8.32以后版本,proxy_cache和fastcgi_cache已经比较完善,加上第三方的ngx_cache_purge模块(用于清除指定URL的缓存),已经可以完全取代Squid。我们已经在生产环境使用了 Nginx 的 proxy_cache 缓存功能超过两个月,十分稳定,速度不逊于 Squid。

     在功能上,Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为“负载均衡服务器”与“Web缓存服务器”来使用。

1、Nginx 负载均衡与缓存服务器在 Linux 下的编译安装:

       安装pcre

  1. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
  2. tar zxvf pcre-8.00.tar.gz  
  3. cd pcre-8.00/  
  4. ./configure  
  5. make && make install  

       安装ngx_cache_purge

  1. wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz  
  2. tar zxvf ngx_cache_purge-2.0.tar.gz  

      安装nginx

  1. wget http://nginx.org/download/nginx-1.2.5.tar.gz  
  2. tar zxvf nginx-1.2.5.tar.gz  
  3. cd nginx-1.2.5/  
  4. ./configure --user=www --group=www --add-module=../ngx_cache_purge-2.0 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  
  5. make && make install

2、/usr/local/nginx/conf/nginx.conf 配置文件内容如下:

  1. user www www;
  2. worker_processes 8;
  3. error_log /usr/local/webserver/nginx/logs/nginx_error.log crit;
  4. pid /usr/local/webserver/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和proxy_cache_path指定的路径必须在同一分区
  41. proxy_temp_path /data0/proxy_temp_dir;
  42. #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天清理一次缓存,硬盘缓存空间大小为30GB。
  43. proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  44. upstream backend_server {
  45. server 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
  46. server 192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;
  47. server 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
  48. }
  49. server
  50. {
  51. listen 80;
  52. server_name www.yourdomain.com 192.168.8.42;
  53. index index.html index.htm;
  54. root /data0/htdocs/www;
  55. location /
  56. {
  57. proxy_pass http://backend_server;
  58. #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
  59. proxy_next_upstream http_502 http_504 error timeout invalid_header;
  60. proxy_cache cache_one;
  61. #对不同的HTTP状态码设置不同的缓存时间
  62. proxy_cache_valid 200 304 12h;
  63. #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
  64. proxy_cache_key $host$uri$is_args$args;
  65. proxy_set_header Host $host;
  66. proxy_set_header X-Forwarded-For $remote_addr;
  67. expires 1d;
  68. }
  69. #用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
  70. location ~ /purge(/.*)
  71. {
  72. #设置只允许指定的IP或IP段才可以清除URL缓存。
  73. allow 127.0.0.1;
  74. allow 192.168.0.0/16;
  75. deny all;
  76. proxy_cache_purge cache_one $host$1$is_args$args;
  77. }
  78. #扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
  79. location ~ .*\.(php|jsp|cgi)?$
  80. {
  81. proxy_set_header Host $host;
  82. proxy_set_header X-Forwarded-For $remote_addr;
  83. proxy_pass http://backend_server;
  84. }
  85. access_log off;
  86. }
  87. }

3、启动 Nginx:

/usr/local/nginx/sbin/nginx 

4、清除指定的URL缓存示例:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值