nginx的web缓存服务与新浪网的开源ncache模块
什么是web缓存
web缓存位于内容源web服务器和客户端之间,当用户访问一个 url时,web缓存服务器回去后端web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的url,web缓存服务器直接输出内容给客户端,而不是像源服务器再次发送请求。web缓存降低了内容源web服务器、数据库的负载,减轻了网络延迟,提高了用户的响应速度,增强了用户体验。
最著名的还要数squid cache,其主要在unix一类系统运行。
nginx的web缓存服务
nginx从0.7.48后支持类似于squid的缓存模块。这个缓存是把url及相关组合当做key,用md5算法对key进行希哈,得到硬盘上对应的希哈路径,从而将缓存内容保存在该目录内。支持任意url链接。同时也支持404/301/302这样的非200状态码。
nginx的web缓存服务主要用于proxy_cache相关指令集和fastcgi相关指令集构成,前者用于反向代理时,对后端内容源进行缓存,后者主要用于对fastcdi的动态程序进行缓存。两者功能基本一样。
proxy_cache相关指令集
1、proxy_cache指令
语法:proxy_cache zone_name;
默认值:none
使用环境:http,server,location
该指令用于设置那个缓存区将被应用,zone_name的值为proxy_cache_path指令创建的缓存区明称。
2、proxy_cache_path指令
语法:proxy_cache_path path[levels=number] keys_z [max_size=size];
默认值:none
使用环境:http
eg:
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_z 500m inactive=1d max_size=30g;
注意该指令只能在http标签内配置,levels指定该缓存有两层hash目录,第一层为1个字母,第二层为2个字母,保存文件名类似于/data0/proxy_cache_dir/c/29/fdg35415fg35f4gsdf2g1535gh465h;key_zone参数用来为缓存区起名,500m指定内存空间大小为500mb;inactive的1d是如果缓存数据在1天之内没有被访问,将被删除;max_size的30g是指硬盘的缓存空间为30gb。
3proxy_cache_methods指令
语法:proxy_cache_methods [get head post];
默认值:proxy_cache_methods get head;
使用环境:http,server,location
该指令用于设置用于缓存那些http方法,默认缓存 http get/head 方法,不缓存http post方法。
4proxy_cache_min_uses指令
语法:proxy_cache_min_uses the_number;
默认值:proxy_cache_min_uses 1;
使用环境:http,server,location
该指令设置缓存最小的使用次数,默认值是1.
5、proxy_cache_valid指令
语法:proxy_cache_valid reply_code [reply_code…]time;
默认值:none
使用环境:http,server,location
该指令用于对不同的返回状态码的url设置不同的缓存时间,例如:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
如果不指定状态吗,直接指定时间,则只有200、301、302状态的url缓存5分钟。
6、proxy_cache_key指令
语法:proxy_cache_key line;
默认值:none
使用环境:http,server,location
该指令用来设置web缓存的key值,nginx根据key值md5希哈存储缓存。一般根据‘$host(域名)、$request_uri(请求路径)’等组合变量合成proxy_cache_key.例如:proxy_cache_key $host:$server_port$uri$is_args$args;
proxy_cache完整示例
suyum -y install pcre//安装pcrewget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gztar zxvf ngx_cache_purge-2.3.tar.gz//获取nginx_cache_purgecd nginx-1.6.3//进入你的nginx文件目录(nginx安装请参考前面的博客) ./configure --user=www --group=www --addmodule=../ngx_cache_purge-2.3 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
配置nginx.conf
cd /usr/local/webserver/nginx/conf
#user www www;worker_processes1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { useepoll; worker_connections1024;}http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] $request '# '$status $body_bytes_sent $http_referer '# '$http_user_agent $http_x_forwarded_for';#access_log logs/access.log main;#charset utf-8;server_name_hash_bucket_size128; client_header_buffer_size32k; large_client_header_buffers432k; sendfileon; #tcp_nopush on;keepalive_timeout30; tcp_nodelyon; proxy_temp_path /data0/proxy_temp_path; proxy_temp_path /data0/proxy_temp_path levels=1:2 key_z200m inactive=1d max_size=30g; upstream my_sever_pool{ server192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s; server192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s; server192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s; } #gzip on;server { listen80; server_name localhost; #charset koi8-r;#access_log logs/host.access.log main;location / { proxy_set_header host $host; proxy_set_header x-forward-for $remote_addr; proxy_passhttp://my_server_pool; # root html;#index index.html index.htm; } location~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { #使用web缓存区cache_oneproxy_cache cache_one; #对不同状态码设置不同缓存时间proxy_cache_valid20030412h; proxy_cache_valid3013021m; proxy_cache_valid any im; #设置web缓存的key值,nginx根据key值md5希哈存储缓存,这里根据“域名/url 参数”组合成key。proxy_cache_key$host$uri$is_args$args; #反向代理,访问后端内容源服务器proxy_set_header host $host; proxy_set_header x-forwarded-for $remote_addr; proxy_pass http:my_server_pool; } #用于清除缓存,假设一个url为http://my.domain.com/text.gif通过访问http://my.domain.com/purge/test.gif可以清除该urk缓存。location~ /purge(/.*) { #设定只允许指定的ip或ip段才可以清除url缓存。allow127.0.0.1 allow 192.168.0.0/16; deny all; proxy_cache_purge cache_one $shot$1$is-args$args; } access_log 0ff #error_page 404 /404.html;# redirect server error pages to the static page /50x.html# error_page 500502503504 /50x.html; location = /50x.html { root html; } # proxy the php scripts to apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the php scripts to fastcgi server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param script_filename /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#} } # another virtual host using mix of ip-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# https server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:ssl:1m;# ssl_session_timeout 5m;# ssl_ciphers high:!anull:!md5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了nginx的web缓存服务与新浪网的开源ncache模块(1),包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。