nginx模塊
- 前言
- 一、常見的nginx web模塊
- 1.隨機主頁 random_index_module
- 2.替換模塊 sub_module
- 3.檔案讀取模塊 ngx_http_core_module
- 4.檔案壓縮 ngx_http_gzip_module
- 5.狀態訪問統計 ngx_http_stub_status_module
- 6.防盜鏈 ngx_http_referer_module
- 7.用戶訪問限制
- 1.連接頻率限制 ngx_http_limit_conn_module
- 2.請求頻率限制 ngx_http_limit_req_module
- 8.用戶訪問控制
- 1.基于主機 ngx_http_access_module
- 2.基于用戶 ngx_http_auth_basic_module
- 二、nginx 新增模塊
前言
nginx模塊是nginx相比于apache一個很大的優點,模塊可以為nginx提供更多的功能來滿足運維日常需求,當有一些需求,我們只需要編譯新的模塊重啟nginx即可完成,
一、常見的nginx web模塊
1.隨機主頁 random_index_module
(1)mkdir /usr/share/nginx/html/random_index
touch /usr/share/nginx/html/random_index/{blue.html,green.html,red.html,yellow.html}
<html>
<head>
<title>color</title>
</head>
<body style="background-color:blue">
<h1>color!</h1>
</body>
</html>
(2)vim /etc/nginx/conf.d/liang.conf
server{
listen 192.168.93.136:8080;
server_name web.liang.com;
location /{
#root /usr/share/nginx/html;
#index liang.html;
root /usr/share/nginx/random_index;
random_index on;
}
}
(3)訪問 192.168.93.136:8080 并重繪 查看網頁變化
2.替換模塊 sub_module
Syntax: sub_filter A B; # 網頁中的A替換成B
Default: —
Context: http, server, location
Syntax: sub_filter_once on | off;
Default: sub_filter_once on;
Context: http, server, location
server{
listen 192.168.93.177:8080;
server_name web.kong.com;
location /{
root /usr/share/nginx/html;
index kong.html index.html;
sub_filter kong ''; # 把kong替換為空
#sub_filter_once on; # 只替換匹配到的第一個kong
sub_filter_once off; # 全部替換
}
}
3.檔案讀取模塊 ngx_http_core_module
Syntax: sendfile on | off; # 加快網路傳輸速率
Default: sendfile off;
Context: http, server, location, if in location
未使用sendfile網路傳輸 程序:
硬碟 >> kernel buffer >> user buffer>> kernel socket buffer >>協議堆疊
使用sendfile網路傳輸 程序:
硬碟 >> kernel buffer (快速拷貝到kernelsocket buffer) >>協議堆疊
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location
未使用tcp_nopush網路資源:
tcp傳輸資料會帶一個40位元組長的包頭,應用程式每次操作都會發送一個包,每個包都會加包頭
于是產生4000%的過載,很輕易地就能令網路發生擁塞
使用tcp_nopush網路資源:
不會每次操作都會發送一個包,會等待多個包一起發送,只用一個包頭
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location
資料包立即發送出去,由于Nagle和DelayedACK的原因,資料包的確認資訊需要積攢到兩個時才發送,長連接情況下,奇數包會造成延時40ms,所以tcp_nodelay會將ack立刻發出去
4.檔案壓縮 ngx_http_gzip_module
啟動該模塊,使檔案傳輸前進行壓縮,提升傳輸效率
Syntax: gzip on | off; # 開啟或關閉壓縮模塊
Default: gzip off;
Context: http, server, location, if in location
Syntax: gzip_comp_level level; # 設定壓縮級別
Default: gzip_comp_level 1;
Context: http, server, location
在nginx.conf的http模塊添加
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_static on;
壓縮包和圖片類物件本身已經自帶壓縮功能,所以壓縮比例較小低,文本類物件在壓縮試驗中,壓縮比例體現優越,
5.狀態訪問統計 ngx_http_stub_status_module
server{
location ~ /status { # 啟動該模塊
stub_status on;
access_log off;
}
}
status頁面:

102為連接總數 102為成功連接的數量 289為共處理的請求數
Reading: 0 讀取客戶端Header的資訊數 請求頭
Writing: 1 回傳給客戶端的header的資訊數 回應頭
Waiting: 1 等待的請求數,開啟了keepalive 這個值等于active - (reading + writing)
keepalive_timeout
httpd守護行程,因為網頁每次訪問都需要tcp三次握手建立連接,然后http建立請求頁面,最后tcp四次揮手斷開連接
keepalive_timeout 設定的時間是當tcp建立連接的時候不立刻斷開連接 而是等待一段時間
防止客戶端和服務端頻繁的建立和端來連接而造成的資源浪費
vim /etc/nginx/nginx.conf
keepalive_timeout 65; # 默認引數 等待65s
改為:keepalive_timeout 0;
更改完配置后再次訪問status頁面,會發現Reading、Writing、Waiting次數一樣,說明nginx每處理完一次請求都會斷開,
6.防盜鏈 ngx_http_referer_module
Syntax: valid_referers none | blocked | server_names | string …;
Default: —
Context: server, location
(1)盜鏈模擬
創建兩個虛擬主機 kong.conf liang.conf
server{
listen 80;
server_name web.kong.com;
location /{
access_log /var/log/nginx/kong.access.log main; # 訪問該界面會生成日志
root /usr/share/nginx/html;
index kong.html index.html;
}
}
server{
listen 80;
server_name web.liang.com;
location /{
access_log /var/log/nginx/liang.access.log main;
root /;
index liang.html;
}
}
vim kong.html
<img src='timg.jpg' />
vim liang.html
<img src='http://web.kong.com/timg.jpg' /> # 用連接來盜取kong站點的timg.jpg,liang站點沒有圖片但是可以看到圖片
分別訪問兩個站點查看兩個日志
當訪問web.kong.com kong.access.log 日志顯示正常
當訪問web.liang.com liang.access.log 日志顯示正常 但是kong.access.log 也會看到重繪
并且可以從$http_referer看出是從哪個連接跳過來 說明網站被盜鏈了
192.168.93.136 - - [17/Nov/2018:16:39:14 +0800] “GET /timg.jpg HTTP/1.1” 200 27361 “http://web.liang.com/” “Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0” “-”
(2)開啟防盜鏈
方法1
在location添加
valid_referers none blocked *.kong.com; # web.kong.com開啟防盜鏈 此時web.liang.com不能看到圖片
if ($invalid_referer) {
return 403;
}
方法2
location ~* \.(gif|jpg|png|bmp)$ {
root /usr/share/nginx/html; # 一定要重新定義網站根目錄 否則圖片顯示不出來
valid_referers none blocked *.kong.com server_names ~\.liangfaqi\.;
if ($invalid_referer) {
return 403;
#rewrite .* http://web.kong.com/1.jpg;
}
}
7.用戶訪問限制
1.連接頻率限制 ngx_http_limit_conn_module
對于大流量惡意的攻擊訪問,會帶來帶寬的浪費,服務器壓力,影響業務,往往考慮對同一個ip的連接數,并發數進行限制,
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
開啟連接頻率限制,示例:
設定一個快取區保存不同key的狀態,大小10m,使用遠程ip來作為key,以此限制每個源IP的鏈接數
http {
limit_conn_zone $binary_remote_addr zone=one:10m; # 定義在http內 server外
}
server {
location / {
...
limit_conn one 2; # 定義每個IP的并發連接數量,特別注意這里最高只能限制到2,高了無效
}
}
# 測驗
yum install -y httpd-tools # 壓力測驗工具
ab -n 100 -c 10 http://web.kong.com/ # 訪問100次 分10次訪問
Time taken for tests: 0.026 seconds
Complete requests: 100
Failed requests: 0
2.請求頻率限制 ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
啟動請求頻率限制,示例:
設定一個快取區reqps保存不同key的狀態,大小10m,這里的狀態是指當前的過量請求數,
http { # 定義在http里面,server的外面
...
limit_req_zone $binary_remote_addr zone=reqps:10m rate=5r/s; # 定義每個IP的每秒請求數量,限制每秒5個連接請求,不延遲
}
server {
location / {
...
limit_req zone=reqps; # 參考
# burst=5 表示最大延遲請求數量不大于5, 如果太過多的請求被限制延遲是不需要的 ,這時需要使用nodelay引數,服務器會立刻回傳503狀態碼,
limit_req zone=reqps burst=5; # 參考限制,但是令牌桶有5個,有延遲,速度慢
limit_req zone=reqps burst=5 nodelay; # 參考限制,但是令牌桶有5個,無延遲,速度快
}
}
yum install -y httpd-tools # 壓力測驗工具
ab -n 100 -c 10 http://web.kong.com # 訪問100次 分10次訪問
Time taken for tests: 0.041 seconds
Complete requests: 100
Failed requests: 99
查看錯誤日志:
2018/11/17 17:55:48 [error] 10697#10697: *100 limiting requests, excess: 0.800 by zone “reqps”, client: 192.168.93.136, server: web.kong.com, request: “GET / HTTP/1.0”, host: “web.kong.com”
8.用戶訪問控制
1.基于主機 ngx_http_access_module
Syntax: allow address | CIDR | unix: | all;
Context: http, server, location, limit_except
server {
allow 10.18.47.64; # 允許訪問的主機
deny all;
}
2.基于用戶 ngx_http_auth_basic_module
htpasswd -cm /etc/nginx/conf.d/passwd user1 # 創建user1 并且創建passwd檔案 -c只需要第一次沒有檔案時使用
htpasswd -m /etc/nginx/conf.d/passwd user2 # 創建user2
location /{
access_log /var/log/nginx/kong.access.log main;
root /usr/share/nginx/html;
index kong.html index.html;
auth_basic "nginx access test!";
auth_basic_user_file /etc/nginx/conf.d/passwd;
}
訪問web.kong.com 查看結果
二、nginx 新增模塊
- 查看nginx已安裝的模塊
./sbin/nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
- 下載需要安裝的模塊 以nginx-module-vts模塊為例
git clone git://github.com/vozlt/nginx-module-vts.git
pwd
/root/nginx-1.20.1/nginx-module-vts
ls
Changes config LICENSE README.md share src t util
- 備份舊的nginx二進制檔案
pwd
/usr/local/nginx/sbin
mv nginx nginx.bak
- 重新編譯添加新增模塊 —add-module
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-1.20.1/nginx-module-vts

5. 編譯完成之后,將新生成的二進制檔案拷貝到nginx目錄下
make
*注:這里不要執行make install,我們只需要通過make將新增模塊編譯進來,生成新的二進制檔案,在objs目錄下,千萬不要執行make instlall ,make install 會重新安裝nginx,覆寫原有nginx
pwd
/root/nginx-1.20.1/objs
ls
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
cp nginx /usr/local/nginx/sbin/
- 檢查模塊,并重啟nginx
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-1.20.1/nginx-module-vts
- 測驗新安裝的模塊
新增nginx配置:
http {
vhost_traffic_status_zone;
...
server {
location /status {
listen 81;
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}
重啟nginx
/usr/local/nginx/sbin/nginx -s reload

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/304491.html
標籤:其他
