标签:nginx cdn nginx_cache_purge 缓存
系统架构:
nginx+tomcat+mysql
本文只做Nginx做为CDN缓存负载均衡代理的配置实现的介绍
相关软件:
nginx-1.8.1.tar.gz
ngx_cache_purge-2.3.tar.gz (用于手动清理缓存)
一、nginx安装
[root@localhost ~]tar -xf nginx-1.8.1.tar.gz
[root@localhost ~]tar -xf ngx_cache_purge-2.3.tar.gz -C /usr/local/ngx_cache_purge-2.3
[root@localhost ~]cd nginx-1.8.1
[root@localhost nginx-1.8.1]./configure --prefix=/usr/local/nginx --user=nginx --without-http_fastcgi_module --without-http_scgi_module --add-module=/usr/local/ngx_cache_purge-2.3;make;make install
二、nginx.conf配置
user nginx;
worker_processes 2;
error_log logs/error.log;
pid /usr/local/nginx/nginx.pid;
events {
worker_connections 1024;
}
http {
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
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;
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;
server_tokens off;
log_format main ‘$http_x_forwarded_for - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$upstream_cache_status" $remote_addr‘;
#缓存配置1:2表示第一级目录1个字符,第二级目录2个字符;cache1:20m表示每个缓存区域20M空间;
#3d表示3天后缓存过期
proxy_cache_path /ngx_cache/proxy_cache/cache1 levels=1:2 keys_zone=cache1:20m inactive=3d max_size=500m;
upstream web_app
{
server 0.0.0.0:8081 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name
#purge缓存清理配置
location ~ /purge(/.*) {
allow all;
proxy_cache_purge cache1 $host$1$is_args$args;
}
#代理配置
location / {
proxy_pass http://web_app;
proxy_next_upstream http_502 http_504 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;
add_header Nginx-Cache $upstream_cache_status;#添加响应头的缓存状态,在浏览器F12可以看到Nginx-Cache:HIT,Nginx-Cache:MISS等状态
proxy_cache cache1;#设置资源缓存的zone
proxy_cache_key $host$uri$is_args$args;#设置缓存的key,以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_valid 200 304 30m;#200和304状态码进行30分钟的缓存
expires 1d;#缓存过期时间
}
#错误页提示
error_page 500 502 503 504 /50/50.html;
location ~ /50(/.*) {
root html;
}
}三、缓存清理测试
如访问http://203.195.144.57/public/images/main/homepage/wechat.jpg
则清理此图片的格式为http://203.195.144.57/purge/public/images/main/homepage/wechat.jpg
标签:nginx cdn nginx_cache_purge 缓存
原文地址:http://jacyliao.blog.51cto.com/9336240/1828175