作为一款高性能的Web服务器和反向代理服务器,Nginx在资源优化和缓存管理方面提供了丰富的功能。下面我将详细介绍如何利用Nginx优化静态资源服务和动态内容缓存。
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_proxied any;
gzip_vary on;
gzip_types
指定需要压缩的文件类型gzip_comp_level
设置压缩级别(1-9)gzip_min_length
设置最小压缩文件大小gzip_vary
添加"Vary: Accept-Encoding"响应头location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
access_log off;
etag on;
}
expires
设置缓存过期时间add_header
添加缓存控制头access_log off
关闭访问日志减少I/Oetag on
启用ETag验证location /static/ {
# 使用CDN时设置正确的CORS头
add_header 'Access-Control-Allow-Origin' '*';
# 启用Brotli压缩(需要Nginx支持)
brotli on;
brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 文件读取优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
}
location ~* ^(.+)\.(v\d+)\.(js|css|png|jpg|jpeg|gif|ico)$ {
try_files $1.$3 =404;
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m use_temp_path=off;
server {
location / {
proxy_cache my_cache;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_lock on;
add_header X-Proxy-Cache $upstream_cache_status;
proxy_pass http://backend;
}
}
proxy_cache_path
定义缓存存储路径和参数proxy_cache_valid
设置不同响应码的缓存时间proxy_cache_use_stale
定义何时使用过期的缓存X-Proxy-Cache
头用于调试缓存命中情况proxy_cache_path /var/cache/nginx/micro levels=1:2 keys_zone=micro_cache:10m inactive=1m;
location /dynamic-content {
proxy_cache micro_cache;
proxy_cache_lock on;
proxy_cache_valid 200 5s;
proxy_cache_methods GET HEAD;
proxy_cache_bypass $http_cache_control;
proxy_no_cache $http_pragma $http_authorization;
proxy_pass http://backend;
}
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fastcgi_cache:10m inactive=60m;
server {
location ~ \.php$ {
fastcgi_cache fastcgi_cache;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_valid 200 301 302 30m;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
fastcgi_pass php_backend;
}
}
upstream backend {
least_conn;
server backend1.example.com max_fails=3 fail_timeout=30s;
server backend2.example.com max_fails=3 fail_timeout=30s;
server backend3.example.com max_fails=3 fail_timeout=30s;
# 健康检查
check interval=5000 rise=2 fall=3 timeout=1000 type=http;
check_http_send "HEAD /health HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
location / {
proxy_pass http://backend;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
}
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
# 全局连接优化
keepalive_timeout 65;
keepalive_requests 100;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
reset_timedout_connection on;
# TCP优化
tcp_nodelay on;
tcp_nopush on;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location /cache_status {
proxy_cache_stats;
allow 127.0.0.1;
deny all;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'Cache: $upstream_cache_status';
缓存不生效问题:
Cache-Control
头是否正确设置proxy_cache_bypass
和proxy_no_cache
指令内存占用过高:
proxy_cache_path
中的keys_zone
大小动态内容更新不及时:
通过以上配置和优化技巧,可以显著提升Nginx服务器的性能,特别是在高并发场景下。建议根据实际业务需求调整参数,并通过监控工具持续观察效果。