> 技术文档 > Nginx常用安全配置指南_nginx安全设置

Nginx常用安全配置指南_nginx安全设置

Nginx是一个轻量级的,高性能的Web服务器以及反向代理和邮箱代理服务器。它运行在UNIX、GNU、linux、BSD、Mac OS X、Solaris和Windows各种版本。根据调查统计数据显示,当前全球超过6%的网站使用Nginx Web服务器来管理Web网站应用。

为了保证基于Nginx的Web应用安全, 我结合之前在项目中遇到的相应问题进行总结和提炼,针对Nginx常用安全配置进行梳理。

一、基础安全配置

  1. 隐藏版本信息:编辑 Nginx 源码中的 /http/ngx_http_header_filter_module.c 文件,找到以下两行并修改为自定义内容:

    static char ngx_http_server_string[] = “Server: Ninja Web Server” ;static char ngx_http_server_full_string[] = “Server: Ninja Web Server” ;

    然后重新编译安装 Nginx。

  2. 限制模块加载:在编译 Nginx 时,禁用不必要的模块,例如:

    ./configure -without-http_autoindex_module -without-http_ssi_modulemakemake install
  3. 限制并发连接:在 nginx.conf 文件中,使用 limit_zonelimit_conn 指令限制每个 IP 的并发连接数:

    limit_zone slimits $binary_remote_addr 5m;limit_conn slimits 5;
  4. 限制访问域名:在 nginx.conf 文件中,添加以下配置,只允许特定域名的请求:

    if ($host !~ ^(guoyu.city|www.guoyu.city|images.guoyu.city)$ ) { return 444;}
  5. 目录访问限制

    • 通过 IP 地址限制访问
      location /docs/ { deny 192.168.1.1; allow 192.168.1.0/24; deny all;}
    • 通过密码保护目录
      mkdir /usr/local/nginx/conf/.htpasswd/htpasswd -c /usr/local/nginx/conf/.htpasswd/passwd user

      nginx.conf 中添加:

      location ~ /(personal-images/.|delta/.) { auth_basic “Restricted”; auth_basic_user_file /usr/local/nginx/conf/.htpasswd/passwd;}
  6. 防止目录遍历:在 nginx.conf 文件中,设置 autoindex off 来防止目录遍历攻击:

    location / { autoindex off;}
  7. 过滤常见XSS攻击: 过滤常见的 XSS 攻击字符XSS 攻击常常涉及将标签或类似的特殊字符(如 )作为查询参数的一部分。你可以通过正则表达式来阻止这些非法字符的访问。

    server { listen 80; server_name your-domain.com; # 屏蔽包含  或类似的 XSS 攻击字符的查询字符串 location / { if ($query_string ~* \"<script|%3Cscript|%3E|%3C|alert|eval|javascript|\") { return 403; # 返回 HTTP 403 禁止访问 } # 其他配置... } # 其他配置...}

    除了 ,你还可以过滤其他可能用于 XSS 攻击的字符,比如 JavaScript 关键字、URL 编码后的字符(如 %3C 为 ),甚至是尝试使用 eval() 或 alert() 等 JavaScript 函数。

    server { listen 80; server_name your-domain.com; # 屏蔽含有危险字符或关键字的请求 location / { if ($query_string ~* \"<script|%3Cscript|%3E|%3C|alert|eval|javascript|\") { return 403; # 返回 HTTP 403 禁止访问 } # 其他配置... } # 其他配置...}
  8. 设置HTTP头: 虽然 X-XSS-Protection 响应头在现代浏览器中已被弃用,但它仍然可以在一些旧版本浏览器中提供一定的保护。可在location规则下添加相应的配置信息。

    add_header X-XSS-Protection \"1; mode=block\";
  9. 设置 Content-Type Options: 阻止浏览器从执行由服务器推断出的 MIME 类型,而非服务器声明的内容类型。可在location规则下添加相应的配置信息。

    add_header X-Content-Type-Options nosniff;
  10. 限制请求大小 :通过限制请求的大小,可以减少大型恶意载荷的风险。可在location规则下添加相应的配置信息。

    # 限制请求大小为10KBclient_max_body_size 10K;
  11. 实施内容安全策略(CSP): CSP 是一个额外的安全层,可以帮助检测和减轻某些类型的攻击,包括 XSS 和数据注入攻击。可在location规则下添加相应的配置信息。

    add_header Content-Security-Policy \"default-src \'self\'; script-src \'self\' https://apis.guoyu.city\";

二、SSL/TLS 配置

  1. 生成 SSL 证书

    cd /usr/local/nginx/confopenssl genrsa -des3 -out server.key 1024openssl req -new -key server.key -out server.csrcp server.key server.key.orgopenssl rsa -in server.key.org -out server.keyopenssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  2. 配置 SSL:在 nginx.conf 文件中,添加以下配置:

    server { server_name project.guoyu.city; listen 443; ssl on; ssl_certificate /usr/local/nginx/conf/server.crt; ssl_certificate_key /usr/local/nginx/conf/server.key; access_log /usr/local/nginx/logs/ssl.access.log; error_log /usr/local/nginx/logs/ssl.error.log;}
  3. 配置 HTTPS 重定向:在 nginx.conf 文件中,添加以下配置,将所有 HTTP 请求重定向到 HTTPS:

    server { listen 80; server_name example.com; return 301 https://$host$request_uri;}
  4. 配置 SSL 参数:在 nginx.conf 文件中,添加以下配置,提高 SSL 安全性:

    ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;ssl_prefer_server_ciphers on;
  5. 使用 Let’s Encrypt 自动续期证书

    • 安装 Certbot 和 Nginx 插件:
      sudo dnf install certbot python3-certbot-nginx -y
    • 使用 Certbot 申请 SSL 证书并自动配置 Nginx:
      sudo certbot --nginx -d your_domain.com -d www.your_domain.com
    • 配置定时任务,每天凌晨 2 点自动检查并续期证书:
      sudo crontab -e0 2 * * * /usr/bin/certbot renew --quiet --post-hook \"systemctl reload nginx\"

三、其他安全配置

  1. 安装 SELinux 策略:安装和编译 SELinux 策略以强化 Nginx Web 服务器:

    yum -y install selinux-policy-targeted selinux-policy-develcd /optwget http://downloads.sourceforge.net/project/selinuxnginx/se-ngix_1_0_10.tar.gz?use_mirror=nchc&rsquotar -zxvf se-ngix_1_0_10.tar.gzcd se-ngix_1_0_10/nginxmake/usr/sbin/semodule -i nginx.pp
  2. 限制 Nginx 连接传出:使用 Iptables 限制 Nginx 用户的传出连接:

    /sbin/iptables -A OUTPUT -o eth0 -m owner -uid-owner nginx -p tcp -dport 80 -m state -state NEW,ESTABLISHED -j ACCEPT
  3. 配置操作系统保护:正确设置 /nginx 文档根目录的权限,确保 Nginx 以非 root 用户运行:

    find /nginx -user nginxfind /usr/local/nginx/html -user nginxls -l /usr/local/nginx/html/
  4. 限制每个 IP 的连接数:在防火墙级别限制每个 IP 的连接数:

    /sbin/iptables -A INPUT -p tcp -dport 80 -i eth0 -m state -state NEW -m recent -set/sbin/iptables -A INPUT -p tcp -dport 80 -i eth0 -m state -state NEW -m recent -update -seconds 60 -hitcount 15 -j DROPservice iptables save

通过以上的安全配置,可以大大提高 Nginx 服务器的安全性,减少网站的安全威胁。