第九章:Nginx常用模塊

第一節(jié):索引目錄模塊

1.應(yīng)用場景:

  • 簡易下載
  • 內(nèi)網(wǎng)YUM倉庫,配合其他軟件

2.參數(shù)解釋:

官方模塊說明:
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

  • autoindex on;
    打開索引模式
  • autoindex 常用參數(shù)
  • autoindex_exact_size off;
    默認(rèn)為 on, 顯示出文件的確切大小,單位是 bytes。
    修改為 off,顯示出文件的大概大小,單位是 kB 或者 MB 或者 GB。
  • autoindex_localtime on;
    默認(rèn)為 off,顯示的文件時(shí)間為 GMT 時(shí)間。
    修改為 on, 顯示的文件時(shí)間為文件的服務(wù)器時(shí)間。
  • charset utf-8,gbk;
    默認(rèn)中文目錄亂碼,添加上解決亂碼

3.操作步驟:編寫nginx自配置文件

[root@web01 /data/yum]# cat /etc/nginx/conf.d/index.conf 
server {
    listen       80;
    server_name  yum.mysun.com;
    location / {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;
        charset utf-8,gbk;
        root   /data/yum;
        index  index.html index.htm;
    }
}

4.創(chuàng)建數(shù)據(jù)目錄

mkdir /data/yum

5.下載測試軟件

yum install --downloadonly --downloaddir=/data/yum mariadb screen -y
echo "biubiubiubiu" >test.txt
echo "biubiubiubiu" >test.txt

第二節(jié):狀態(tài)監(jiān)控

1.字段解釋

  • Active connections # 當(dāng)前活動(dòng)的連接數(shù)
  • accepts # 當(dāng)前的總連接數(shù) TCP
  • handled # 成功的連接數(shù) TCP
  • requests # 總的 http 請求數(shù)
  • Reading # 請求
  • Writing # 響應(yīng)
  • Waiting # 等待的請求數(shù),開啟了 keepalive

注意, 一次 TCP 的連接,可以發(fā)起多次 http 的請求, 如下參數(shù)可配置進(jìn)行驗(yàn)證

  • keepalive_timeout 0; # 類似于關(guān)閉長連接
  • keepalive_timeout 65; # 65s 沒有活動(dòng)則斷開連接

2.配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/status.conf 
server {
   listen 80;
   server_name  status.oldboy.com;
   stub_status on;
   access_log off;
}

3.測試訪問

[root@web01 ~]# curl status.oldboy.com
Active connections: 1 
server accepts handled requests
 4 4 6 
Reading: 0 Writing: 1 Waiting: 0 

第三節(jié):基于用戶名密碼訪問控制

1.安裝httpd-tools工具

yum install httpd-tools -y 

2.生成密碼文件

htpasswd -b -c /etc/nginx/auth_conf admin admin

3.配置nginx配置文件

server {
    listen       80;
    server_name  yum.mysun.com;
    location / {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;
        charset utf-8,gbk;
        auth_basic "mysunnnnn!";
        auth_basic_user_file auth_conf;
        root   /data/yum;
        index  index.html index.htm;
    }
}

4.檢查并重啟nginx

nginx -t 
systemctl restart nginx 

第四節(jié):基于IP訪問控制

1.命令解釋

http://nginx.org/en/docs/http/ngx_http_access_module.html

2.參數(shù)配置

#拒絕10網(wǎng)段,允許其他網(wǎng)段
deny 10.0.0.0/24 ;
allow all; 

#拒絕10.0.0.1訪問,允許其他訪問
deny 10.0.0.1 ;
allow all;

#只允許172網(wǎng)段訪問
allow 172.16.1.0/24;
deny all;

#只允許172.16.1.7可以訪問
allow 172.16.1.7;
deny all;

第五節(jié):根據(jù)請求限速

1.參數(shù)解釋:

定義一條規(guī)則

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

limit_req_zone          #引用限速模塊
$binary_remote_addr     #判定條件,每個(gè)請求的IP
zone=one:10m            #定義一個(gè)zone名稱
rate=1r/s;              #限制速度,1秒1次

引用一條限速規(guī)則

limit_req zone=two burst=5 nodelay;

limit_req               #引用限速規(guī)則語法
zone=one                #引用哪一條規(guī)則
burst=5                 #令牌桶,允許排隊(duì)的數(shù)量
nodelay;                #如果不希望在請求被限制時(shí)延遲過多的請求,則應(yīng)使用參數(shù)nodelay

2.Nginx配置文件

[root@web01 /etc/nginx/conf.d]# cat index.conf

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_req_zone $binary_remote_addr zone=two:10m rate=2r/s;

server {
    listen       80;
    server_name  yum.mysun.com;
    location / {
        limit_req zone=two burst=5 nodelay;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex_format html;
        charset utf-8,gbk;
        root   /data/yum;
        index  index.html index.htm;
    }
}

3.測試訪問

一秒1次
for i in {1..10000};do curl -I 10.0.0.7;sleep 1;done

一秒2次
for i in {1..10000};do curl -I 10.0.0.7;sleep 0.5;done

一秒5次
for i in {1..10000};do curl -I 10.0.0.7;sleep 0.2;done

相當(dāng)于
for i in {1..10000};
do
curl -I 10.0.0.7;
sleep 1;
done

4.ab壓測工具:

ab -c 10 -n 100 10.0.0.7/
option:-c 并發(fā)數(shù)
       -n 請求總數(shù)

第六節(jié): location匹配

1.配置語法解釋

http://www.itdecent.cn/p/2026360ff272

2.配置參數(shù)

[root@web01 ~]# cat /etc/nginx/conf.d/index.conf

server {
    listen       80;
    server_name  yum.mysun.com;

    location / {
        return 200  "location / \n";
    }

    location = / {
        return 200 "location = \n";
    }

    location /documents/ {
        return 200 "location /documents/ \n";
    }

    location ^~ /image/ {
        return 200 "location ^~ /images/ \n";

    }

    location ~* \.(gif|jpg|jpeg)$ {
        return 200 "location ~* \.(gif|jpg|jpeg) \n";
    }

    location ~* /mysun/ {
        return 200 "location !~ mysun \n";
    }

    location ~ /myqu/ {
        return 200 "location ~ myqu \n";
    }
}

3.測試命令

curl yum.mysun.com
curl yum.mysun.com/documents
curl yum.mysun.com/documents/
curl yum.mysun.com/documents/
curl yum.mysun.com/documents
curl yum.mysun.com/documents/
curl yum.mysun.com/hahahah
curl yum.mysun.com/images/
curl yum.mysun.com/imagesssss/
curl yum.mysun.com/imagesssss
curl yum.mysun.com/imaaaaaa
curl yum.mysun.com/ima22222
curl yum.mysun.com/sunsunsun.jpg
curl yum.mysun.com/sunsunsun.jPg
curl yum.mysun.com/sunsunSSS.jPg
curl yum.mysun.com/mysun/
curl yum.mysun.com/mysUn/
curl yum.mysun.com/mYsUn/
curl yum.mysun.com/myqu/
curl yum.mysun.com/myqU/
curl yum.mysun.com/mYqU/
curl yum.mysun.com/image/hahah.jpg
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容