淺談ngx_http_ssl_module模塊

1️⃣ ngx_http_ssl_module

這個模塊全部用於http、server區域中

  • 配置參數
ssl on | off;  #爲指定的虛擬主機配置是否啓⽤ssl功能,此功能在1.15.0廢棄,使⽤listen [ssl]替代。
 
ssl_certificate /path/to/file; #當前虛擬主機使⽤使⽤的公鑰⽂件,⼀般是crt⽂件
 
ssl_certificate_key /path/to/file; #當前虛擬主機使⽤的私鑰⽂件,⼀般是key⽂件
 
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];  #⽀持ssl協議版本,早期爲ssl,現在是TSL,
默認爲後三個
 
ssl_session_cache off | none | [builtin[:size]] [shared:name:size]; #配置ssl緩存    
      off: 關閉緩存    
      none:  通知客⼾端⽀持ssl session cache,但實際不⽀持    
      builtin[:size]:使⽤OpenSSL內建緩存,爲每worker進程私有    
      [shared:name:size]:在各worker之間使⽤⼀個共享的緩存,需要定義⼀個緩存名稱和緩存空間⼤⼩,⼀兆 
      可以存儲4000個會話信息,多個虛擬主機可以使⽤相同的緩存名稱。
 
ssl_session_timeout time;#客⼾端連接可以復⽤ssl session cache中緩存的有效時⻓,默認5m 
  • ngx_http_ssl_module參數示例:
# 生成自簽名證書
mkdir /etc/nginx/certs
cd /etc/nginx/certs

openssl req  -newkey rsa:4096 -nodes -sha256 -keyout ca.key  -x509 -days 3650 -out ca.crt 
Generating a 4096 bit RSA private key
..................................................................................................................................................................................................................++
..................................................++
writing new private key to 'ca.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:test.com
Organizational Unit Name (eg, section) []:test.com
Common Name (eg, your name or your server's hostname) []:www.test.com    
Email Address []:

# ⾃制key和csr⽂件
openssl req  -newkey rsa:4096 -nodes -sha256 -keyout www.test.com.key     -out  www.test.com.csr 
Generating a 4096 bit RSA private key
................................................................................................................................................................................++
...............................................................++
writing new private key to 'www.test.com.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN  
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:test.com
Organizational Unit Name (eg, section) []:test.com
Common Name (eg, your name or your server's hostname) []:www.test.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@cenot7-7 certs]# ll
total 16
-rw-r--r-- 1 root root 2004 Jan  9 16:18 ca.crt
-rw-r--r-- 1 root root 3268 Jan  9 16:18 ca.key
-rw-r--r-- 1 root root 1691 Jan  9 16:21 www.test.com.csr
-rw-r--r-- 1 root root 3268 Jan  9 16:21 www.test.com.key

# 簽發證書
openssl x509 -req -days 3650 -in  www.test.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial  -out www.test.com.crt
Signature ok
subject=/C=CN/ST=BJ/L=BJ/O=test.com/OU=test.com/CN=www.test.com
Getting CA Private Key

# 查看證書內容
openssl x509 -in www.test.com.crt  -noout -text 

# 配置證書
server {
        listen 443 ssl ;
        server_name www.test.com ;
        root /app/website1;
        ssl_certificate /etc/nginx/certs/www.test.com.crt;
        ssl_certificate_key /etc/nginx/certs/www.test.com.key;
        ssl_session_cache shared:ssl_cache:5m;
        ssl_session_timeout 5m;
}

2️⃣ ngx_http_rewrite_module

  • Nginx服務器利⽤ngx_http_rewrite_module 模塊解析和處理rewrite請求,
  • 此功能依靠 PCRE(perl compatible regularexpression),因此編譯之前要安裝PCRE庫,
  • rewrite是nginx服務器的重要功能之⼀,⽤於實現URL的重 寫,URL的重寫是⾮常有⽤的功能
  • 官方文檔:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html

🅿 if指令

⽤於條件匹配判斷,並根據條件判斷結果選擇不同的Nginx配置,可以配置在server或location塊中進⾏配置, Nginx的if語法僅能使⽤if做單次判斷,不⽀持使⽤if else或者if elif這樣的多重判
在這裏插入圖片描述

  • condition
    使⽤正則表達式對變量進⾏匹配,匹配成功時if指令認爲條件爲true,否則認爲false,變量與表達式之間使⽤以下 符號鏈接:
    # 比較操作符
    =: #⽐較變量和字符串是否相等,相等時if指令認爲該條件爲true,反之爲false。 
    !=: #⽐較變量和字符串是否不相等,不相等時if指令認爲條件爲true,反之爲false。 
    ~: #表⽰在匹配過程中區分⼤⼩寫字符,(可以通過正則表達式匹配),滿⾜匹配條件爲真,不滿⾜爲假。 
    !~:#爲區分⼤⼩寫字符且匹配結果不匹配,不滿⾜爲真,滿⾜爲假。
    
    ~*: #表⽰在匹配過程中不區分⼤⼩寫字符,(可以通過正則表達式匹配),滿⾜匹配條件爲真,不滿⾜爲假。 
    !~*: #爲不區分⼤⼩字符且匹配結果不匹配,滿⾜爲假,不滿⾜爲真。
    
    # 文件及目錄存在性判斷
    -f 和 ! -f: #判斷請求的⽂件是否存在和是否不存在 
    -d 和 ! -d: #判斷請求的⽬錄是否存在和是否不存在。 
    -x 和 ! -x: #判斷⽂件是否可執⾏和是否不可執⾏。 
    -e 和 ! -e: #判斷請求的⽂件或⽬錄是否存在和是否不存在(包括⽂件,⽬錄,軟鏈接)
  • 示例如下:
if ( $scheme = http ){       
    return / https://wwww.studylinu.xyz;     
} 
if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}
if ($request_method = POST) {
    return 405;
}
if ($slow) {
    limit_rate 10k;
}
if ($invalid_referer) {
    return 403;
}

# 注: 如果$變量的值爲空字符串或是以0開頭的任意字符串,則if指令認爲該條件爲false,其他條件爲true

🅿 set指令

  • 指定key並給其定義⼀個變量,變量可以調⽤Nginx內置變量賦值給key
  • 另外set定義格式爲set $key $value
  • ⽆論是key還是value都要加$符號。

🅿 break指令

  • ⽤於中斷當前相同作⽤域(location)中的其他Nginx配置,與該指令處於同⼀作⽤域的Nginx配置中,位於它前⾯的 配置⽣效,位於後⾯的指令配置就不再⽣效了
  • Nginx服務器在根據配置處理請求的過程中遇到該指令的時候,回到上⼀層作⽤域繼續向下讀取配置
  • 該指令可以在server塊和location塊以及if塊中使⽤

🅿 return指令

  • return⽤於完成對請求的處理,並直接向客⼾端返回響應狀態碼
  • ⽐如其可以指定重 定向URL(對於特殊重定向狀態碼,301/302等)
  • 或者是指定提⽰⽂本內容(對於特殊狀態碼403/500等),處於此指令 後的所有配置都將不被執⾏
  • return可以在server、if和location塊進⾏配
    在這裏插入圖片描述
  • 示例:
return code; #返回給客⼾端指定的HTTP狀態碼 
return code (text); #返回給客⼾端的狀態碼及響應體內容,可以調⽤變量 
return code URL; #返回給客⼾端的URL地址

location /main {    
  root /data/nginx/html/pc;    
  default_type text/html;    index index.html;      
  if ( $scheme = http ){        
    #return 666;        
    #return 666 "not allow http";        
    #return 301 http://www.baidu.com;        
    return 500 "service error";        
    echo "if-----> $scheme"; #return後⾯的將不再執⾏     
  }
}

🅿 rewrite_log指令

  • 設置是否開啓記錄ngx_http_rewrite_module模塊⽇志記錄到error_log⽇志⽂件當中
  • 可以配置在http、server、 location或if當中
  • 需要⽇志級別爲notice

🅿 rewrite指令

通過正則表達式的匹配來改變URI,可以同時存在⼀個或多個指令,按照順序依次對URI進⾏匹配,rewrite主要是 針對⽤⼾請求的URL或者是URI做具體處理,以下是URL和URI的具體介紹

URI(universal resource identifier):通⽤資源標識符,標識⼀個資源的路徑,可以不帶協議。 
URL(uniform resource location):統⼀資源定位符,是⽤於在Internet中描述資源的字符串,是URI的⼦集, 
主要包括傳輸協議(scheme)、主機(IP、端⼝號或者域名)和資源具體地址(⽬錄和⽂件名)等三部分,⼀般格式爲
 scheme://主機名[:端⼝號][/資源路徑],如:http://www.a.com:8080/path/file/index.html就是⼀個URL 路徑,URL必須帶訪問協議。
 每個URL都是⼀個URI,但是URI不都是URL。
 
例如: 
http://example.org:8080/path/to/resource.txt  #URI/URL 
ftp://example.org/resource.txt   #URI/URL 
/absolute/path/to/resource.txt   #URI
  • rewrite的官⽅介紹地址:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite,
  • rewrite可 以配置在server、location、if,其具體使⽤⽅式爲
    在這裏插入圖片描述
  • rewrite將⽤⼾請求的URI基於regex所描述的模式進⾏檢查,匹配到時將其替換爲表達式指定的新的URI。
  • 注意
    • 如果在同⼀級配置塊中存在多個rewrite規則,那麼會⾃下⽽下逐個檢查;
    • 被某條件規則替換完成後,會重新⼀輪 的替換檢查,隱含有循環機制,但不超過10次;
    • 如果超過,提⽰500響應碼,[flag]所表⽰的標誌位⽤於控制此循環 機制
    • 如果替換後的URL是以http://或https://開頭,則替換結果會直接以重向返回給客⼾端, 即永久重定向301

🅿 rewrite flag使⽤介紹

  • 利⽤nginx的rewrite的指令,可以實現url的重新跳轉
  • rewrtie有四種不同的flag,分別是redirect(臨時重定向)、 permanent(永久重定向)、break和last
  • 前兩種是跳轉型的flag,跳轉型是指有客⼾端瀏覽器重新對新地址進⾏請求
  • 後兩種是代理型,代理型是在WEB服務器內部實現跳轉的
redirect; #臨時重定向,重寫完成後以臨時重定向⽅式直接返回重寫後⽣成的新URL給客⼾端,由客⼾端重新發起請求;
           使⽤相對 路徑,或者http://或https://開頭,狀態碼:302
 
permanent; #重寫完成後以永久重定向⽅式直接返回重寫後⽣成的新URL給客⼾端,由客⼾端重新發起請求,狀態碼:301
 
last; #重寫完成後停⽌對當前URI在當前location中後續的其它重寫操作,⽽後對新的URL啓動新⼀輪重寫檢查,
       不建議在 location中使⽤
 
break; #重寫完成後停⽌對當前URL在當前location中後續的其它重寫操作,⽽後直接將匹配結果返還給客⼾端即結束循環
        並返回數據給客⼾端,建議在location中使⽤
發佈了107 篇原創文章 · 獲贊 20 · 訪問量 6350
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章