配置Nginx代理缓存可以大幅减少后端应用的负载,显著提升响应速度:
# 在http块中定义缓存区域
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=app_cache:10m max_size=1g inactive=60m;
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_cache app_cache;
proxy_cache_valid 200 10m; # 200响应缓存10分钟
proxy_cache_valid 404 1m; # 404缓存1分钟
proxy_cache_use_stale error timeout updating;
add_header X-Cache-Status $upstream_cache_status;
}
# 静态文件直接服务(绕过后端)
location /static/ {
root /var/www/myapp;
expires 1y;
add_header Cache-Control "public, immutable";
gzip_static on;
}
}
开启Nginx的Gzip压缩:
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript
text/xml application/xml image/svg+xml;
gzip_vary on;
在Anubiz Host的NVMe VPS上,Nginx的静态文件服务性能接近理论I/O上限,是高性能Web托管的理想组合。