#设缓存区名称blo**n,内存缓存大小500M,缓存数据超7天没被访问自动清除;访问缓存数据,硬盘缓存空间大小8G
proxy_cache_path /data/caches/a.com levels=1:2 keys_zone=blo**n:500m inactive=30d max_size=8G;
server {
listen 80;
listen 443 ssl http2;
ssl_certificate /data/ssl/www.a.com.crt;
ssl_certificate_key /data/ssl/www.a.com.key;
ssl_session_timeout 10m;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ‘ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384HE-RSA-AES128-GCM-SHA256HE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHAHE-RSA-AES128-SHA256HE-RSA-AES128-SHAHE-RSA-AES256-SHA256HE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHAES-CBC3-SHA:!DSS’;
ssl_prefer_server_ciphers on;
ssl_stapling on;
ssl_stapling_verify on;
proxy_ssl_server_name on;
server_name a.com a.com;
access_log /data/wwwlogs/a.com_nginx.log combined;
#判断移动设备(针对非自适应主题)
set $is_mobile 0;
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
set $is_mobile 1;
}
#缓存清理配置
location ~ /dcache(/.*) {
#清理PC缓存
#proxy_cache_purge blo**n $1$is_args$args;
#清理PC和移动页面缓存
proxy_cache_purge blo**n $1$is_args$args$is_mobile;
}
#缓存静态资源
location ~ .*.(css|js|png|jpg|jpeg|gif|bmp|ico|svg|webp|ttf|woff)?$ {
proxy_ignore_headers X-Accel-Expires Cache-Control Expires Set-Cookie Vary;
proxy_hide_header Cache-Control;
proxy_set_header Accept-Encoding "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_pass https://源站IP;
#proxy_redirect off;
proxy_cache blo**n;
proxy_cache_lock on;
proxy_cache_lock_age 5s;
proxy_cache_lock_timeout 5s;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_min_uses 1;
#缓存服务器缓存状态码30天
proxy_cache_valid 200 304 30d;
proxy_cache_valid 301 302 1d;
proxy_cache_valid 500 502 503 504 0s;
proxy_cache_valid any 1m;
proxy_cache_revalidate on;
#浏览器缓存30天
expires 30d;
proxy_cache_key $uri$is_args$args;
add_header CDN-Cache "$upstream_cache_status";
}
#缓存首页、html页面、分类页面、搜索页面等全部
location / {
#不遵循源站服务器的响应头
proxy_ignore_headers X-Accel-Expires Cache-Control Expires Vary;
proxy_pass https://源站IP;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache blo**n;
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
proxy_cache_min_uses 1;
proxy_cache_valid 200 304 1d;
proxy_cache_valid 500 502 503 504 0s;
proxy_cache_valid any 10s;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
#PC和移动页面单独缓存
proxy_cache_key $uri$is_args$args$is_mobile;
add_header CDN-Cache "$upstream_cache_status";
#设置登陆情况下不缓存
set $do_not_cache 0;
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
}
# 跳过缓存
proxy_cache_bypass $do_not_cache;
# 不保存缓存
proxy_no_cache $do_not_cache;
}
#忽略动态页面、登陆页面、后台中心
location ~ .*.(php||xml|wp-admin|admin)?$ {
proxy_pass https://源站IP;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}