nginx 安全配置规范

隐藏版本号

      
        http {
          server_tokens off; # 隐藏版本号
        }
      
    

开启HTTPS

      
        http {
          server {
            listen 443; # 监听443端口
            server_name opstrip.com; # 监听域名
            
            ssl on; # 开启https
            ssl_certificate /etc/nginx/certs/opstrip.com.pem; # 配置nginx ssl证书的路径
            ssl_certificate_key /etc/nginx/certs/opstrip.com.key; # 配置nginx ssl证书key的路径
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 指定客户端建立连接时使用的ssl协议版本,如果不需要兼容TSLv1,直接去掉即可
            ssl_ciphers HIGH:!aNULL:!MD5; # 指定客户端连接时所使用的加密算法,你可以再这里配置更高安全的算法
          }
        }
      
    

黑白名单配置

      
        location /admin/ {
          allow 192.168.1.0/24; # 只允许该ip访问
          deny all; # 拒绝所有ip

          # 更多的时候客户端请求会经过层层代理,我们需要通过$http_x_forwarded_for来进行限制,可以这样写:
          # set $allow false;
          # if ($http_x_forwarded_for = "211.144.204.2") { set $allow true; }
          # if ($http_x_forwarded_for ~ "108.2.66.[89]") { set $allow true; }
          # if ($allow = false) { return 404; }
        }
      
    

账号认证

      
        server {
          location / {
          auth_basic "please input user&passwd";
          auth_basic_user_file key/auth.key;
          }
        }
      
    

限制请求方法

      
        # $request_method能够获取到请求nginx的method。这里配置只允许GET/POST方法访问,其他的method返回405
        if ($request_method !~ ^(GET|POST)$ ) {
          return 405;
        }
      
    

拒绝User-Agent

      
        if ($http_user_agent ~* LWP::Simple|BBBike|wget|curl) {
          return 444;
        }
      
    

图片防盗链

      
        location /images/ {
          valid_referers none blocked www.opstrip.com opstrip.com;
          if ($invalid_referer) {
          return 403;
          }
        }
      
    
      
        location /images/ {
          valid_referers blocked www.opstrip.com opstrip.com
          if ($invalid_referer) {
          rewrite ^/images/.*.(gif|jpg|jpeg|png)$ /static/qrcode.jpg last;
          }
        }
      
    

控制并发连接数

      
        http {
          limit_conn_zone $binary_remote_addr zone=ops:10m; # 设定保存各个键
      
          server {
              listen 80;
              server_name opstrip.com;
                 
              root /home/project/webapp;
              index index.html;
              location / {
                  limit_conn ops 10;
              }
              access_log /var/log/nginx/nginx_access.log main;
          }
        }
      
    

连接权限控制

      
        user  www;
        worker_processes  4;
        error_log  /var/log/nginx/nginx_error.log  crit;
        pid        /var/nginx/nginx.pid;
        events {
            use epoll;
            worker_connections 65535;
        }
      
    
      
        http {
          limit_req_zone $binary_remote_addr zone=test_req:10m rate=20r/s;
           …
           server {
               …
               location /download/ {
                  limit_req zone=test_req burst=5 nodelay;
               }
           }
        }
      
    
      
        http {
          limit_conn_zone test_zone $binary_remote_addr 10m;
          server {
              location /download/ {
                  limit_conn test_zone 10;
                  limit_rate 500k;
              }
          }
        }
      
    

缓冲区溢出攻击

      
        client_body_buffer_size 1K; # 客户端请求body占用缓冲区大小
        client_header_buffer_size 1k; # 客户端请求头部的缓冲区大小
        client_max_body_size 1k; # 客户端请求的最大可接受body大小,它出现在请求头部的Content-Length字段
        large_client_header_buffers 2 1k; # 一些比较大的请求头使用的缓冲区数量和大小
      
    
同时需要修改几个超时时间的配置
      
        client_body_timeout 10; # 请求body的超时时间, Nginx将返回“Request time out” (408)错误
        client_header_timeout 10; # 读取客户端请求头的超时时间 408错误
        keepalive_timeout 5 5; # 参数的第一个值表示客户端与服务器长连接的超时时间,可选的第二个参数参数表示Response头中Keep-Alive: timeout=time的time值,这个值可以使一些浏览器知道什么时候关闭连接,以便服务器不用重复关闭
        send_timeout 10; # 客户端应答后的超时时间
      
    

Header头设置

      
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";
      
    

其他的Header头配置