动静分离是指将动态请求和静态资源请求分开处理,通过不同的服务器或处理方式来优化网站性能。
server {
listen 80;
server_name example.com;
# 静态资源处理
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ {
root /path/to/static/files;
expires 30d;
access_log off;
}
# 动态请求转发
location / {
proxy_pass http://backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
root /var/www/static;
expires max;
add_header Cache-Control "public, no-transform";
etag on;
}
location ~* ^/static/.+\.(css|js)$ {
root /var/www;
try_files $uri $uri/ /static/main$is_args$args;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 365d;
add_header Cache-Control "public";
add_header Pragma "public";
add_header Vary "Accept-Encoding";
}
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json text/xml application/xml application/xml+rss text/javascript;
# 使用Nginx的concat模块合并多个CSS/JS文件
location /static/css/ {
concat on;
concat_max_files 10;
concat_types text/css;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
if ($http_x_forwarded_proto != "https") {
return 301 https://$host$request_uri;
}
root /var/www/static;
expires max;
add_header Cache-Control "public";
add_header CDN-Cache-Status $upstream_http_x_cache;
}
location ~* ^/static/ {
proxy_pass https://cdn.example.com;
proxy_cache my_cache;
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;
}
Link
头或<link rel="preload">
# 静态资源服务器配置
server {
listen 443 ssl http2;
server_name static.example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
root /var/www/static;
# 静态资源处理
location ~* \.(?:ico|css|js|gif|jpe?g|png|webp|svg|woff2?|ttf|eot)$ {
expires 365d;
access_log off;
add_header Cache-Control "public";
add_header Access-Control-Allow-Origin "*";
# Brotli压缩
brotli_static on;
# Gzip压缩
gzip_static on;
}
# 防止恶意访问
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
# 禁止访问隐藏文件
location ~ /\. { deny all; }
}
# 动态应用服务器配置
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
# 静态资源重定向到CDN
location ~* \.(?:ico|css|js|gif|jpe?g|png|webp|svg|woff2?|ttf|eot)$ {
return 301 https://static.example.com$request_uri;
}
# 动态请求处理
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
# 健康检查
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
}
通过以上配置和方法,可以有效地实现Nginx的动静分离和静态资源加速,显著提升网站性能。