# 工作进程配置
worker_processes auto; # 自动设置为CPU核心数
worker_rlimit_nofile 100000; # 每个worker能打开的最大文件数
events {
worker_connections 4096; # 每个worker的最大连接数
multi_accept on; # 一次接受所有新连接
use epoll; # Linux下高性能事件模型
}
http {
# 关闭不必要的日志
access_log off; # 生产环境可开启,但需限制级别
# 或使用缓冲
access_log /var/log/nginx/access.log combined buffer=32k flush=5m;
sendfile on; # 启用高效文件传输模式
tcp_nopush on; # 仅在sendfile on时有效,优化数据包发送
tcp_nodelay on; # 禁用Nagle算法,提高实时性
keepalive_timeout 30; # 保持连接超时时间
keepalive_requests 1000; # 每个连接最大请求数
# 文件传输优化
client_max_body_size 10m; # 根据需求调整上传大小限制
client_body_buffer_size 128k;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
# MIME类型缓存
types_hash_max_size 2048;
# 开启Gzip压缩
gzip on;
gzip_min_length 1024; # 小于此值不压缩
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_proxied any;
# 静态文件缓存
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}
ssl_protocols TLSv1.2 TLSv1.3; # 禁用旧的不安全协议
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_ecdh_curve secp384r1; # 使用更安全的椭圆曲线
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off; # 根据安全需求可选
ssl_stapling on; # OCSP装订
ssl_stapling_verify on;
# 隐藏Nginx版本号
server_tokens off;
# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
# CSP内容安全策略(根据实际需求调整)
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.example.com; img-src 'self' https://*.example.com data:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com" always;
# 禁用不安全的HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
# 限制特定文件访问
location ~* \.(env|log|htaccess|htpasswd|git|svn|swp)$ {
deny all;
return 404;
}
# 防止目录遍历
location ~ /\. {
deny all;
return 404;
}
# 定义限制区
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
# 应用请求速率限制
limit_req zone=one burst=20 nodelay;
limit_conn addr 10; # 每个IP最大连接数
# 登录页面更严格的限制
location = /login {
limit_req zone=one burst=5;
}
}
# 禁用iframe嵌入(除非必要)
add_header X-Frame-Options "DENY";
# 启用HSTS(强制HTTPS)
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
# 防止MIME类型混淆攻击
add_header X-Content-Type-Options "nosniff";
# 禁用Referer头中的来源信息
add_header Referrer-Policy "no-referrer-when-downgrade";
# 特征策略(根据实际需求调整)
add_header Feature-Policy "geolocation 'none'; midi 'none'; notifications 'none'; push 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; speaker 'self'; vibrate 'none'; fullscreen 'self'; payment 'none'";
log_format json_combined escape=json
'{'
'"time_local":"$time_local",'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"status": "$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"http_x_forwarded_for":"$http_x_forwarded_for"'
'}';
access_log /var/log/nginx/access.log json_combined;
# 启用状态页面(仅限内网访问)
server {
listen 127.0.0.1:8080;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
nginx -t
测试配置有效性以上配置需要根据实际业务需求进行调整,建议在应用前进行充分测试。