如何通过Nginx配置提升Web服务器安全性
引言
· Nginx作为广泛使用的Web服务器,其安全性配置至关重要。
· 本文将详细介绍如何从多个维度增强Nginx的安全性。
一、基础安全配置
1. 隐藏版本号信息
· 目的:防止攻击者利用版本号信息进行定向攻击。
· 配置:
server_tokens off;
2. 配置安全Headers
· 目的:防御常见的Web攻击。
· 配置:
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1;mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "strict-origin-origin-when-cross-origin";
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'";
二、访问控制优化
1. 限制连接数
· 目的:防止DOS攻击。
· 配置:
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn addr 100;
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=10r/s;
limit_req zone=req_zone burst=20 nodelay;
2. 配置白名单
· 目的:保护敏感区域。
· 配置:
location /admin/ {
allow 192.168.1.0/24;
allow 10.0.0.0/8;
deny all;
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
}
三、SSL/TLS安全配置
1. 启用HTTPS
· 目的:强制HTTPS访问,保护数据传输安全。
· 配置:
listen 443 ssl;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
return 301 https://$server_name$request_uri;
add_header Strict-Transport-Security "max-age=31536000" always;
2. 优化SSL配置
· 目的:使用更安全的SSL配置参数。
· 配置:
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_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
四、文件上传安全
1. 限制上传文件大小
· 目的:防止通过上传大文件耗尽服务器资源。
· 配置:
client_max_body_size 10m;
client_body_buffer_size 128k;
client_body_temp_path /var/nginx/client_body_temp;
2. 配置上传目录权限
· 目的:确保上传目录的权限配置正确。
· 配置:
location /uploads/ {
root /var/www/uploads;
client_body_temp_path /var/www/tmp;
dav_methods PUT DELETE MKCOL COPY MOVE;
create_full_put_path on;
dav_access user:rw group:rw all:r;
if ($request_filename ~* ^.*?\.(php|php5|sh|pl|py)$) {return 403;}
}
五、防止常见攻击
1. 防止SQL注入
· 目的:过滤特殊字符,防止SQL注入。
· 配置:
location / {
if ($request_uri ~* [;'<>] ) {return 444;}
if ($args ~* [;'<>] ) {return 444;}
location ~* /(admin|backup|config|db|src)/ {deny all;}
}
2. 防止目录遍历
· 目的:禁止访问隐藏文件和目录。
· 配置:
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location ~* ^/(uploads|images)/.*\.(php|php5|sh|pl|py|asp|aspx|jsp)$ {deny all;}
autoindex off;
六、日志安全
1. 配置访问日志
· 目的:详细记录访问信息,便于安全分析。
· 配置:
log_format detailed '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time $upstream_response_time';
access_log /var/log/nginx/access.log detailed buffer=32k flush=5s;
2. 配置错误日志
· 目的:设置适当的错误日志级别。
· 配置:
error_log /var/log/nginx/error.log warn;
七、其他安全措施
1. 禁止执行脚本
· 目的:在静态资源目录中禁止执行脚本。
· 配置:
location /static/ {
location ~ \.(php|php5)$ {deny all;}
location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf|eot)$ {expires 30d; add_header Cache-Control "public, no-transform";}
}
2. 配置超时时间
· 目的:设置合理的超时参数,防止慢速攻击。
· 配置:
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 5 5;
send_timeout 10;
proxy_read_timeout 10;
proxy_connect_timeout 10;
总结
· 以上配置涵盖了Nginx安全加固的主要方面。
· 根据具体业务需求和安全级别要求,适当调整配置。
· 定期更新Nginx,使用配置文件包含组织大型配置,检查配置正确性,定期检查日志文件,并配合WAF使用。