参考资料

  1. Nginx expires、etag、if_modified_since:客户端缓存控制详细说明以及案例
  2. Nginx 启动、停止、重启、加载配置详细说明以及案例
  3. LVS(Linux Virtual Server)负载均衡详细说明以及案例
  4. nginx 查看配置文件
  5. Nginxreferer:请求头控制模块详细说明以及案例
  6. Nginx的基本配置文件结构
  7. Nginx伪流媒体服务器搭建详细说明以及案例
  8. Nginxindex:首页处理模块详细说明以及案例

如何设置nginx高级安全配置

  1. 禁用不必要的HTTP方法
    在配置文件中添加:

location / {
    limit_except GET POST {
        deny all;
    }
}
  1. 隐藏Nginx版本信息
    在http模块中添加:

server_tokens off;
  1. 配置安全头部

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Content-Security-Policy "default-src 'self'";
  1. 限制客户端请求大小

client_max_body_size 1M;
  1. 启用SSL并配置安全协议

ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
  1. 配置访问控制

location /admin {
    allow 192.168.1.0/24;
    deny all;
}
  1. 防止目录遍历

autoindex off;
  1. 限制连接速率

limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
  1. 配置日志记录

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
  1. 启用HTTP严格传输安全(HSTS)

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;