参考资料

  1. 忽略非错误信息 nginx -q详细说明以及案例
  2. Nginx HTTP代理服务器详细说明以及案例
  3. Nginx镜像模块:ngx_http_mirror_modu详细说明以及案例le
  4. Jenkins安装与配置详细说明以及案例
  5. 如何防止SQL注入和XSS攻击?
  6. Nginx Keepalived配置详细说明以及案例
  7. Nginx进程配置指令详解
  8. Nginx的配置与管理
  1. 限制HTTP方法

location / {
    limit_except GET POST {
        deny all;
    }
}
  1. 禁用服务器信息

server_tokens off;
  1. 添加安全头

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
  1. 启用HTTPS重定向

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}
  1. 限制客户端上传大小

client_max_body_size 1M;
  1. 启用Basic认证

location /admin {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}
  1. 防止目录遍历

location ~* \.(env|log|htaccess)$ {
    deny all;
}
  1. 限制IP访问

location /api {
    allow 192.168.1.0/24;
    deny all;
}
  1. 启用CSP策略

add_header Content-Security-Policy "default-src 'self'";
  1. 禁用不必要的HTTP方法

if ($request_method !~ ^(GET|HEAD|POST)$ ) {
    return 405;
}