使用nginx进行cache缓存
一、nginx cache配置
1、nginx.conf 主配置文件
这个基本按默认配置做的,如果是生产环境可以再加上只允许通过的域名访问,不允许的403或转到其他页。另外进程数和连接数也要做相应修改。
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; 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; #CDN Include include proxy.conf; include upstream.conf; include www.361way.com.conf; server { listen 80; server_name localhost; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
2、proxy.conf代理配置
我这里将反向代理cache缓存的配置放在这里了,这个配置上面是放到全局配置里了,其实也可以放到局部设置里。
proxy_temp_path /data/cdn_cache/proxy_temp_dir; proxy_cache_path /data/cdn_cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;
3、upstream.conf配置
这个主要指向反向代理的真实应用的地址,如下:
upstream www.361way.com { server 192.168.56.102:80 weight=10 max_fails=3; }
4、虚拟主机www.361way.com.conf 配置
server { listen 80; server_name www.361way.com; access_log logs/www.361way.com-access.log main; location ~ .*\.(gif|jpg|png|html|htm|css|js|ico|swf|pdf)$ { #Proxy proxy_redirect off; proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://www.361way.com; #Use Proxy Cache proxy_cache cache_one; proxy_cache_key "$host$request_uri"; add_header Cache "$upstream_cache_status"; proxy_cache_valid 200 304 301 302 8h; proxy_cache_valid 404 1m; proxy_cache_valid any 2d; } location / { proxy_redirect off; proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://www.361way.com; client_max_body_size 40m; client_body_buffer_size 128k; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; } }
创建cache使用的目录:
mkdir -p /data/cdn_cache
完成后,重启nginx服务,通过ps查看会发现多出来两个nginx cache相关的进程。
二、nginx cache的结果查看
再通过url访问,查看头文件,会出现第一次访问的时候响应头是MISS的,第二次访问的时候就变成HIT的了,即命中了。
同时,查看cache使用目录可以查看到访问的文件对象内容:
# tree -A /data/cdn_cache/ /data/cdn_cache/ +-- proxy_cache_dir | +-- 9 | | +-- a8 | | +-- f28e02e3877f3826567907bcb0ebea89 | +-- e | +-- 88 | +-- 114250cf63938b2f9c60b2fb3e4bd88e +-- proxy_temp_dir
由于缓存使用的是md5sum加密,实际上对应的对象是可以查看对应缓存的位置的。如下:
echo -n '192.168.56.101/index.html' |md5sum |awk '{print $1}' 114250cf63938b2f9c60b2fb3e4bd88e
我们再看下proxy_cache_path /data/cdn_cache/proxy_cache_dir levels=1:2这个配置是很有意思的。其中的levels=1:2决定了cache对象存放的路径:
- 其中1表示MD5的最后一位。
- 其中2表示MD5的倒数第三位和第二位。
- 一个冒号表示一层。
比如下面的index.html,最后一位是e,倒数第三二位是88,所以其位置在e/88/下。
本站的发展离不开您的资助,金额随意,欢迎来赏!
You can donate through PayPal.My paypal id: itybku@139.comPaypal page: https://www.paypal.me/361way
You can donate through PayPal.My paypal id: itybku@139.comPaypal page: https://www.paypal.me/361way
近期评论