nginx使用-2(模組和日誌)
默認官方模組
1.1、Gzip壓縮
壓縮文件,使文件變小,傳輸更快了。目前市場上大部分瀏覽器是支援GZIP的。IE6以下支援不好,會出現亂碼情況。
官方文檔://nginx.org/en/docs/http/ngx_http_gzip_module.html
示例語法:
#配置到http段里,使整個http服務都啟用gzip壓縮
#開啟gzip壓縮
gzip on;
#http協議版本
gzip_http_version 1.0;
#IE瀏覽器不開啟gzip IE6以下會亂碼
gzip_disable 'MSIE [1-6].';
#開啟gzip 文件的格式
gzip_types image/jpeg image/jpg image/png text/plain text/css;
驗證文件是否開啟gzip
1.2、客戶端快取
B/S架構里 browser瀏覽器 就是客戶端
告知瀏覽器獲取的資訊是在某個區間時間段是有效的。
官方文檔://nginx.org/en/docs/http/ngx_http_headers_module.html#expires
示例語法:
location ~ \.(js|css)$ {
#單位參數 d day 天|H hour 小時 M 分
expires 1h;
}
#在整個http中生效 配置到http段里
expires 1h
1.3、基於IP的訪問控制
基於ngx_http_access_module模組,默認可使用
官方文檔://nginx.org/en/docs/http/ngx_http_access_module.html
語法:
deny ip 禁止ip訪問
allow ip 允許訪問
1.4、基於用戶的訪問控制
基於ngx_http_auth_basic_module模組,默認可用
官方文檔://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
語法:
auth_basic “提示資訊”
auth_basic_user_file /etc/nginx/htpasswd;
配置實現:
①創建用戶名和密碼存儲文件
[root@server01 ~] # cd /usr/local/nginx/conf
#htpasswd 如果不存在就通過 yum -y install httpd-tools安裝
#生成用戶名稱和密碼
[root@server01 ~] # htpasswd -c ./passwd.db lnmp
#輸入密碼並再次確認密碼
#查看passwd.db文件是否創建成功
②在配置文件中進行配置
[root@server01 ~] # vim /usr/local/nginx/conf/nginx.conf
配置文件內容
#根據業務需求,配置到server段里
#登錄框顯示的標題提示
auth_basic "test login"
#載入用戶名稱和密碼校驗文件
auth_basic_user_file /usr/local/nginx/conf/passwd.db;
③測試查看
1.5、目錄列表顯示
顯示文件列表,或者需要做一個下載列表
官方文檔://nginx.org/en/docs/http/ngx_http_autoindex_module.html#autoindex
示例語法:
#開啟目錄列表顯示
autoindex on;
#index 當index默認找不到時,才會使用目錄列表
index index;
注意:如果目錄中沒有配置的默認index訪問項,而autoindex又沒有開啟,不能夠查看訪問目錄列表,就會報出403錯誤。
1.6、反向代理
正向代理
特點:知道自己使用了代理,需要填寫代理伺服器的IP等相關連接資訊
常見於代理客戶端上網等操作。
反向代理
特點:用戶是無感知的,不知道使用了代理伺服器。反向代理伺服器是和真實訪問的伺服器是在一起的,有關聯的。
作用:可以根據實際業務需求,分發代理頁面到不同的解釋器
可以隱藏真實伺服器的路徑
常見於代理後端伺服器
官方文檔://nginx.org/en/docs/http/ngx_http_proxy_module.html
①配置反向代理
LNMPA
驗證例子:
①安裝httpd 需改埠8080
#安裝apache
[root@server01 ~] # yum install -y httpd
#配置apache的配置文件
[root@server01 ~] # vim /etc/httpd/conf/httpd.conf
修改配置項
listen 8080
②配置nginx的server並進行轉發
location / {
proxy_pass http://127.0.0.1:8080;
}
日誌管理
日誌類型:
①access.log 訪問日誌 查看統計用戶的訪問資訊 流量
②error.log 錯誤日誌 錯誤資訊 重寫資訊
1、訪問日誌
官方文檔://nginx.org/en/docs/http/ngx_http_log_module.html
①查看access.log
[root@server01 ~] # cd /usr/local/nginx/logs
[root@server01 ~] # cat access.log
access.log日誌文件內容示例
127.0.0.1 - - [06/Oct/2017:11:46:16 +0800] "GET /phpinfo.php HTTP/1.1" 200 25206 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36"
②查看配置解析參數說明
[root@server01 ~] # vim nginx.conf
查看訪問日誌相關參數
#定義日誌格式 格式命名 詳細格式參數
#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 logs/access.log main;
參數 | 意義 |
---|---|
$remote_addr | 客戶端的ip地址(代理伺服器,顯示代理服務ip) |
$remote_user | 用於記錄遠程客戶端的用戶名稱(一般為「-」) |
$time_local | 用於記錄訪問時間和時區 |
$request | 用於記錄請求的url以及請求方法 |
$status | 響應狀態碼,例如:200成功、404頁面找不到等。 |
$body_bytes_sent | 給客戶端發送的文件主體內容位元組數 |
$http_user_agent | 用戶所使用的代理(一般為瀏覽器) |
$http_x_forwarded_for | 可以記錄客戶端IP,通過代理伺服器來記錄客戶端的ip地址 |
$http_referer | 可以記錄用戶是從哪個鏈接訪問過來的 |
訪問日誌,可以統計分析用戶的流量的相關情況。客情分析
2、錯誤日誌
記錄一些啟動和運行過程中的錯誤資訊
# 定義開啟錯誤日誌 日誌位置 日誌級別
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
官方文檔://nginx.org/en/docs/ngx_core_module.html#error_log
[root@server01 ~] # cat /usr/local/nginx/logs/error.log
格式示例:
2019/06/06 11:42:43 [error] 25356#0: *38 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.17.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.17.220", referrer: "//192.168.17.220/index.php"
3、基於域名日誌分割
①開啟日誌的定義規則
#定義日誌格式 定義http里
log_format mylogs '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
②重啟nginx測試查看
#訪問日誌的存儲路徑配置 調用的日誌格式
#在server段裡面配置 也就是在當前server里的訪問日誌,會被寫入定義的這裡
access_log logs/shop.lnmp.com_access.log mylogs;
日誌切割的方式有很多種:
①基於域名分開存儲
②日誌輪轉 時間段
③自定義腳本 定時檢測大小 根據文件大小進行切割