Nginx常用模塊

Nginx常用模塊

1.nginx開啟目錄瀏覽 提供下載功能

默認(rèn)情況下,網(wǎng)站返回index指定的主頁,但如果該網(wǎng)站不存在主頁,則將請求交給autoindex模塊
如果開啟autoindex模塊,則提供一個(gè)下載的頁面, 如果沒有開啟autoindex 則會(huì)報(bào)錯(cuò) 403

[root@web01 centos]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf
server {
    listen 80;
    server_name mirror.oldxu.com;
    charset utf8;                   #字符集

    location / {
        root /code;                 #訪問/會(huì)從該目錄下找內(nèi)容
        index index.html;           #默認(rèn)格式,指向一個(gè)頁面
        autoindex on;               #開啟目錄索引,提供下載
        autoindex_exact_size off;   #以人性化方式顯示大小
        autoindex_localtime on;     #與本地時(shí)間保持一致
    }
}

2.nginx實(shí)現(xiàn)訪問控制

基于來源IP控制
基于用戶名和密碼控制

注意:deny和allow的順序是有影響的

默認(rèn)情況下,從第一條規(guī)則進(jìn)行匹配
如果匹配成功,則不繼續(xù)匹配下面的內(nèi)容。
如果匹配不成功,則繼續(xù)往下尋找能匹配成功的內(nèi)容。

2.1基于來源IP控制:

2.1.1允許特定的IP訪問,其他全部拒絕

10.0.0.1 可以正常訪問 /centos
10.0.0.100 僅能訪問 /ubuntu /redhat

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;               #開啟目錄索引,提供下載
    autoindex_exact_size off;   #以人性化方式顯示大小
    autoindex_localtime on;     #與本地時(shí)間保持一致

    location / {
        index index.html;
    }

    location /centos {
        allow 10.0.0.1/32;      #允許
        deny all;               #拒絕
    }
}

2.1.2拒絕特定的IP訪問,其他全部允許

拒絕10.0.0.100,其他來源IP地址都允許

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
        autoindex on;           #開啟目錄索引,提供下載
        autoindex_exact_size off;   #以人性化方式顯示大小
        autoindex_localtime on;     #與本地時(shí)間保持一致

    location / {
        index index.html;
    }

    location /centos {
        deny 10.0.0.100/32;         #拒絕
        allow all;                  #允許
    }
}

2.2基于用戶名和密碼限制

針對個(gè)人、針對運(yùn)維人員

2.2.1安裝密碼生成工具

[root@web01 ~]# yum install httpd-tools -y

2.2.2生成密碼

#命令 參數(shù) 密碼保存的位置 用戶名 密碼
[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf wang 123456
Adding password for user wang

生成的密碼會(huì)進(jìn)行加密處理,所以打開文件只能

[root@web01 ~]# cat /etc/nginx/auth_conf 
wang:$apr1$I.zmeMre$xQ8Xz6Fq3Ax3/j1.0OhX41

2.2.3修改Nginx配置文件

[root@web01 ~]# cat  /etc/nginx/conf.d/mirror.oldxu.com.conf 
server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
        autoindex on;           #開啟目錄索引,提供下載
        autoindex_exact_size off;   #以人性化方式顯示大小
        autoindex_localtime on;     #與本地時(shí)間保持一致

    location / {
        index index.html;
    }

    location /centos {           #指定訪問/centos觸發(fā)密碼驗(yàn)證
        auth_basic "hello test";   #登錄界面的頭部,必須要加!內(nèi)容自定
        auth_basic_user_file "/etc/nginx/auth_conf";   #用戶密碼所在的位置
    }
}

3.nginx實(shí)現(xiàn)限速

nginx所能實(shí)現(xiàn)的限速有:下載限速 限制單位時(shí)間內(nèi)的Http請求 連接限制

3.1請求頻率限制 Http

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;  #請求模塊

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;               #開啟目錄索引,提供下載
    autoindex_exact_size off;   #以人性化方式顯示大小
    autoindex_localtime on;     #與本地時(shí)間保持一致
    
    limit_req zone=req_od burst=3 nodelay;   #超過3次請求就503
    

    location / {
        index index.html;
    }

    location /centos {
        auth_basic "hello test";
        auth_basic_user_file "/etc/nginx/auth_conf";
    }
}
limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s;

第一個(gè)參數(shù):$binary_remote_addr表示通過這個(gè)標(biāo)識(shí)來做限制,限制同一客戶端ip地址。
第二個(gè)參數(shù):zone=req_one:10m表示生成一個(gè)大小為10M,名為req_one的內(nèi)存區(qū)域,用來存儲(chǔ)訪問的頻次信息。
第三個(gè)參數(shù):rate=1r/s表示允許相同標(biāo)識(shí)的客戶端的訪問頻次,這里限制的是每秒1次,還可以30r/m。

limit_req zone=req_one burst=3 nodelay;

第一個(gè)參數(shù):zone=req_one 設(shè)置使用哪個(gè)配置區(qū)域來做限制,與上面limit_req_zone 里的name對應(yīng)。
第二個(gè)參數(shù):burst=3,設(shè)置一個(gè)大小為3的緩沖區(qū),當(dāng)有大量請求過來時(shí),超過了訪問頻次限制的請求可以先放到這個(gè)緩沖區(qū)內(nèi)。
第三個(gè)參數(shù):nodelay,超過訪問頻次并且緩沖區(qū)也滿了的時(shí)候,則會(huì)返回503,如果沒有設(shè)置,則所有請求會(huì)等待排隊(duì)。

3.2連接數(shù)限制

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_conn_zone $binary_remote_addr zone=conn_od:10m;  #連接的模塊

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;           #開啟目錄索引,提供下載
    autoindex_exact_size off;   #以人性化方式顯示大小
    autoindex_localtime on;     #與本地時(shí)間保持一致
    limit_conn conn_od 2;       #最大連接2

    location / {
        index index.html;
    }

    location /centos {
        auth_basic "hello test";
        auth_basic_user_file "/etc/nginx/auth_conf";
    }
}

3.3下載速率限制

[root@web01 ~]# cat /etc/nginx/conf.d/mirror.oldxu.com.conf 
limit_conn_zone $binary_remote_addr zone=conn_od:10m;

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
        autoindex on;               #開啟目錄索引,提供下載
        autoindex_exact_size off;   #以人性化方式顯示大小
        autoindex_localtime on;     #與本地時(shí)間保持一致
    limit_conn conn_od 2;           #最大連接2個(gè)
    limit_rate_after 100m;          #到達(dá)100m之后開始限速
    limit_rate 100k;                #限制下載速度到100k

    location / {
        index index.html;
    }

    location /centos {
        auth_basic "hello test";
        auth_basic_user_file "/etc/nginx/auth_conf";
    }
}

4.模擬實(shí)例-練習(xí)題

綜合案例、限制web服務(wù)器請求數(shù)處理為1秒一個(gè),觸發(fā)值為5、限制并發(fā)連接數(shù)為1、限制下載速度為100k,如果超過下載次數(shù),則返回提示 "請充值會(huì)員"

[root@web01 conf.d]# cat mirror.oldxu.com.conf 
limit_req_zone  $binary_remote_addr zone=req_od:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=conn_od:10m;

server {
    listen 80;
    server_name mirror.oldxu.com;
    root /code;
    charset utf8;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    limit_req zone=req_od burst=5 nodelay;   #超過請求次數(shù)會(huì)報(bào)503
    limit_conn conn_od 1;
    limit_rate_after 100m;
    limit_rate 100k;
    
    error_page 503 @errpage;
    location @errpage {
        default_type text/html;
        return 200 ' Oldxu提示--->請充值會(huì)員';
    }
    location / {
        index index.html;
    }
}

5.nginx狀態(tài)指標(biāo)

nginx狀態(tài)指標(biāo),俗稱7種狀態(tài) 監(jiān)控Nginx

#在配置文件中添加官方提供的模塊即可,無需創(chuàng)建該目錄
location /nginx_status {
        stub_status;
    }

[圖片上傳失敗...(image-5f392c-1570707835503)]

狀態(tài) 含義
Active connections 活躍的連接數(shù)
accepts 總的TCP連接數(shù)
handled 成功握手的TCP連接數(shù)
accepts 減 handled 失敗的TCP連接數(shù)
requests 總的請求數(shù)
Reading 讀取到請求頭的數(shù)量
Writing 響應(yīng)客戶端到的數(shù)量
Waiting 客戶端與服務(wù)端的連接數(shù)
vim /etc/nginx/nginx.conf
    keepalive_timeout 65;       #長連接超時(shí)時(shí)間,一次連接多次請求
    keepalive_timeout 0;        #模擬短連接效果,一次連接一次請求

6.nginx location匹配 優(yōu)先級(jí)

location是用來控制用戶請求的uri路徑的

語法:

location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... } #用戶內(nèi)部重定向

符號(hào) 作用 優(yōu)先級(jí)
= 精準(zhǔn)匹配 1
^~ 以字符串方式匹配,以某個(gè)字符串開頭 2
~ 正則匹配,區(qū)分大小寫 3
~* 正則匹配,不區(qū)分大小寫 4
/ 通用匹配,任何請求都會(huì)匹配到 5

6.1編寫優(yōu)先級(jí)實(shí)例:

[root@web01 conf.d]# cat location.oldxu.com.conf 
server {
    listen 80;
    server_name location.oldxu.com;

    location = / {
        default_type text/html;
        return 200 'location = /';
    }

    location / {
        default_type text/html;
        return 200 'location /';
    }

    location /documents/ {
        default_type text/html;
        return 200 'location /documents/';
    }

    location ^~ /images/ {
        default_type text/html;
        return 200 'location ^~ /images/';
    }

    location ~* \.(gif|jpg|jpeg)$ {
        default_type text/html;
        return 200 'location ~* \.(gif|jpg|jpeg)';
    }
}

6.2測試:

1.請求 http://location.oldxu.com/             會(huì)被  location =/     匹配
2.請求 http://location.oldxu.com/index.html   會(huì)被  location /      匹配
3.請求 http://location.oldxu.com/documents/test.html  會(huì)被  location /documents/    匹配
4.請求 http://location.oldxu.com/images/test.gif  會(huì)被  location ^~ /images/    匹配
5.請求 http://location.oldxu.com/documents/1.jpg  會(huì)被  location ~* \.(gif|jpg|jpeg)$   匹配

6.3優(yōu)先級(jí)用法解釋

[root@web01 conf.d]# cat location2.oldxu.com.conf 
server {
    listen 80;
    server_name location2.oldxu.com;

    # 通用匹配,任何請求都會(huì)匹配到
    location / {
        root html;
        index index.html;
    }

    # 精準(zhǔn)匹配,必須請求的uri是/nginx_status
    location = /nginx_status {
        stub_status;
    }

    # 嚴(yán)格區(qū)分大小寫,匹配以.php結(jié)尾的都走這個(gè)location    
    location ~ \.php$ {
        default_type text/html;
        return 200 'php訪問成功';
    }

    # 嚴(yán)格區(qū)分大小寫,匹配以.jsp結(jié)尾的都走這個(gè)location 
    location ~ \.jsp$ {
        default_type text/html;
        return 200 'jsp訪問成功';
    }

    # 不區(qū)分大小寫匹配,只要用戶訪問.jpg,gif,png,js,css 都走這條location
    location ~* \.(jpg|gif|png|js|css)$ {
        return 403;
    }

    # 不區(qū)分大小寫匹配
    location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
        deny all;
    }
}
    location @name { ... }
    @”前綴定義命名位置。這樣的位置不用于常規(guī)請求處理,而是用于請求重定向.

server {
    listen 80;
    mirror.oldxu.com;
    root /code;
        
    location / {
        index index.html;
    }

    #如果出現(xiàn)異常,則重新定向到@error_404這個(gè)location上
    error_page 404  @error_404;
    location @error_404 {
        default_type text/html;
        return 200 '你可能是瞎訪問,走丟了。但是不要以為瞎訪問就能找到Bug.....';
    }
}

7.nginx日志

7.1日志分類

nginx日志:/var/log/nginx/access.log,統(tǒng)計(jì)、分析、那個(gè)uri請求的次數(shù)最多

nginx錯(cuò)誤日志: /var/log/nginx/error.log,錯(cuò)誤日志,用來排除故障

7.2日志含義

#日志模塊
[root@web01 ~]# vim /etc/nginx/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"';
...
######每個(gè)部分的含義#######
$remote_addr            # 來源的客戶端IP      (   user--->web  )
$remote_user            # 登錄的用戶名 Http基本認(rèn)證才會(huì)有 -
[$time_local]           # 時(shí)間
$request                # 請求uri 請求的方法 請求的協(xié)議
$status                 # 狀態(tài)碼
$body_bytes_sent        # 發(fā)送的字節(jié)
$http_referer           # 從那個(gè)url過來的
$http_user_agent        # 來源的設(shè)備
$http_x_forwarded_for   # 記錄真實(shí)的客戶端IP    (   user--->proxy--->web  )

7.3日志過濾

location = /favicon.ico {
        access_log off;
        access_log /dev/null;
}

7.3日志切割

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

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

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