4. Nginx模塊

Nginx官方模塊

1.ngx_http_stub_status_module

http://nginx.org/en/docs/http...

此模塊能夠查看nginx對數據包處理的基本信息php

#啓用方法
location /status {
  stub_status;
}

訪問 /status,展現的數據以下html

Active connections: 4 
server accepts handled requests
310840 310840 481035 
Reading: 0 Writing: 1 Waiting: 3

2. ngx_http_random_index_module

http://nginx.org/en/docs/http...

此模塊用於在目錄下,隨機地取用某個文件,做爲默認主頁。假如 /usr/share/nginx/html/random 目錄下有 aaa.htmlbbb.htmlccc.html 三個文件,則使用以下配置便可nginx

location /random {
    root /usr/share/nginx/html;
    random_index on;
}

以下代碼會隨機返回 aaabbbccc 這三個html之一shell

curl http://127.0.0.1/random/

3. ngx_http_sub_module

http://nginx.org/en/docs/http...

用於替換掉響應內容中的指定字符串。數據庫

location / {
    sub_filter '<a href="https://www.baidu.com/'  '<a href="https://www.qq.com/';
    #若是文件中有多處須要替換,只替換第一處
    sub_filter_once on;
    #保留替換前,原始的最後修改時間
    sub_filter_last_modified on;
    #默認只替換 text/html 這一MIME類型
    sub_filter_types text/html;
}

4. ngx_http_limit_conn_module

http://nginx.org/en/docs/http...
HTTP協議 請求與鏈接 說明
HTTP 1.0 TCP不能複用 一個鏈接,一個請求
HTTP 1.1 順序性TCP複用 一個鏈接,能夠按順序的發出多個請求
HTTP 2.0 多路TCP複用 一個鏈接,能夠並行的發出多個請求

咱們能夠把nginx的zone,理解成一個動態的數據庫,1M的zone內存能夠存儲至少16000條記錄,判斷用戶請求是否合法,就是不斷查詢當前IP在數據庫中記錄的數量是否超出了限制。併發

在以下的路由請求中,咱們限制用戶的併發鏈接數分別爲5個和10個。運維

注意:併發是指同一時刻的請求量,與每秒的請求數量是有區別的。dom

http {
    limit_conn_zone $binary_remote_addr zone=addr_conn:10m;
    ...
    server {
        ...
        location / {
            limit_conn addr_conn 5;
             ...
        }
        location ~ \.php$ {
            limit_conn addr_conn 10;
            ...
        }
    }
}

爲了測試代碼效果,咱們能夠安裝 httpd-tools 進行ab測試。
對於limit_conn 的測試,無論是內網測試,仍是外網測試,都是可行的。curl

yum install httpd-tools
ab -n 20 -c 20 http://127.0.0.1/

執行 ab -n 20 -c 20 http://127.0.0.1/,總共20個鏈接,失敗了15個,成功創建的鏈接數只有5個了工具

條目 數值
Complete requests: 20
Failed requests: 15

執行 ab -n 20 -c 20 http://127.0.0.1/index.php,總共20個鏈接,失敗了10個,成功創建的鏈接數確實只有10個

條目 數值
Complete requests: 20
Failed requests: 10

通過以上測試,能夠得出的結論就是:代碼是不會騙人的

對於頻率限制形成的 Failed requests ,咱們在 nginxerror.log 中也能發現錯誤記錄。若是錯誤日誌過多,咱們就須要排查一下訪問量是否正常。若是正常的話,又須要考慮一下如何優化參數設置。

2017/07/31 11:41:37 [error] 24550#0: *580766 limiting connections by zone "addr_conn", client: 119.130.188.64, server: www.siguoya.name, request: "GET / HTTP/1.0", host: "www.siguoya.name"
2017/07/31 11:41:37 [error] 24550#0: *580767 limiting connections by zone "addr_conn", client: 119.130.188.64, server: www.siguoya.name, request: "GET / HTTP/1.0", host: "www.siguoya.name"

5.ngx_http_limit_req_module

http://nginx.org/en/docs/http...

ngx_http_limit_conn_module 用於限制併發鏈接數,而 ngx_http_limit_req_module 則用於限制併發請求數。實際業務場景中,一般使用 ngx_http_limit_req_module 會更多一些。

http {
    limit_req_zone $binary_remote_addr zone=addr_req:10m rate=1r/s;
    ...
    server {
        ...
        location / {
            limit_req zone=addr_req;
        }
    }
}

6.ngx_http_access_module

http://nginx.org/en/docs/http...

該模塊能夠經過限制 ip 來實現訪問控制,但弊端在於咱們有時沒法獲取到真實的IP,並且用戶的IP也是能夠動態變化的,另外還比較容易出現誤殺的狀況

能夠經過如下方法來彌補:

  • x_forwarded_for,可是並不可靠,存在被修改或者未傳遞的可能
  • geo 模塊
  • 經過HTTP自定義變量來傳遞
location / {
    allow 192.168.1.0/24;
    deny  all;
}

7.ngx_http_auth_basic_module

http://nginx.org/en/docs/http...

該模塊能夠要求用戶在訪問特定頁面的時候,必須輸入正確的帳號和密碼才能夠訪問,從而實現一個比較初級的權限控制功能

location /auth_basic {
  auth_basic "custom comment for this auth_basic";
  auth_basic_user_file /usr/local/nginx/conf/.passwd;
}

關於 auth_basic_user_file 使用到的文件,咱們可使用 htpasswd 這個工具來實現

#新建一個受權文件
htpasswd -bc username password >> .passwd
#在已有的受權文件中新加一個用戶
htpasswd -b username password >> .passwd

ngx_http_auth_basic_module 的缺陷以下:用戶權限依賴於受權文件,容易形成企業有多個用戶帳號體系,運維麻煩。

解決方法:

  • 結合 lua 實現高效認證
  • 利用 nginx-auth-ldap 模塊,和 ldap 打通

Nginx第三方模塊

有興趣的能夠訪問 https://www.nginx.com/resourc...,這裏就不一一展開介紹了

相關文章
相關標籤/搜索