自用nginx配置(常見安全配置,http轉https,http和https混合請求,解決http host頭攻擊漏洞)

自用nginx配置(常見安全配置,http轉https,http和https混合請求,解決http host頭攻擊漏洞)



#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
 
    access_log off; 
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    #keepalive_timeout  65;
    keepalive_timeout  60 60;
 
    # 設置請求正文即請求體(request body)的讀超時時間
    client_body_timeout 60s;  
    # 指定等待client發送一個請求頭的超時時間 
    client_header_timeout 60s;  
    # 設置將響應傳輸到客戶端的超時
    send_timeout 60s;  
     # 有大文件上傳時,需要指定body的最大值
    client_max_body_size 50m;
    # 限制了同一ip1秒鐘內最多可以請求20次
    #limit_req_zone $binary_remote_addr zone=reqperip:20m rate=50r/s;
    # 同一ip的請求使用緩存,大小爲10,並且不延遲
    #limit_req zone=reqperip burst=50 nodelay;
  
    # 限制併發數,$binary_remote_addr是限制同一客戶端ip地址;
    limit_conn_zone $binary_remote_addr zone=one:10m; 
    # 限制併發數,$server_name是限制同一server最大併發數 
    limit_conn_zone $server_name zone=perserver:10m;   
    # 最大併發連接數100
    limit_conn one  500; 
    # 表示該服務提供的總連接數不得超過2000,超過請求的會被拒絕 
    limit_conn perserver 2000;  
    # 限制下載帶寬,limit_rate爲限制下載速度;
    limit_rate 5000k;           
 
 
    #隱藏Nginx版本號
    server_tokens off;
  
    #隱藏Nginx後端服務指定Header的狀態:
    proxy_hide_header X-Powered-By;
    proxy_hide_header Server; 

    #防止XSS攻擊
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-XSS-Protection '1;mode=block';
    add_header X-Content-Type-Options nosniff;    
    ###############
    gzip  on;  # 默認壓縮配置
    gzip_min_length 2k; # 小於2k的不壓縮
    gzip_proxied any; # 無條件壓縮
    gzip_comp_level 2; # 壓縮級別1~9,越高壓縮越小CPU資源消耗越多
    gzip_buffers 16 8k; # 存儲壓縮結果數據流,以8k爲單位,向內存申請16倍
    #gzip  on; 
    #定義負載均衡域名,供後面的http和https使用
    upstream server8180 { 
       server 127.0.0.1:8180;   
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
 
   server {
     listen       80;
     listen       443 ssl;  
     # 配置Nginx解決http host頭攻擊漏洞https://blog.csdn.net/weixin_48207312/article/details/128288660
     server_name 127.0.0.1 test.abc.com localhost;
     if ($http_Host !~* ^127.0.0.1|test.abc.com|localhost$){
            return 403;
     }

     ssl_certificate      my_ssl.pem;
     ssl_certificate_key  my_ssl.key;
     ssl_session_cache    shared:SSL:1m;
     ssl_session_timeout  5m;
     ssl_ciphers  HIGH:!aNULL:!MD5;
     ssl_prefer_server_ciphers  on;
     # 自定義錯誤頁(關鍵參數:這個變量開啓後,我們才能自定義錯誤頁面,當後端返回404,nginx攔截錯誤定義錯誤頁面)
     proxy_intercept_errors on;
     error_page  404  /404.html;
     # 承接上面的location。
     location = /404.html {
       # 放錯誤頁面的目錄路徑。
       root   html;
     }  
  
     location /html {
       # 放錯誤頁面的目錄路徑。
       root   html;
     }  
  
    #強制將http重定向到https
    if ($scheme = http) {
       return 307 https://$host$request_uri;
     }
  
   #解決報錯“400 Bad Request: The plain HTTP request was sent to HTTPS port”
   location ~/ {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto https;
      proxy_pass http://server8180;
      proxy_redirect http:// https://;
      #解決http和https混合請求報錯
      add_header Content-Security-Policy upgrade-insecure-requests;
   } 

  }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章