建站教程 2026-08-22 · 10分钟

影视站Nginx配置优化详解:性能调优/缓存配置/防盗链/安全头设置


影视站Nginx配置优化详解



Nginx是影视站最常用的Web服务器。合理的Nginx配置可以显著提升性能和安全性。本文将详细介绍影视站Nginx配置优化方法。

一、基础性能优化



```nginx
# nginx.conf 全局配置
worker_processes auto;
worker_rlimit_nofile 65535;

events {
worker_connections 10240;
use epoll;
multi_accept on;
}

http {
# 开启sendfile
sendfile on;
tcp_nopush on;
tcp_nodelay on;

# 保持连接
keepalive_timeout 30;
keepalive_requests 100;

# 压缩
gzip on;
gzip_min_length 1k;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml;

# 缓存路径
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
}
```

二、缓存配置



```nginx
# 浏览器缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}

# 代理缓存(反向代理时)
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m max_size=1g inactive=60m;

location / {
proxy_cache cache;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
}
```

三、防盗链配置



```nginx
location ~* \.(jpg|jpeg|png|gif|mp4|m3u8|ts)$ {
valid_referers none blocked server_names *.yourdomain.com;

if ($invalid_referer) {
return 403;
# 或返回防盗链图片
# rewrite ^/ /static/no_hotlink.png last;
}
}
```

四、安全头设置



```nginx
# 安全响应头
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "default-src 'self'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
```

五、限流配置



```nginx
# 请求限流
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;

# 连接限流
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;

server {
limit_req zone=req_limit burst=20 nodelay;
limit_conn conn_limit 10;
}
```

六、HTTPS优化



```nginx
server {
listen 443 ssl http2;

ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;

# 优化SSL
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
}
```

总结



Nginx配置优化包括性能调优、缓存、防盗链、安全头、限流和HTTPS。推荐使用http2+TLS1.3+gzip+缓存全面提升性能。资源组(ziyuanzu.com)提供Nginx最佳实践配置参考。


---

> 本文由 资源组(ziyuanzu.com) 原创整理发布,转载请注明出处。资源组持续收录和评测各类优质影视资源站与采集站,为影视站长提供最专业的资源站导航、采集站评测与视频源监控服务。