sudo sh -c "echo -n 'username:' >> /etc/nginx/.htpasswd"
sudo sh -c "openssl passwd -apr1 >> /etc/nginx/.htpasswd"
location /protected/ {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location /admin/ {
allow 192.168.1.100;
allow 10.0.0.0/24;
deny all;
}
location / {
deny 123.45.67.89;
allow all;
}
location /sensitive/ {
autoindex off;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
}
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location /uploads/ {
# 禁止执行PHP文件
location ~ \.php$ {
deny all;
return 403;
}
}
location /api/ {
limit_except GET POST {
deny all;
}
}
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
location /login/ {
limit_req zone=one burst=5;
}
}
location /admin/ {
access_log /var/log/nginx/admin_access.log;
error_log /var/log/nginx/admin_error.log;
}
通过以上配置和实践,您可以有效地保护Nginx服务器上的敏感目录,防止未授权访问和数据泄露。