参考资料

  1. Nginx的启动与停止命令
  2. Nginx 基于SSL的TCP代理服务器详细说明以及案例
  3. 如何设置Nginx的SSL/TLS加密?
  4. NginxWebDAV模块配置详细说明以及案例
  5. CGI(Common Gateway Interface,通用网关接口)详细说明以及案例
  6. Nginxrewrite重定向配置详解
  7. Nginx处理HTTP请求详细说明以及案例
  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;
}