【Day11】Nginx實戰訓練營
- 41 配置Nginx單向SSL
- 42 配置Nginx雙向SSL
- 43 Nginx錯誤日志
- 44 Nginx通路日志-日志格式
41 配置Nginx單向SSL
先看機器裡安裝的 nginx 有沒有安裝 ssl 子產品,yum 安裝的可以 nginx -V,編譯安裝的可以 /usr/local/nginx/sbin/nginx -V
一般 yum 安裝的 nginx 都已經自帶 ssl 子產品,而編譯安裝的需要重新編譯安裝。
./configure --with-http_ssl_module --prefix=/usr/local/nginx
make && make install
-
Nginx 單向配置示例:
cp /etc/pki/ca_test/server/server.* /usr/local/nginx/conf/
或者改變 ssl_certificate 參數絕對路徑
server {
listen 443 ssl;
server_name www.1.com;
index index.html index.php;
root /data/wwwroot/1.com;
ssl on;
ssl_certificate /etc/pki/ca_test/server/server.crt;
ssl_certificate_key /etc/pki/ca_test/server/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;
ssl_prefer_server_ciphers on;
}
-
報錯:
nginx: [warn] the “ssl” directive is deprecated, use the “listen … ssl” directive instead in /etc/nginx/conf.d/www.1.com.conf:6
-
解決:
将 ssl on; 這行注釋掉
- 配置說明:
- 443 端口為 ssl 監聽端口。
- ssl on 表示打開ssl 支援。
- ssl_certificate 指定 crt 檔案所在路徑,如果寫相對路徑,必須把該檔案和 nginx.conf 檔案放到一個目錄下。
- ssl_certificate_key 指定 key 檔案所在路徑。
- ssl_protocols 指定 SSL 協定。
- ssl_ciphers 配置 ssl 加密算法,多個算法用:分隔,ALL 表示全部算法,! 表示不啟用該算法,+ 表示将該算法排到最後面去。
- ssl_prefer_server_ciphers 如果不指定預設為 off,當為 on 時,在使用 SSLv3 和 TLS 協定時,伺服器加密算法将優于用戶端加密算法。
42 配置Nginx雙向SSL
cp /etc/pki/ca_test/root/ca.crt /usr/local/nginx/conf/
- Nginx 雙向配置示例:
server {
listen 443 ssl;
server_name www.1.com;
index index.html index.php;
root /data/wwwroot/1.com;
ssl on;
ssl_certificate /etc/pki/ca_test/server/server.crt;
ssl_certificate_key /etc/pki/ca_test/server/server.key
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!eNULL;
ssl_prefer_server_ciphers on;
ssl_client_certificate /etc/pki/ca_test/root/ca.crt; //這裡的ca.crt是根證書公鑰檔案
ssl_verify_client on;
}
-
用戶端(浏覽器)操作
如果不進行以下操作,浏覽器會出現400錯誤。400 Bad Request(No required SSL certificate was sent)
首先需要将client.key轉換為pfx(p12)格式【Day11】Nginx實戰訓練營41 配置Nginx單向SSL42 配置Nginx雙向SSL43 Nginx錯誤日志44 Nginx通路日志-日志格式
[[email protected] client]# cd /etc/pki/ca_test/client
[[email protected] client]# openssl pkcs12 -export -inkey client.key -in client.crt -out client.pfx
Enter Export Password:
Verifying - Enter Export Password:
//這一步需要輸入一個自定義密碼,一會在windows上安裝的時候要用到,需要記一下。
然後将 client.pfx 拷貝到 windows 下,輕按兩下即可安裝。
chrome 浏覽器中,設定——進階——隐私設定和安全性——管理證書
也可以直接 curl 測試:
參考:
https://coding.net/u/aminglinux/p/nginx/git/blob/master/ssl/nginx.md
43 Nginx錯誤日志
-
Nginx的錯誤日志
Nginx錯誤日志平時不用太關注,但是一旦出了問題,就需要借助錯誤日志來判斷問題所在。
配置參數格式: error_log /path/to/log level;
-
Nginx錯誤日志級别
常見的錯誤日志級别有 debug | info | notice | warn | error | crit | alert | emerg
級别越高記錄的資訊越少,如果不定義,預設級别為 error.
它可以配置在 main、http、server、location 段裡。
如果在配置檔案中定義了兩個 error_log,在同一個配置段裡的話會産生沖突,是以同一個段裡隻允許配置一個 error_log。
但是,在不同的配置段中出現是沒問題的。
-
Nginx錯誤日志示例
error_log /var/log/nginx/error.log crit;
如果要想徹底關閉 error_log,需要這樣配置 error_log /dev/null;
44 Nginx通路日志-日志格式
Nginx 通路日志可以設定自定義的格式,來滿足特定的需求。
- 通路日志格式示例
- 示例1
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
- 示例2
log_format main '$remote_addr [$time_local] '
'$host "$request_uri" $status "$request"'
'"$http_referer" "$http_user_agent" "$request_time"';
格式:
若不配置 log_format 或者不在 access_log 配置中指定 log_format,則預設格式為:
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent";
- 常見變量
變量 | 說明 |
---|---|
$time_local | 通用日志格式下的本地時間;(伺服器時間) |
$remote_addr | 用戶端(使用者)IP位址 |
$status | 請求狀态碼,如200,404,301,302等 |
$body_bytes_sent | 發送給用戶端的位元組數,不包括響應頭的大小 |
$bytes_sent | 發送給用戶端的總位元組數 |
$request_length | 請求的長度(包括請求行,請求頭和請求正文) |
$request_time | 請求處理時間,機關為秒,小數的形式 |
$upstream_addr | 叢集輪詢位址 |
$upstream_response_time | 指從Nginx向後端(php-cgi)建立連接配接開始到接受完資料然後關閉連接配接為止的時間 |
$remote_user | 用來記錄用戶端使用者名稱 |
$request | 請求方式(GET或者POST等)+URL(包含 r e q u e s t m e t h o d , request_method, requestmethod,host,$request_uri) |
$http_user_agent | 使用者浏覽器辨別 |
$http_host | 請求的url位址(目标url位址)的host |
$host | 等同于$http_host |
$http_referer | 來源頁面,即從哪個頁面轉到本頁,如果直接在浏覽器輸入網址來通路,則referer為空 |
$uri | 請求中的目前URI(不帶請求參數,參數位于 a r g s ) , 不 同 于 浏 覽 器 傳 遞 的 args),不同于浏覽器傳遞的 args),不同于浏覽器傳遞的request_uri的值,它可以通過内部重定向,或者使用index指令進行修改。 |
$document_uri | 等同于$uri |
$request_uri | 比 u r i 多 了 參 數 , 即 uri多了參數,即 uri多了參數,即uri+$args |
$http_x_forwarded_for | 如果使用了代理,這個參數會記錄代理伺服器的ip和用戶端的ip |
資料:
https://coding.net/u/aminglinux/p/nginx/git/blob/master/log/format.md