bash
./configure --without-http_autoindex_module --without-http_ssi_module
bash
sudo apt-get update && sudo apt-get upgrade nginx
nginx
server_tokens off;
nginx
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
nginx
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Content-Security-Policy "default-src 'self'";
nginx
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
nginx
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
nginx
location /admin {
allow 192.168.1.0/24;
deny all;
}
速率限制:
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
location / {
limit_req zone=one burst=20;
}
nginx
set_real_ip_from 172.31.0.0/16;
real_ip_header X-Forwarded-For;
nginx
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;
dockerfile
FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf
USER nginx
nginx
location /health {
access_log off;
return 200 "healthy\n";
}
结构化日志:
log_format json_combined escape=json
'{"time":"$time_iso8601",'
'"remote_addr":"$remote_addr",'
'"request":"$request",'
'"status":$status,'
'"body_bytes_sent":$body_bytes_sent}';
access_log /var/log/nginx/access.log json_combined;
bash
nginx -t # 测试配置
grep -r "ssl_protocols" /etc/nginx # 检查加密配置
使用配置管理工具:
package { 'nginx':
ensure => 'latest',
}
service { 'nginx':
ensure => running,
enable => true,
}
DDoS防护:
limit_conn_zone $binary_remote_addr zone=addr:10m;
location / {
limit_conn addr 10;
}
nginx
location ~* "(\'|\")(.*)(drop|insert|md5|select|union)" {
deny all;
}
通过以上措施,可以显著提升Nginx在云端环境中的安全性,同时保持高性能和可用性。建议定期复查安全配置并根据最新的威胁情报进行调整。