文章目錄
- 一、nginx常用模塊
- 1. Nginx目錄索引
- 1.1 配置示例
- 1.2 模擬企業內網倉庫
- 2. Nginx訪問控制
- 2.1 配置語法
- 2.2 示例
- 3. Nginx基礎認證
- 3.1 配置語法
- 3.2 配置示例
- 4. Nginx限流限速
- 4.1 為什么要限速
- 4.2 限速應用場景
- 4.3 請求頻率限速原理
- 4.4 限制請求并發數
- 4.5 并發連接數限制
- 4.6 限制下載速度
- 4.7 綜合場景實踐
- 5. nginx狀態監控
- 6 Nginx資源快取
- 6.1 瀏覽器無快取
- 6.2 瀏覽器有快取
- 6.3 快取過期校驗
- 6.4 快取配置語法
- 7. Nginx資源壓縮
- 7.1 配置語法
- 7.2圖片壓縮
- 7.3 文本檔案壓縮
- 二、 Nginx Location
- 2.1 什么是Location
- 2.2 語法
- 2.3 Location優先級示例
- 2.4 應用場景示例
- 2.5 Location @重定向
- 三、 Nginx 日志模塊
- 3.1 日志格式語法
- 3.2 Nginx日志格式中常用的變數
- 3.3 Nginx訪問日志
- 3.4 Nginx錯誤日志
- 3.5 Nginx日志過濾
一、nginx常用模塊
1. Nginx目錄索引
當ngx_http_index_module模塊找不到索引檔案時,通常會將請求傳遞給ngx_http_autoindex_module模塊,ngx_http_autoindex_module模塊處理以斜杠字符/結尾的請求,并生成目錄串列,
#啟用或禁用目錄串列輸出,on開啟,off關閉,
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location # 可以放在http server location任意位置
#指定是否應在目錄串列中輸出確切的檔案大小,on顯示位元組,off顯示大概單位,
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location
#指定目錄串列中的時間是應以本地時區還是UTC輸出,on本地時區,off UTC時間,
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location
1.1 配置示例
[root@web01 conf.d]# cat /etc/nginx/conf.d/bertwu.conf
server {
listen 80;
server_name mirror.bertwu.com;
charset utf-8; # 指定字符集,防止中文亂碼
autoindex on; # 打開索引,系統找不到index.html時候自動生成索引串列而不報404錯誤
autoindex_exact_size off; # 檔案以人性化單位顯示,默認位元組
autoindex_localtime on; # 檔案顯示本地時間
root /mirror/;
location / {
index index.html;
}
}

1.2 模擬企業內網倉庫
server {
listen 80;
server_name mirror.bertwu.com;
charset utf-8;
root /mirror;
location / {
index index.html;
}
# 提供yum源倉庫目錄
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
# 首頁內容
[root@web01 mirror]# cat /mirror/index.html
<font color="red" size="14" >歡迎來到企業私有鏡像站</p>
<a href="repo">點我跳轉到鏡像站</a>
#使用rsync同步科大園
[root@web01 mirror]# rsync -avz rsync://rsync.mirrors.ustc.edu.cn/repo/centos/ /mirror/repo/
# 將該目錄創建為倉庫目錄
# createrepo /mirror/repo
2. Nginx訪問控制
ngx_http_access_module 模塊允許限制對某些客戶端地址的訪問,
2.1 配置語法
#允許配置語法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location,
limit_except
#拒絕配置語法
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location,
limit_except
2.2 示例
- 只允許指定的IP能訪問/centos, 其它網段全部拒絕,
[root@web01 conf.d]# cat bertwu.com.conf
server {
listen 80;
server_name mirror.bertwu.com;
charset utf-8;
root /mirror;
location / {
index index.html;
}
# 提供yum源倉庫目錄
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.7/32; # 只允許本機訪問 /repo
deny all;
}
}
在本機上直接crul
[root@web01 conf.d]# curl -H Host:mirror.bertwu.com http://10.0.0.7/repo
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
換一臺主機crul 發現報403錯誤(因為另一臺主機的ip不是10.0.0.7)
[root@rsync ~]# curl http://10.0.0.7/repo
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
注意:deny和allow的順序是有影響的,默認情況下,從第一條規則進行匹配如果匹配成功,則不繼續匹配下面的內容,如果匹配不成功,則繼續往下尋找能匹配成功的內容
3. Nginx基礎認證
ngx_http_auth_basic_module模塊允許使用HTTP基本身份驗證,驗證用戶名和密碼來限制對資源的訪問,
3.1 配置語法
#使用HTTP基本身份驗證協議啟用用戶名和密碼驗證,
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location,
limit_except
#指定保存用戶名和密碼的檔案
Syntax: auth_basic_user_file file;
Default: -
Context: http, server, location,
limit_except
指定保存用戶名和密碼的檔案,格式如下:
#可以使用htpasswd程式或"openssl passwd"命令生成對應的密碼;
name1:passwd1
name2:passwd2
[root@web01 conf.d]# yum install httpd-tools
#使用htpaaswd創建新的密碼檔案, -c創建新檔案 -b允許命令列輸入密碼
[root@web01 conf.d]# htpasswd -c -b /etc/nginx/ngx_passwd bertwu 123
3.2 配置示例
[root@web01 conf.d]# cat bertwu.com.conf
server {
listen 80;
server_name mirror.bertwu.com;
charset utf-8;
root /mirror;
location / {
index index.html;
}
# 提供yum源倉庫目錄
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
#allow 10.0.0.7/32;
#deny all;
auth_basic "歡迎登錄";
auth_basic_user_file /etc/nginx/ngx_passwd; # 指定密碼檔案路徑
}
}
訪問 /repo發現已經需要密碼了

4. Nginx限流限速
4.1 為什么要限速
限制某個用戶在一定時間內能夠產生的Http請求,或者說限制某個用戶的下載速度,
超出限制系統會報503: 過載保護錯誤
4.2 限速應用場景
下載限速:限制用戶下載資源的速度;ngx_http_core_module
請求限制:限制用戶單位時間內所產生的Http請求數;ngx_http_limit_req_module
連接限制:限制同一時間的連接數,及并發數限制;ngx_http_limit_conn_module
4.3 請求頻率限速原理
水(請求)從上方倒入水桶,從水桶下方流出(被處理);如果說水(請求)流入的過快,水桶流出(被處理)的過慢,來不及流出的水存在水桶中(快取),然后以固定速率流出,水桶滿后則水溢位(丟棄),簡單來說就是:當處理速度,達不到請求的速度,則會將請求放置快取,然后持續處理,當快取被沾滿,如果還有大量的請求,則會被丟棄,
4.4 限制請求并發數
1.語法:
請求限制設定語法
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
呼叫語法
Syntax: limit_req zone number [burst=number] [nodelay];
Default: —
Context: http, server, location
2.基于來源IP對下載速率限制,限制每秒處理1次請求,但可以突發超過5個請求放入快取
[root@web01 conf.d]# cat bertwu.com.conf
# http標簽中定義請求限制, rate限制速率,限制一秒鐘最多一個IP請求
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
server {
listen 80;
server_name mirror.bertwu.com;
charset utf-8;
root /mirror;
# 請求超過1r/s,剩下的將被延遲處理,請求數超過burst定義的數量,則回傳503
limit_req zone=req_one burst=5 nodelay;
location / {
index index.html;
}
# 提供yum源倉庫目錄
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
#allow 10.0.0.7/32;
#deny all;
#auth_basic "歡迎登錄";
#auth_basic_user_file /etc/nginx/ngx_passwd;
}
}
語法解釋:
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
#第一個引數:$binary_remote_addr表示通過這個標識來做限制,限制同一客戶端ip地址,
#第二個引數:zone=req_one:10m表示生成一個大小為10M,名為req_one的記憶體區域,用來存盤訪問的頻次資訊,
#第三個引數:rate=1r/s表示允許相同標識的客戶端的訪問頻次,這里限制的是每秒1次,
limit_req zone=req_one burst=5 nodelay;
#第一個引數:zone=req_one 設定使用哪個配置區域來做限制,與上面limit_req_zone 里的name對應,
#第二個引數:burst=5,設定一個大小為5的緩沖區,當有大量請求過來時,超過了訪問頻次限制的請求
可以先放到這個緩沖區內,
#第三個引數:nodelay,超過訪問頻次并且緩沖區也滿了的時候,則會回傳503,如果沒有設定,
則所有請求會等待排隊
4.5 并發連接數限制
1.指令
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
Syntax: limit_conn zonename number;
Default: —
Context: http, server, location
2.設定共享記憶體區域和給定鍵值的最大允許個連接數,超過此限制時,服務器將回傳503錯誤以回復請求
[root@web01 conf.d]# cat bertwu.com.conf
limit_conn_zone $binary_remote_addr zone=req_od:10m; # 設定
server {
listen 80;
server_name mirror.bertwu.com;
charset utf-8;
root /mirror;
limit_conn req_od 2; # 呼叫,最大并發連接數為2,超過報503錯誤
limit_rate 200k; # 設定限速
location / {
index index.html;
}
# 提供yum源倉庫目錄
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
# 在/mirror/repo下創建bigdata檔案,瀏覽器模擬下載此檔案,由于限速,所以能夠長時間保持連接
[root@web01 mirror]# dd if=/dev/zero of=/mirror/repo/bigdata bs=300M count=1

還可以回傳指定資訊
[root@web01 conf.d]# cat bertwu.com.conf
limit_conn_zone $binary_remote_addr zone=req_od:10m;
server {
listen 80;
server_name mirror.bertwu.com;
charset utf-8;
root /mirror;
limit_conn req_od 2;
limit_rate 200k;
# 捕獲503錯誤,內部跳轉做回應處理
error_page 503 @temp;
location @temp {
default_type text/html;
return 200 '請聯系客服充值,然后再次下載';
}
location / {
index index.html;
}
# 提供yum源倉庫目錄
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
4.6 限制下載速度
[root@web01 conf.d]# cat bertwu.com.conf
#limit_conn_zone $binary_remote_addr zone=req_od:10m;
server {
listen 80;
server_name mirror.bertwu.com;
charset utf-8;
root /mirror;
#limit_conn req_od 2;
limit_rate_after 100m; # 先不限速下載100m
limit_rate 200k; # 100m后速度限制為200k/s
location / {
index index.html;
}
# 提供yum源倉庫目錄
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}

4.7 綜合場景實踐
- 限制web服務器請求數處理為1秒一個,觸發值為5,限制用戶僅可同時下載一個檔案,
- 當下載超過100M則限制下載速度為500k
- 如果同時下載超過2個視頻,則回傳提示 “請聯系管理員進行會員充值” 或 跳轉到其他頁面;
[root@web01 conf.d]# cat bertwu.conf
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=req_od:10m;
server {
listen 80;
charset utf-8;
server_name mirror.bertwu2.com;
root /mirror;
limit_rate_after 100m;
limit_rate 500k;
limit_req zone=req_one burst=5 nodelay;
limit_req_status 412;
limit_conn req_od 1;
error_page 503 500 502 @error_temp;
location @error_temp {
default_type text_html;
return 302 http://www.jd.com;
}
location / {
index index.html;
}
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
5. nginx狀態監控
ngx_http_stub_status_module模塊提供對基本狀態資訊的訪問,默認情況下不集成該模塊,需要使用
--with-http_stub_status_module集成,
語法
Syntax: stub_status;
Default: —
Context: server, location
示例
[root@web01 conf.d]# cat bertwu.conf
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=req_od:10m;
server {
listen 80;
charset utf-8;
server_name mirror.bertwu2.com;
root /mirror;
limit_rate_after 100m;
limit_rate 500k;
limit_req zone=req_one burst=5 nodelay;
limit_req_status 412;
limit_conn req_od 1;
error_page 503 500 502 @error_temp;
location @error_temp {
default_type text_html;
return 302 http://www.jd.com;
}
location / {
index index.html;
}
location /repo {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
location /nginx_status {
stub_status;
allow 10.0.0.1/32;
deny all;
}
}

# Active connections:當前活躍連接數,包括Waiting等待連接數,
# accepts: 已接收的總TCP連接數量,
# handled: 已處理的TCP連接數量,
# requests: 當前總http請求數量,
# Reading: 當前讀取的請求頭數量,
# Writing: 當前回應的請求頭數量,
# Waiting: 當前等待請求的空閑客戶端連接數,
如何理解Reading、Writing、Waiting?
假設現在有兩條船分別為C 、S,C船需要 S船的1個物品,那么此時C船就要給S船發送一個訊息,
1、S船收到這個訊息時就是reading
2、S船將物資發送給C船,這個時候就是writing
3、如果C船需要S船很多個物品,那么需要C船和S船建立起一個物資傳送管道,不斷的傳送物資,這個管道建立起來的時候,就是waiting狀態了,
6 Nginx資源快取
- 瀏覽器快取設定用于提高網站性能,尤其是新聞網站,圖片一旦發布,改動的可能是非常小的,所以我們希望能否用戶訪問一次后,圖片快取在用戶的瀏覽器長時間快取,
- 瀏覽器是有自己的快取機制,它是基于HTTP協議快取機制來實作的,在HTTP協議中有很多頭(Headers)資訊,那么實作瀏覽器的快取就需要依賴特殊的頭資訊來與服務器進行特殊的驗證,如: Expires (http/1.0) ;Cache-control (http/1.1) Nginx靜態資源快取參考
6.1 瀏覽器無快取

6.2 瀏覽器有快取

6.3 快取過期校驗
- 瀏覽器請求服務器會先進行Expires、Cache-Control的檢查,檢查快取是否過期,如果沒有過期則直接從快取檔案中讀取,
- 如果快取過期,首先檢查是否存在etag,如果存在則會客戶端會向web服務器請求if-None-Match,與etag值進行比對,由服務器決策回傳200還是304,
- 如果etag不存在,則進行last-Modified檢查,客戶端會向web服務器請求If-Modified-Since,與last-Modified進行比對,由服務器決策回傳200還是304,
深入理解瀏覽器快取機制
6.4 快取配置語法
作用: 添加Cache-Control, Expires頭
Syntax: expires [modified] time;或者expires epoch | max | off;
Default: expires off;
Context: http, server, location, if in location
[root@web01 conf.d]# cat cache.conf
server {
listen 80;
server_name www.bertwu.com;
root /test;
charset utf-8;
location / {
index index.html;
}
location ~ .*\.(png|jpg|jpeg|gif)$ {
expires 1d;
}
}
如果開發代碼沒有正式上線時,希望靜態檔案不被快取
location ~ .*\.(css|js|swf|json|mp4|htm|html)$ {
add_header Cache-Control no-store;
add_header Pragma no-cache;
}
}
7. Nginx資源壓縮
Nginx將發送至客戶端之前的資料進行壓縮,然后傳輸,這能夠有效地節約帶寬,并提高回應速度;
對圖片的壓縮不顯著,對文本的壓縮比較顯著,

7.1 配置語法
1、啟用或關閉gzip壓縮
Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in location
2、gzip支持的壓縮型別
Syntax: gzip_types mime-type ...;
Default: gzip_types text/html image/gif ; # 在/etc/nginx/mime.types檔案中添加需要的型別
Context: http, server, location
3、gzip壓縮比率,壓縮率越高,CPU消耗越大,范圍1--9
Syntax: gzip_comp_level level;
Default: gzip_comp_level 1;
Context: http, server, location
4、gzip壓縮的最小檔案,小于設定的值檔案將不會被壓縮(由"Content-Length"回應頭欄位確定)
Syntax: gzip_min_length length;
Default: gzip_min_length 20;
Context: http, server, location
5、gzip壓縮支持的協議版本
Syntax: gzip_http_version 1.0 | 1.1;
Default: gzip_http_version 1.1;
Context: http, server, location
6、啟用壓縮,是否在回應報文首部插入"Vary: Accept-Encoding"
Syntax: gzip_vary on | off;
Default: gzip_vary off;
Context: http, server, location
壓縮建議區域設定,因為請求的型別不同,壓縮比率也會不同,所以不建議全域設定
7.2圖片壓縮
server {
listen 80;
server_name www.bertwu.com;
root /test;
charset utf-8;
location / {
index index.html;
}
location ~ .*\.(png|jpg|jpeg|gif)$ {
expires 1d;
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_min_length 20k;
gzip_types image/jpeg image/gif image/png;
gzip_vary on;
}
}
7.3 文本檔案壓縮
server {
listen 80;
server_name www.bertwu.com;
root /test;
charset utf-8;
location / {
index index.html;
}
location ~ .*\.(txt|pdf)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_min_length 20k;
gzip_types text/plain application/pdf;
gzip_vary on;
}
}
二、 Nginx Location
2.1 什么是Location
Location用來控制訪問網站的uri路徑;
2.2 語法
location [ = | ~ | ~* | ^~ ] uri { ... }
# 匹配符 匹配規則 優先級(數字越小優先級越高)
# = 精確匹配 1
# ^~ 以某個字串開頭 2
# ~ 區分大小寫的正則匹配 3
# ~* 不區分大小寫的正則匹配 4
# / 通用匹配,任何請求都會匹配到 5
location uri 添加 / 和不添加 / 的區別?
#不添加/,默認上/code/test目錄下找index.html檔案,如果沒有 index.html則會查找/code/test檔案
location /test {
root /code;
index index.html;
}
#添加/,默認上/test目錄下尋找index.html檔案,如果沒有index.html 則會直接回傳404
location /test/ {
root /code;
}
#示例測驗代碼
[root@web01 code]# cat /etc/nginx/conf.d/uri.oldxu.net.conf
server {
listen 80;
server_name uri.oldxu.net;
root /code;
# location /test {
# index index.html;
# }
location /test/ {
index index.html;
}
}
2.3 Location優先級示例
[root@web01 conf.d]# cat location.conf
server {
listen 80;
server_name location.bertwu.com;
charset utf-8;
location =/ {
default_type text/html;
return 200 'location = /';
}
location / {
default_type text/html;
return 200 'location /';
}
location /documents/ {
default_type text/html;
return 200 'location /documents';
}
location ^~ /images/ {
default_type text/html;
return 200 'location ^~ /images';
}
location ~* \.(gif|jpg|jpeg)$ {
default_type text/html;
return 200 'location ~* \.(gif|jpg|jpeg)';
}
}
測驗結果如下,建議用curl測驗
1.請求 location.bertwu.com,會被location = /匹配
[root@web01 conf.d]# curl -H Host:location.bertwu.com http://10.0.0.7
location = /
2.請求 location.bertwu.com/index.html,會被location /匹配
[root@web01 conf.d]# curl -H Host:location.bertwu.com http://127.0.0.1/index.html
location /
3.請求 location.bertwu.com/documents/,會被location /documents匹配
[root@web01 conf.d]# curl -H Host:location.bertwu.com http://127.0.0.1/documents/
location /documents
4.請求 location.bertwu.com/images/1.gif,會被location ^~ /images匹配
[root@web01 conf.d]# curl -H Host:location.bertwu.com http://172.16.1.7/images/1.gif
location ^~ /images
5.請求 location.bertwu.com/documents/1.jpg,會被location ~* \.(gif|jpg|jpeg)匹配,因為 /documents 這種匹配是通用匹配,優先級低
root@web01 conf.d]# curl -H Host:location.bertwu.com http://172.16.1.7/documents/1.jpg
location ~* \.(gif|jpg|jpeg)
2.4 應用場景示例
[root@web01 conf.d]# cat location1.conf
server {
listen 80;
server_name location1.bertwu.com;
charset utf-8;
root /code;
# 通用匹配,任何請求都會匹配到
location / {
index index.html;
}
# 精準匹配,必須請求的uri是/nginx_status才回傳服務器狀態
location = /nginx_status {
stub_status;
}
# 嚴格區分大小寫,匹配以.php結尾的都走這個location
location ~ \.php$ {
default_type text/html;
return 200 'php訪問成功';
}
# 嚴格區分大小寫,匹配以.jsp結尾的都走這個location
location ~ \.jps$ {
default_type text/html;
return 200 'jps訪問成功';
}
# 不區分大小寫匹配,只要用戶訪問.jpg,gif,png,js,css 都走這條location
location ~* \.(gif|jpg|jpeg|css)$ {
expires 3d;
default_type text/html;
return 200 'location ~* \.(gif|jpg|jpeg)';
}
# 不區分大小寫匹配,禁止訪問下面內容
location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
deny all;
}
}
模擬瀏覽器訪問結果
[root@web01 code]# curl -H Host:location1.bertwu.com http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
218 218 234
Reading: 0 Writing: 1 Waiting: 0
[root@web01 code]# curl -H Host:location1.bertwu.com http://127.0.0.1/1.php
php訪問成功
[root@web01 code]# curl -H Host:location1.bertwu.com http://127.0.0.1/1.jps
jps訪問成功
[root@web01 code]# curl -H Host:location1.bertwu.com http://127.0.0.1/1.jpg
location ~* \.(gif|jpg|jpeg)
[root@web01 code]# curl -H Host:location1.bertwu.com http://127.0.0.1/1.git
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
2.5 Location @重定向
location @name 這樣的location不用于常規請求處理,而是用于內部重定向,
重定向有兩種方式
1.重定向到指定的頁面
error_page 404 /error.html 表示跳轉到指定頁面 /表示root指定的路徑
2.error_page 404 @error_404 表示跳轉到內部location 可以回傳頁面,也可以跳轉到具體頁面,
1.跳轉到自定義頁面
[root@web01 conf.d]# cat error.conf
server {
listen 80;
server_name error.test.com;
root /error;
charset utf-8;
location / {
index index.html;
}
error_page 400 403 404 /error.html;
}
[root@web01 conf.d]# mkdir /error
[root@web01 conf.d]# echo "首頁" > /error/index.html
[root@web01 conf.d]# echo "我是自定義錯誤頁面" > /error/error.html
[root@web01 conf.d]# curl -H Host:error.test.com http://127.0.0.1
首頁
[root@web01 conf.d]# curl -H Host:error.test.com http://127.0.0.1/asdfsff
我是自定義錯誤頁面
- 回傳文本文字
[root@web01 conf.d]# cat error.conf
server {
listen 80;
server_name error.test.com;
root /error;
charset utf-8;
location / {
index index.html;
}
error_page 400 403 404 @error;
location @error {
default_type text/html;
return 200 '糟糕,頁面不小心走丟了!';
}
}
- 跳轉到首頁或其他頁面
[root@web01 conf.d]# cat error.conf
server {
listen 80;
server_name error.test.com;
root /error;
charset utf-8;
location / {
index index.html;
}
error_page 400 403 404 @error;
location @error {
default_type text/html;
return 302 http://$server_name;
#return 302 http://error.test.com;
#return 302 http://www.jd.com; # 跳轉到京東
}
}
三、 Nginx 日志模塊
Nginx的日志記錄非常靈活,可以通過log_format來定義格式,
3.1 日志格式語法
# 配置語法: 包括: error.log access.log 兩個檔案
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http
# 默認Nginx定義語法格式如下
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; # 宣告
示例:
root@web01 ~]# tail -1 /var/log/nginx/access.log
10.0.0.1 - - [14/Aug/2021:16:56:26 +0800] "GET /dsfsdf HTTP/1.1" 302 145 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36" "-"
可以自定義日志格式
log_format test '$remote_addr - $remote_user [$time_local] "$request" ' '$status';
/var/log/nginx/access_test.log test; # 宣告
3.2 Nginx日志格式中常用的變數
$remote_addr # 記錄客戶端IP地址
$remote_user # 記錄客戶端用戶名
$time_local # 記錄通用的本地時間
$time_iso8601 # 記錄ISO8601標準格式下的本地時間
$request # 記錄請求的方法以及請求的http協議
$status # 記錄請求狀態碼(用于定位錯誤資訊)
$body_bytes_sent # 發送給客戶端的資源位元組數,不包括回應頭的大小
$bytes_sent # 發送給客戶端的總位元組數
$msec # 日志寫入時間,單位為秒,精度是毫秒,
$http_referer # 記錄從哪個頁面鏈接訪問過來的, '-'表示直接訪問,沒有經過任何跳轉
$http_user_agent # 記錄客戶端瀏覽器相關資訊
$http_x_forwarded_for #記錄客戶端IP地址,沒有經過反向代理為'-'
$request_length # 請求的長度(包括請求行, 請求頭和請求正文),
$request_time # 請求花費的時間,單位為秒,精度毫秒
# 注:如果Nginx位于負載均衡器,nginx反向代理之后, web服務器無法直接獲取到客戶端真實的IP地址,
# $remote_addr獲取的是反向代理的IP地址, 反向代理服務器在轉發請求的http頭資訊中,
增加http_x_forwarded_for資訊,用來記錄客戶端IP地址和客戶端請求的服務器地址,
3.3 Nginx訪問日志
1.web服務器的訪問日志是非常重要的,我們可以通過訪問日志來分析用戶的訪問情況,也可以通過訪問日志發現一些例外訪問,access_log日志配置語法,
如果服務器有多個站點server ,應該對每個server配置對應的日志,而不應該在location中配置日志,因為沒必要,如果把所有站點日志配置在http中會顯得特別混亂,
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off; # 關閉日志命令
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
示例
http {
access_log /var/log/nginx/access.log main; # http層全域log所有都會記錄這里
server {
access_log /var/log/nginx/bertwu1.log main # 站點1的日志路徑
location / {
...
}
}
server {
access_log /var/log/nginx/bertwu2.log main # 站點2的日志路徑
location / {
}
location /admin {
.....
}
}
}
日志記錄規則,先區域,后全域,區域若定義了日志,只在區域生效,否則往上層找,直到找到為止,location層定義了,就在location層,否則去server層,如果這兩層都沒定義,就去找http層,
3.4 Nginx錯誤日志
Nginx常見的錯誤日志級別有debug | info | notice | warn | error | crit | alert | emerg級別越高記錄的資訊越少,如果不定義,默認級別為warn,它可以配置在main、http、server、location段里
Nginx錯誤日志示例
error_log /var/log/nginx/error.log warn;
# 關閉錯誤日志error_log /dev/null;
3.5 Nginx日志過濾
個網站會包含很多元素,尤其是有大量的images、js、css等靜態資源,這樣的請求可以不用記錄日志,
#請求favicon.ico時,不記錄日志
location /favicon.ico {
access_log off;
expires 365d;
return 200;
}
#當有人訪問gif、png等資源時,將日志丟入空
location ~* .*\.(gif|jpg|png|css|js)$ {
access_log /dev/null;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/294178.html
標籤:其他
