nginx的模塊有官方模塊和第三方模塊之分。
我們?cè)诮K端中可以查看nginx加載的模塊有哪些
--with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
http_stub_status_module模塊講解:
編譯選項(xiàng):--with-http_stub_status_module
作用:Nginx的客戶端狀態(tài)
主要用于展示當(dāng)前處理鏈接的狀態(tài)蒜魄,用于監(jiān)控鏈接信息
配置語(yǔ)法:
Syntax:stub_status;
Default:——
Context:server,location
例子:
我們編輯默認(rèn)配置文件猴凹,添加自己的配置
[root@hongshaorou conf.d]# pwd
/etc/nginx/conf.d
[root@hongshaorou conf.d]# vim default.conf
編輯完成檢查配置是否正確
[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
正確之后重啟服務(wù)
[root@hongshaorou conf.d]# nginx -s reload -c /etc/nginx/nginx.conf
重啟服務(wù)之后我們?cè)跒g覽器中輸入對(duì)應(yīng)的url地址
第一行展示的是當(dāng)前活躍連接數(shù)
第二行第一個(gè)數(shù)字表示nginx接受的握手的總次數(shù)猎物,第二個(gè)表示nginx所處理的連接數(shù),最后一個(gè)表示請(qǐng)求數(shù)拯爽。正常情況連接數(shù)和握手?jǐn)?shù)是相等的炕横,表示沒(méi)有丟失。
最后一行分別表示讀寫等待的數(shù)量老厌,最后一個(gè)等待表示開(kāi)啟長(zhǎng)連接時(shí)瘟则,客戶端服務(wù)端既沒(méi)有讀也沒(méi)有寫僅僅建立連接的數(shù)量。
--with-http_random_index_module模塊講解
編譯選項(xiàng):--with-http_random_index_module
作用:主目錄中選一個(gè)隨機(jī)主頁(yè)
配置語(yǔ)法:
Syntax:random_index on | off;
Default:random_index off;
Context:location
例子:生成隨機(jī)頁(yè)面
編輯默認(rèn)配置文件
我們重新設(shè)置了主目錄文件枝秤,并且在主目錄下新建了html文件
[root@hongshaorou code]# pwd
/opt/app/code
[root@hongshaorou code]# ls
1.html 2.html
檢查重啟:
[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hongshaorou conf.d]# systemctl reload nginx
然后在瀏覽器中輸入主機(jī)IP醋拧,多次刷新頁(yè)面就會(huì)在兩個(gè)文件間來(lái)回切換。
注意:雖然nginx會(huì)將主目錄下的文件作為隨機(jī)主頁(yè)淀弹,但是不會(huì)將隱藏文件包括在內(nèi)丹壕,Linux的隱藏文件是指以點(diǎn) . 開(kāi)始的文件。
--with-http_sub_module模塊講解
編譯選項(xiàng):--with-http_sub_module
作用:HTTP內(nèi)容替換
該模塊是用于Nginx服務(wù)端在給客戶端response內(nèi)容的時(shí)候薇溃,進(jìn)行HTTP內(nèi)容更換菌赖。
語(yǔ)法:
Syntax: sub_filter string replacement; (string表示要替換的內(nèi)容,replacement表示替換后的對(duì)象)
Default: —
Context: http, server, location
句法: sub_filter_last_modified on | off;
默認(rèn): sub_filter_last_modified off;
語(yǔ)境: http沐序,server琉用,location```
該模塊用于判斷每次請(qǐng)求的服務(wù)端內(nèi)容是否發(fā)生變化,當(dāng)發(fā)生變化的時(shí)候返回給客戶端策幼,當(dāng)沒(méi)有發(fā)生變化的時(shí)候邑时,不再返回內(nèi)容。重要用于緩存垄惧。
語(yǔ)法: sub_filter_once on | off;
默認(rèn)值: sub_filter_once on;
配置段: http, server, location
字符串替換一次還是多次替換刁愿,默認(rèn)替換一次,例如你要替換響應(yīng)內(nèi)容中的ttlsa為運(yùn)維生存時(shí)間到逊,如果有多個(gè)ttlsa出現(xiàn)铣口,那么只會(huì)替換第一個(gè)滤钱,如果off,那么所有的ttlsa都會(huì) 被替換
詳細(xì)例子參考文章:http://www.ttlsa.com/linux/nginx-modules-ngx_http_sub_module/
教程例子:
首先我們編寫了一個(gè)html文件
[root@hongshaorou code]# ls
1.html 2.html submodule.html
[root@hongshaorou code]# cat submodule.html
<html>
<head>
<meta charset="utf-8">
<title>submodules</title>
</head>
<body>
<a>jeson</a>
<a>at</a>
<a>imooc</a>
<a>jeson</a>
<a>imooc</a>
</body>
</html>
然后在瀏覽器中查看
下面我們替換返回的數(shù)據(jù)
然后重新加載服務(wù)
[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hongshaorou conf.d]# systemctl reload nginx
刷新剛才的網(wǎng)頁(yè)
我們發(fā)現(xiàn)已經(jīng)發(fā)生了替換因?yàn)閟ub_filter_once默認(rèn)開(kāi)啟脑题,因此只替換了一個(gè)件缸。