yum update
或 apt-get update && apt-get upgrade
PermitRootLogin no
PasswordAuthentication no
bash
useradd -r -s /sbin/nologin nginx
user nginx;
server_tokens off;
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
# 只允许HTTP/1.1和HTTP/2
if ($server_protocol !~* "HTTP/1.1|HTTP/2.0") {
return 444;
}
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384...';
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
location / {
limit_req zone=one burst=20;
}
location ~ /\. {
deny all;
}
client_max_body_size 10m;
client_body_buffer_size 128k;
log_format security '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/security.log security;
使用ModSecurity模块:
load_module modules/ngx_http_modsecurity_module.so;
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
add_header X-Frame-Options "SAMEORIGIN";
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.example.com;";
bash
#!/bin/sh
nginx -s stop
通过以上步骤,您可以建立一个相对安全的Nginx服务器环境。安全是一个持续的过程,需要定期审查和更新配置以应对新的威胁。