第四章:nginx重要模塊+HTTPS+Lua詳解

官方模塊*

nginx -V
顯示的信息就是加載的模塊信息

  • Module(1)ngx_http_stub_status_module 本機狀態(tài)

    該ngx_http_stub_status_module模塊提供對基本狀態(tài)信息的訪問。
    此模塊不是默認生成的,應該使用--with-http_stub_status_module 配置參數(shù)啟用 。
    配置

    location / basic_status { 
        stub_status; 
    }
    
    Syntax(句法): stub_status;
    Default(默認): -
    Context(語境): server, location
    實例:
    1.增加配置
    image.png

    2.驗證語法
    nginx -tc /etc/nginx/nginx.conf 檢查文件是否有錯
    3.重載服務
    nginx -s reload -c /etc/nginx/nginx.conf
    4.效果查看
    訪問59.110.243.88/mystatus
    image.png
Module(2) ngx_http_random_index_module 主頁隨機

該ngx_http_random_index_module模塊處理以斜線字符(' /')結(jié)尾的請求,并選取一個目錄中的隨機文件作為索引文件。該模塊在ngx_http_index_module模塊之前進行處理 。
此模塊不是默認生成的,應該使用--with-http_random_index_module 配置參數(shù)啟用 。

location / {
    random_index on;
}
  • Directives(指令):
    Syntax(語法): random_index on | off;
    Default(默認): random_index off;
    Context(語境): location
Module(3) ngx_http_sub_module 字符替換

The ngx_http_sub_module module is a filter that modifies a response by replacing one specified string by another.
This module is not built by default, it should be enabled with the --with-http_sub_module configuration parameter.

  • Example Configuration
  location / {
    sub_filter '<a <a href="https://$host/';
    sub_filter '<img src="http://127.0.0.1:8080/' '<img src="https://$host/';
    sub_filter_once on;
  }
  • Directives
    Syntax: sub_filter string replacement;
    Default: —
    Context: http, server, location

  • Module(4) ngx_http_limit_conn_module 連接限制

    該ngx_http_limit_conn_module模塊用于限制每個定義的密鑰的連接數(shù)量,特別是來自單個IP地址的連接數(shù)量。
    并非所有連接都被計算在內(nèi) 只有在服務器處理請求并且已經(jīng)讀取了整個請求頭時才計算連接。

    • 一次只允許每個IP地址一個連接。
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    server {
      location /download/ {
          limit_conn addr 1;
      }
    
    • 可能有幾個limit_conn指令。例如,以下配置將限制每個客戶端IP到服務器的連接數(shù),同時限制連接到虛擬服務器的總數(shù):
    limit_conn_zone $ binary_remote_addr zone = perip:10m; 
    limit_conn_zone $ server_name zone = perserver:10m; 
    server{ 
        ... 
        limit_conn perip 10; 
        limit_conn perserver 100; 
      }
    
    • Directives
      Syntax: limit_conn zone number;
      Default: —
      Context: http, server, location

    • 詳細官方鏈接

  • Module(5) ngx_http_limit_req_module 請求限制

    所述ngx_http_limit_req_module模塊(0.7.21)用于限制每一個定義的密鑰的請求的處理速率,特別是從一個單一的IP地址的請求的處理速率。限制是使用“泄漏桶”方法完成的。

    • 平均每秒不超過1個請求,連發(fā)數(shù)不超過5個請求。
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
    server {
      location /search/ {
          limit_req zone=one burst=5;
      }
    
    • 可能有幾個limit_req指令。例如,以下配置將限制來自單個IP地址的請求的處理速率,同時限制虛擬服務器的請求處理速率:
    limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
    limit_req_zone $server_name zone=perserver:10m rate=10r/s;
    server {
      ...
      limit_req zone=perip burst=5 nodelay;
      limit_req zone=perserver burst=10;
    }
    
    • Directives
      Syntax: limit_req zone=name [burst=number] [nodelay];
      Default: —
      Context: http, server, location
    • 詳細官方鏈接
  • Module(6) ngx_http_access_module 限制訪問某些客戶端地址。

    該ngx_http_access_module模塊允許限制訪問某些客戶端地址。訪問也可以通過密碼子請求結(jié)果或JWT來限制滿足控制地址和密碼的同時訪問限制

    具有局限性,因為用戶一旦通過代理來訪問的話,是阻止不了的。http_x_forwarded_for頭信息控制訪問 會更好的解決該問題,它要求訪問時必須帶上所有用到的ip的地址信息

局限性解決方法總結(jié):

方法一: 采用http頭信息控制訪問,如HTTP_X_FORWARD_FOR
方法二: 結(jié)合geo模塊
方法三: 通過HTTP自定義變量傳遞

  • Example Configuration
location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

實例:
      location ~ ^/admin.html{
              root /usr/share/nginx;
              deny 192.168.1.1;#訪問admin.html需要限制的ip
              allow all;
              index index.html index.htm;
      }
  • 規(guī)則按順序檢查,直到找到第一個匹配。在本例中,只允許IPv4網(wǎng)絡訪問 10.1.1.0/16``192.168.1.0/24 不包括地址192.168.1.1和IPv6網(wǎng)絡訪問2001:0db8::/32在很多規(guī)則的情況下,使用模塊變量是可取的。
Syntax:   allow address | CIDR | unix: | all;
Default:  —
Context:  http, server, location, limit_except
  • 允許訪問指定的網(wǎng)絡或地址。如果unix:指定了特殊值(1.5.1),則允許訪問所有UNIX域套接字。
    句法: deny address | CIDR | unix: | all;
    默認: -
    語境: http,server,location,limit_except

  • 詳細官方鏈接

  • Module(7) ngx_http_auth_basic_module

    該ngx_http_auth_basic_module模塊允許通過使用“HTTP基本認證”協(xié)議驗證用戶名和密碼來限制對資源的訪問。訪問也可以通過地址,jwt。滿足指令控制地址和密碼的同時訪問限制。

在這里首先安裝htpasswd

局限性:

一: 用戶信息依賴文件
二: 操作管理機械,效率低

解決方式:

一: nginx結(jié)合LUA實現(xiàn)高效驗證
二: nginx配合LDAP打通,利用nginx-auth-ldap模塊

  • Example Configuration
    location / {
    auth_basic           "closed site";
    auth_basic_user_file conf/htpasswd;
  }
  實例:

location ~ ^/admin.html{
              root /usr/share/nginx;
              auth_basic "please input your password!"#這里隨便寫字符串
              auth_basic_user_file /etc/nginx/password; #htpasswd生成的密碼所在文件路徑
              index index.html index.htm;
      }
然后wq ->語法檢查 nginx -t -c /etc/nginx/nginx.conf
  • 使用“HTTP基本驗證”協(xié)議驗證用戶名和密碼。指定的參數(shù)用作a realm。參數(shù)值可以包含變量(1.3.10,1.2.7)。特殊值off允許取消auth_basic從先前配置級別繼承的指令的效果。
Syntax:   auth_basic string | off;
Default:  
auth_basic off;
Context:  http, server, location, limit_except

  • 指定一個保存用戶名和密碼的文件,格式如下:
    # comment
    name1:password1
    name2:password2:comment
    name3:password3
    Syntax: auth_basic_user_file file;
    Default:    —
    Context:    http, server, location, limit_except
  • 詳細官方鏈接

  • Module(8) ngx_http_addition_module

    該ngx_http_addition_module模塊是一個過濾器,在響應之前和之后添加文本。此模塊不是默認生成的,應該使用--with-http_addition_module 配置參數(shù)啟用 。

    • Example Configuration
        location / {
          add_before_body /before_action;
          add_after_body  /after_action;
        }
    
    • 在響應主體之前添加處理給定子請求的結(jié)果返回的文本。""作為參數(shù)的空字符串()會取消從前一個配置級別繼承的加法。
    句法:   add_before_body uri;
    默認:   -
    語境:   http,server,location
    
    • 在響應主體之后添加由于處理給定的子請求而返回的文本。""作為參數(shù)的空字符串()取消了從以前的配置級別繼承的加法。
      句法: add_after_body uri;
      默認: -
      語境: http,server,location
    • 允許在指定的MIME類型的響應中添加文本,除了“ text/html”。特殊值“ *”匹配任何MIME類型(0.8.29)。
      句法: addition_types mime-type ...;
      默認: addition_types text / html;
      語境: http,server,location
      該指令出現(xiàn)在0.7.9版本中。
    • 詳細官方鏈接
  • Module(9) ngx_http_slice_module-大文件分片請求
  • 優(yōu)勢:每個自請求收到的數(shù)據(jù)都會形成一個獨立文件,一個請求斷了,其他請求不受影響。

  • 缺點:當文件很大或者slice很小的時候,可能會導致文件描述符耗盡等情況

location / {
    slice             1m;
    proxy_cache       cache;
    proxy_cache_key   $uri$is_args$args$slice_range;
    proxy_set_header  Range $slice_range;
    proxy_cache_valid 200 206 1h;
    proxy_pass        http://localhost:8000;
}

高級模塊篇

  • Module(10) ngx_http_secure_link_module

    該ngx_http_secure_link_module模塊(0.7.18)用于檢查請求鏈接的真?zhèn)危Wo資源免受未經(jīng)授權的訪問,并限制連桿的壽命。
    請求的鏈接的真實性通過將請求中傳遞的校驗和值與為請求計算的值進行比較來驗證。如果鏈接的使用期限有限且時間已過,則認為該鏈接已過時。這些檢查的狀態(tài)在$secure_link變量中可用 。
    該模塊提供兩種替代操作模式。第一種模式由secure_link_secret指令啟用,用于檢查請求的鏈接的真實性,并保護資源免受未經(jīng)授權的訪問。第二種模式(0.8.50)由secure_link和secure_link_md5指令啟用, 也用于限制鏈接的生存期。

此模塊不是默認生成的,應該使用--with-http_secure_link_module 配置參數(shù)啟用 。

  • Example Configuration
location /s/ {
  secure_link $arg_md5,$arg_expires;
  secure_link_md5 "$secure_link_expires$uri$remote_addr secret";

  if ($secure_link = "") {
      return 403;
  }

  if ($secure_link = "0") {
      return 410;
  }

  ...
}
--------------------------------分割線----------------------------------------------
location /p/ {
  secure_link_secret secret;

  if ($secure_link = "") {
      return 403;
  }

  rewrite ^ /secure/$secure_link;
}

location /secure/ {
  internal;
}
  • 定義一個帶有變量的字符串,從中提取鏈接的校驗值和生存期。
句法: secure_link expression;
默認: -
語境: http,server,location
  • 定義word用于檢查所請求的鏈接的真實性的秘密。
句法: secure_link_secret word;
默認: -
語境: location
  • 定義一個計算MD5哈希值的表達式,并將其與請求中傳遞的值進行比較。
句法: secure_link_md5 expression;
默認: -
語境: http,server,location
  • 詳細官方鏈接
  • Module(11) ngx_http_geoip_module
    • 使用場景:
    1. 區(qū)別國內(nèi)外作HTTP訪問規(guī)則
    2. 區(qū)別國內(nèi)城市地域作HTTP訪問規(guī)則
    • 該模塊需要下載MaxMind庫(存放世界的ip信息)
location / {
    if ($geoip_country_code != CN){
          return 403;      #如果ip不是中國的,就返回403
    }
}
  • 詳細官方鏈接
  • Module(12) 配置HTTPS服務器
    • 為什莫要用HTTPS?:
    1. HTTP傳輸數(shù)據(jù)可以被中間人盜用
    2. 數(shù)據(jù)被劫持篡改
    3. 同時用到對稱加密+非對稱加密
    • 該模塊需要安裝openssl (用于生成密鑰和CA證書)
cd /etc/nginx
mkdir ssl_key
openssl genrsa -idea -out fantj.key 1024
      enter pass phrase for fantj.key:
------
openssl req -new -key fantj.key -out fant.csr
------
openssl x509 -req -days 3650 -in fantj.csr -signkey fantj.key -out fantj.crt #打包成crt文件

server {
    listen              443 ssl;
    server_name         127.0.0.1:8080;
    ssl_certificate     /etc/nginx/ssl_key/fantj.crt;
    ssl_certificate_key /etc/nginx/ssl_key/fantj.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ...
}
  • HTTPS服務優(yōu)化
    1. 激活keepalive長連接
    2. 設置ssl session緩存
http {
    ssl_session_cache   shared:SSL:10m; #設置緩存
    ssl_session_timeout 10m;   #設置過期時間

    server {
        listen              443 ssl;
        server_name         www.example.com;
        keepalive_timeout   70;   #保持連接時間

        ssl_certificate     www.example.com.crt;
        ssl_certificate_key www.example.com.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        ...
  • 詳細官方鏈接
  • Module(13) nginx+Lua 實現(xiàn)灰度發(fā)布

    灰度發(fā)布(又名金絲雀發(fā)布)是指在黑與白之間,能夠平滑過渡的一種發(fā)布方式。在其上可以進行A/B testing,即讓一部分用戶繼續(xù)用產(chǎn)品特性A,一部分用戶開始用產(chǎn)品特性B,如果用戶對B沒有什么反對意見,那么逐步擴大范圍,把所有用戶都遷移到B上面來。灰度發(fā)布可以保證整體系統(tǒng)的穩(wěn)定,在初始灰度的時候就可以發(fā)現(xiàn)、調(diào)整問題,以保證其影響度。

location / {
      default_type "text/html";
      content_by_lua_file /opt/app/lua/dep.lua;   #將根目錄請求交給dep.lua過濾處理
}
至于dep.lua源碼,大家盡可在百度中得到,在這里我不做重點,以后可能會單獨作為一章。
bacause重點是模塊講解,so這里提綱挈領下。
~sorry
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容