Appearance
静态资源缓存
合理配置静态资源缓存可以显著提高网站性能,减少服务器负载。
浏览器缓存
expires指令
nginx
location ~* \.(jpg|jpeg|png|gif|webp|svg|ico)$ {
expires 30d;
add_header Cache-Control "public";
}Cache-Control头
nginx
location ~* \.(css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}完整配置
nginx
server {
listen 80;
server_name static.example.com;
root /var/www/static;
# HTML文件(短期缓存)
location ~* \.html$ {
expires 1h;
add_header Cache-Control "public";
}
# 图片文件(中期缓存)
location ~* \.(jpg|jpeg|png|gif|webp|svg|ico)$ {
expires 30d;
add_header Cache-Control "public";
}
# CSS和JS文件(中期缓存)
location ~* \.(css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# 字体文件(长期缓存)
location ~* \.(woff|woff2|ttf|otf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}Nginx缓存
proxy_cache_path指令
nginx
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;proxy_cache指令
nginx
location / {
proxy_cache my_cache;
proxy_pass http://backend;
}proxy_cache_valid指令
nginx
location / {
proxy_cache my_cache;
proxy_cache_valid 200 60m;
proxy_cache_valid 404 1m;
proxy_pass http://backend;
}完整缓存配置
静态文件缓存
nginx
server {
listen 80;
server_name static.example.com;
root /var/www/static;
# 性能优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# 文件缓存
open_file_cache max=10000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# HTML文件
location ~* \.html$ {
expires 1h;
add_header Cache-Control "public";
}
# 图片文件
location ~* \.(jpg|jpeg|png|gif|webp|svg|ico)$ {
expires 30d;
add_header Cache-Control "public";
access_log off;
}
# CSS和JS文件
location ~* \.(css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
}
# 字体文件
location ~* \.(woff|woff2|ttf|otf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
add_header Access-Control-Allow-Origin "*";
}
}代理缓存
nginx
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=proxy_cache:10m max_size=1g inactive=60m use_temp_path=off;
upstream backend {
server 192.168.1.10:8080;
}
server {
listen 80;
server_name proxy.example.com;
location / {
proxy_cache proxy_cache;
proxy_cache_valid 200 60m;
proxy_cache_valid 404 1m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_pass http://backend;
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 X-Cache-Status $upstream_cache_status;
}
}缓存策略
短期缓存
nginx
location ~* \.html$ {
expires 1h;
add_header Cache-Control "public";
}中期缓存
nginx
location ~* \.(jpg|jpeg|png|gif|css|js)$ {
expires 30d;
add_header Cache-Control "public";
}长期缓存
nginx
location ~* \.(woff|woff2|ttf|otf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}禁用缓存
nginx
location ~* \.html$ {
expires off;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}缓存清理
手动清理
bash
# 删除缓存目录
rm -rf /var/cache/nginx/*
# 删除特定缓存
find /var/cache/nginx -name "*.cache" -delete缓存清除模块
nginx
location /cache/ {
proxy_cache_bypass $http_cache_control;
proxy_no_cache $http_cache_control;
}缓存监控
查看缓存状态
nginx
location /cache_status {
stub_status on;
access_log off;
}查看缓存命中率
nginx
log_format cache '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$upstream_cache_status"';
access_log /var/log/nginx/cache.log cache;完整示例
生产环境配置
nginx
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=proxy_cache:10m max_size=1g inactive=60m use_temp_path=off;
upstream backend {
server 192.168.1.10:8080;
}
server {
listen 80;
server_name www.example.com;
root /var/www/html;
# 文件缓存
open_file_cache max=10000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# 静态资源
location ~* \.(jpg|jpeg|png|gif|webp|svg|ico)$ {
expires 30d;
add_header Cache-Control "public";
access_log off;
}
location ~* \.(css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
}
location ~* \.(woff|woff2|ttf|otf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
add_header Access-Control-Allow-Origin "*";
}
# 代理缓存
location /api/ {
proxy_cache proxy_cache;
proxy_cache_valid 200 60m;
proxy_cache_valid 404 1m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_pass http://backend;
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 X-Cache-Status $upstream_cache_status;
}
}常见问题
缓存不生效
原因: Cache-Control头设置错误
解决: 检查Cache-Control头
nginx
add_header Cache-Control "public";缓存未更新
原因: 缓存时间过长
解决: 缩短缓存时间或手动清理
nginx
expires 1h;缓存命中率低
原因: 缓存key设置不当
解决: 优化缓存key
nginx
proxy_cache_key "$scheme$request_method$host$request_uri";总结
静态资源缓存的关键点:
- 浏览器缓存:设置合理的过期时间
- Nginx缓存:配置proxy_cache
- 缓存策略:根据文件类型设置不同策略
- 缓存清理:定期清理过期缓存
- 缓存监控:监控缓存命中率
合理配置缓存,提高网站性能,减少服务器负载。