nginx设置web缓存,需要用到的相关指令:
1、proxy_cache #使用字段:http, server, location
2、proxy_cache_path #使用字段:http
3、proxy_cache_valid #使用字段:http, server, location
需要注意的事项:
1、windows下面设置缓存不会在指定的path生成的缓存文件
2、指定的path上一级目录需要存在,否则会报错 (假设缓存文件存放于/usr/local/cc/abc,那么cc目录需要事先建立)
3、proxy_cache_path 只能存放于http中,不能放置于server或location
实例:
user www www; worker_processes 8; #worker_cpu_affinity auto; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; worker_rlimit_nofile 65535; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { use epoll; worker_connections 20480; } http { server_names_hash_bucket_size 512; server_names_hash_max_size 512; 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; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; server_tokens off; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; proxy_cache_path /wwwroot/nginx/cache/one levels=1:2 keys_zone=one:50m inactive=1h max_size=1g; proxy_cache_path /wwwroot/nginx/cache/two levels=1:2 keys_zone=two:50m inactive=1h max_size=1g; upstream lunxun1 { server 192.168.1.1:80; server 192.168.1.2:80; server 192.168.1.3:80; server 192.168.1.4:80; ip_hash; } upstream lunxun2 { server 192.168.2.1:80; server 192.168.2.2:80; server 192.168.2.3:80; server 192.168.2.4:80; ip_hash; } include vhost/*.conf; } |
proxy_cache_path /wwwroot/nginx/cache/one levels=1:2 keys_zone=one:50m inactive=1h max_size=1g;
levels指定目录结构,可以使用任意的1位或2位数字作为目录结构,如 X, X:X,或X:X:X 例如: “2”, “2:2”, “1:1:2“,但是最多只能是三级目录。
keys_zone=one 参数用来为这个缓存区起名(proxy_cache 指令需要用到 其后对应缓存区名称)
one指的是共享池的名称,50m指的是内存缓存空间的大小
inactive=1h指如果缓存数据在1小时(天:d、秒:s、分:m)内没有被访问,将自动被删除,默认inactive为10分钟;
max_size=1g 指硬盘缓存大小为1g
下面是vhost里面的具体网站的conf文件配置
server { listen 80 default; server_name www.test.com; #增加两头部 查看缓存命中情况 add_header Server-Via $server_addr; add_header Nginx-Cache $upstream_cache_status; location / { proxy_pass http://test; proxy_set_header Host www.hao123.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; proxy_cache one; proxy_cache_valid 200 10m; proxy_connect_timeout 60; proxy_read_timeout 600; proxy_send_timeout 600; } access_log off; error_log /dev/null; } |
proxy_cache one; #设置一个缓存区域的名称,一个相同的区域可以在不同的地方使用
proxy_cache_valid 200 10m; #为不同的应答设置不同的缓存时间
#增加两头部 查看缓存命中情况和服务器地址
add_header Server-Via $server_addr;
add_header Nginx-Cache $upstream_cache_status;
用fiddler查看headers里面的Nginx-Cache、Server-Via
MISS 未命中
EXPIRED – expired。请求被传送到后端。
UPDATING – expired。由于proxy/fastcgi_cache_use_stale正在更新,将使用旧的应答。
STALE – expired。由于proxy/fastcgi_cache_use_stale,后端将得到过期的应答。
HIT 命中
参照资料:http://www.360doc.com/content/13/1114/12/7694408_329125489.shtml