作为IT工程师,保护Nginx服务器的安全至关重要。以下是一套全面的安全措施,可有效防止恶意操作和数据泄漏:
# 定期更新Nginx
sudo apt-get update && sudo apt-get upgrade nginx
# 或对于yum系统
sudo yum update nginx
sudo chown -R root:root /etc/nginx/
sudo chmod -R 644 /etc/nginx/
sudo find /etc/nginx/ -type d -exec chmod 755 {} \;
server_tokens off;
location / {
limit_except GET POST {
deny all;
}
}
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
location /admin {
allow 192.168.1.0/24;
allow 10.0.0.1;
deny all;
}
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
server {
location / {
limit_req zone=one burst=5;
}
}
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
location ~* "(\'|\")(.*)(drop|insert|md5|select|union)" {
deny all;
}
location ~* \.(php|log|env|git|svn|htaccess)$ {
deny all;
}
if ($http_user_agent ~* (wget|curl|libwww-perl|nikto|sqlmap|nmap|nessus|adminer) ) {
return 403;
}
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;
load_module modules/ngx_http_modsecurity_module.so;
http {
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
}
load_module modules/ngx_http_naxsi_module.so;
http {
include /etc/nginx/naxsi_core.rules;
location / {
SecRulesEnabled;
DeniedUrl "/RequestDenied";
}
}
# 使用Nginx Amplify或Nginx Plus进行配置审计
# 定期运行漏洞扫描工具如OpenVAS或Nessus
通过实施这些措施,您可以显著提高Nginx服务器的安全性,有效防止恶意操作和数据泄漏。记住,安全是一个持续的过程,需要定期审查和更新您的安全策略。