Nginx的配置與開發學習(二):訪問控制與請求限制

Nginx模塊講解

Nginx官方模塊

編譯選項 作用
–with-http_stub_status_module Nginx的客戶端狀態

http_stub_status_module配置

server {
    listen       80;  //確保端口不被佔用
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    #添加內容
    location /mystatus {
        stub_status;
    }
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    ......
}
  1. 檢查語法是否正確
    nginx -tc /etc/nginx/nginx.conf

  2. 沒問題之後重載服務
    nginx -s reload -c /etc/nginx/nginx.conf

  3. 訪問網址
    ip:port/mystatus

    Active connections: 1 
    server accepts handled requests
     255(處理握手次數) 255(處理連接數) 265(總的請求數) 
    Reading: 0 Writing: 1 Waiting: 0 
    

默認模塊

編譯選項 作用
–with-http_random_index_ module 目錄中選擇一個隨機主頁

http_random_index_ module配置

server {
    listen       80;  //確保端口不被佔用
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    
    location / {
        root   /opt/app/code;      ##文件夾中放3個html,訪問主頁之後回隨機訪問這3個html(不能以.xx.html命名)
        random_index on;   #默認爲off
        index  index.html index.htm;
    }
    ......
}
編譯選項 作用
–with-http_sub_module http內容替換

–with-http_sub_module配置

server {
    listen       80;  //確保端口不被佔用
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /opt/app/code; ##該目錄下的html文件是要替換的內容
        index  index.html index.htm;
        sub_filter '需要替換的字符串' '被替換的內容';##網頁中需要替換的內容
        sub_filter_once off;   ##設置全局替換,而不是隻替換一次
    }
    .......
}

Nginx的請求限制(壓測工具 ab)

HTTP請求建立在一次TCP的連接基礎上

一次TCP請求至少產生一次HTTP請求

    limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
    limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
server {
    listen       80;  //確保端口不被佔用
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    #添加內容
    location /mystatus {
        stub_status;
    }
    
    location / {
        root   /opt/app/code;
        #limit_conn conn_zone 1;   限制連接數
        #limit_req zone=req_zone burst=3 nodelay; 對前3個請求延遲響應,其他全部返回50X
        #limit_req zone=req_zone burst=3;對前3個請求延遲響應,其他全部等待
        #limit_req zone=req_zone;
        index  index.html index.htm;
    }
    ......
}

Nginx的訪問控制

  1. 基於IP的訪問控制 ---- http_access_module
server {
    listen       80;  //確保端口不被佔用
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    #添加內容
    location /mystatus {
        stub_status;
    }
    
    location / {
        root   /opt/app/code;
        index  index.html index.htm;
    }
    
    location ~ ^/admin.html {#模式匹配
        root   /opt/app/code;
        deny  XXX.XXX.XXX.XX;
        allow all;
        index  index.html index.htm;
    }
    
    location ~ ^/admin.html {#模式匹配
        root   /opt/app/code;
        allow XXX.XXX.XXX.XX
        deny  all;
        index  index.html index.htm;
    }
    ......
}

侷限性:你限制了ip1地址,但是你實際上用這個ip1去訪問的時候採用了代理ip2,那麼nginx認爲的ip是代理的ip2,就不會限制ip1的地址

解決方法:

  • 採用別的HTTP頭信息訪問控制,但是頭信息可以唄修改 如:http_x_forwarded_for模塊,http_x_forwarded_for=Client IP,Proxy(1) IP,Proxy(2) IP…
  • 結合geo模塊來解決
  • 通過HTTP自定義變量傳遞,在HTTP頭中自定義變量,一級一級的攜帶到後端
  1. 基於用戶的信任登錄----http_auth_basic_module

    • 服務器安裝htpasswd工具:yum install httpd-tools -y
    • 生成密碼文件,最好在文件上一級目錄/etc/nginx: htpasswd -c ./auth_conf cyj
    • 查看密碼文件:more auth_conf
    server {
        listen       80;  //確保端口不被佔用
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        #添加內容
        location /mystatus {
            stub_status;
        }
        
        location / {
            root   /opt/app/code;
            index  index.html index.htm;
        }
        
        location ~ ^/admin.html {#模式匹配
            root   /opt/app/code;
            auth_basic  "XXXXXXXX請輸入密碼";
            auth_basic_user_file /etc/nginx/auth_conf;  #存放密碼文件路徑
            index  index.html index.htm;
        }
        .......
    }
    

    侷限性:

    • 用戶信息依賴文件方式
    • 操作管理機械,效率低下

    解決方案:

    • Nginx結合LUA實現高效驗證
    • Nginx和LDAP打通,利用nginx-auth-ldap模塊
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章