淺談Nginx相關HTTP雜項模塊(一)
1️⃣ ngx_http_access_module
http_access_module
包含了http的訪問權限控制的一個模塊,前面一節的limit_except
指令裏用過,還可以用在http, server, location等地方。
allow
- 允許訪問指定的網絡或地址。如果unix:指定了特殊值(NGINX1.5.1),則允許訪問所有UNIX域套接字。
deny
- 拒絕訪問指定的網絡或地址。如果unix:指定了特殊值(NGINX 1.5.1),則拒絕所有UNIX域套接字的訪問。
- 示例:
location /admin {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
- 依次檢查規則,直到找到第一個匹配項。在此示例中,僅允許對IPv4網絡 10.1.1.0/16(192.168.1.0/24 不包括地址192.168.1.1)和IPv6網絡進行訪問2001:0db8::/32
2️⃣ ngx_http_auth_basic_module
實現基於用戶的訪問控制,使用basic機制進行用戶認證
- auth_basic
- 使用“ HTTP基本身份驗證”協議啓用用戶名和密碼的驗證。
- 指定的參數用作realm。參數值可以包含變量(1.3.10,1.2.7)。
- 該特殊值off允許取消auth_basic從先前配置級別繼承的指令的效果。
- auth_basic_user_file
- 由
htpasswd
命令(http-tools
提供)實現加密文本文件 - 示例:實現使用basic機制進行用戶認證
apt -y install apache2-utlis # ubuntu的htpasswd工具包
htpasswd -c -m /apps/nginx/conf.d/.auth_passwd user1 # 生成密碼文件
# 修改配置文件
server {
listen 80;
server_name www.studylinux.com;
location / {
root /data/nginx/pc;
location /admin/{
root /data/nginx/;
auth_basic "admin auth";
auth_basic_user_file /apps/nginx/conf.d/.auth_passwd;
}
}
error_page 404 =200 /404.html;
location /404.html {
root /data/nginx/error_page;
}
}
echo "auth passwd" > /data/nginx/admin/index.html
nginx -t
nginx -s reload
3️⃣ ngx_http_stub_status_module
用於輸出nginx的基本狀態信息
- stub_status
- 示例:
location =/basic_status {
stub_status;
allow 172.16.0.0/16;
deny all;
}
- 輸出結果示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
- 輸出信息
Active connections
: 活動狀態的連接數
accepts
:已經接受的客戶端請求的總數
handled
:已經處理完成的客戶端請求的總數
requests
:客戶端發來的總的請求數
Reading
:正在讀取客戶端請求報文首部的連接的連接數
Writing
:正在向客戶端發送響應報文過程中的連接數
Waiting
:正在等待客戶端發出請求的空閒連接數
4️⃣ ngx_http_log_module
指定http訪問日誌格式和路徑的模塊
▶ 1.log_format:定義日誌格式
- string可以使用nginx核心模塊及其它模塊內嵌的變量。
- Nginx內嵌變量:http://nginx.org/en/docs/varindex.html
- nginx主配置文件裏面有示例
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
- 參數分析
$remote_addr
:指的就是客戶端地址。
$remote_user
:Basic認證時候的用戶名
[$time_local]
: 中括號括起來的本地時間(通用的日誌格式的那種)
$request
:request URI
$status
:響應碼
body_bytes_sent
:返回的body大小
http_referer
:跳轉鏈接,從哪個網頁跳轉過來,可以用來看外鏈。
http_user_agent
:用戶瀏覽器類型
http_x_forwarded_for
:簡稱XFF頭,它代表客戶端,也就是HTTP的請求端真實的IP,在通過了HTTP 代理或者負載均衡服務器時會添加該項。 - httpd的日誌格式默認使用的combined模式,combined格式的默認定義爲:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
- 這裏寫了各種參數代表的意義:http://blog.csdn.net/hytfly/article/details/11209909
- 官方詳細參數解析:http://httpd.apache.org/docs/2.4/mod/mod_log_config.html#formats
▶ 2.accsess_log:定義日誌路徑 格式,日誌模式
- 可以在不同的區域自定義訪問日誌文件路徑和文件名,還有日誌格式,以及相關的緩衝的配置
▶ 3.自定義json格式日誌
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",' '"clientip":"$remote_addr",'
'"size":$body_bytes_sent,' '"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
access_log /apps/nginx/logs/access_json.log access_json;
open_log_file_cache
- 緩存各日誌文件相關的元數據信息。
max
:緩存的最大文件描述符數量
min_uses
:在inactive指定的時長內訪問大於等於此值方 可被當作活動項
inactive
:非活動時長
valid
:驗正緩存中各緩存項是否爲活動項的時間間隔
5️⃣ ngx_http_gzip_module
gzip 是否開啓壓縮功能
gzip_comp_level: gzip壓縮級別1-9,平時用3-5
gzip disable: 禁止IE6之類的瀏覽器壓縮
gzip_min_length: gzip壓縮的最⼩⽂件,⼩於設置值的⽂件將不會壓縮
gzip_buffers: 支持實現壓縮功能時爲其配置的緩衝區數量及每個緩存區的大小。
gzip_proxied:nginx作爲代理服務器接收到從被代理服務器發送的響應報文後,在何種條件下啓用壓縮功能的。
off
:對代理的請求不啓用。no-cache
,no-store
,private
:表示從被代理服務器收到的響應報文首部的Cache-Control
的值爲此三者中任何一個,則啓用壓縮功能。
gzip_types mime-type: 壓縮過濾器,僅對此處設定的MIME類型的內容啓用壓縮功能。
gzip_vary on | off: 如果啓⽤壓縮,是否在響應報⽂⾸部插⼊Vary: Accept-Encoding
- 示例:開啓壓縮,響應報文首部插入“Vary: Accept-Encoding”,支持壓縮html, js, css, txt文件
server {
listen 80;
server_name www.studylinux.com;
location / {
root /data/nginx/pc;
gzip on;
gzip_comp_level 3;
gzip_min_length 32;
gzip_types text/css application/javascript text/plain;
gzip_vary on;
}
nginx -t
nginx -s reload
# 製作txt文件要大於配置文件要求 否則壓縮不啓用 上述配置32K
# 開始測試
root@ubuntu1804-31:~# curl -I www.studylinux.com/ceshi.txt
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 06 Jan 2020 14:04:13 GMT
Content-Type: text/plain
Last-Modified: Mon, 06 Jan 2020 14:03:54 GMT
Connection: keep-alive
Vary: Accept-Encoding # 響應報文
ETag: W/"5e133e4a-2504ba"
Content-Encoding: gzip # 啓用壓縮傳輸