1.Nginx目錄索引
ngx_http_autoindex_module子產品處理以斜杠字元('/')結尾的請求,并生成目錄清單。
當ngx_http_index_module子產品找不到索引檔案時,通常會将請求傳遞給子產品。
1.指令
#啟用或禁用目錄清單輸出,on開啟,off關閉。
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
#指定是否應在目錄清單中輸出确切的檔案大小,on顯示位元組,off顯示大概機關。
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location
#指定目錄清單中的時間是應以本地時區還是UTC輸出。on本地時區,off UTC時間。
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location
2.示例配置
[root@web ~]# cat /etc/nginx/conf.d/module.conf
server {
listen 80;
server_name module.bgx.com;
charset utf-8,gbk; #設定字元集,防止中文字元亂碼顯示。
location /download {
root /code/;
autoindex on;
autoindex_exact_size off;
}
}
2.Nginx狀态監控
ngx_http_stub_status_module子產品提供對基本狀态資訊的通路。
預設情況下不建構此子產品,應使用--with-http_stub_status_module 配置參數啟用它 。
Syntax: stub_status;
Default: —
Context: server, location
[root@web ~]# cat /etc/nginx/conf.d/module.conf
server {
listen 80;
server_name module.bgx.com;
access_log off;
location /nginx_status {
stub_status;
}
}
3.此配置建立一個簡單的網頁,其基本狀态資料可能如下所示:
4.提供以下狀态資訊
Active connections # 目前活動用戶端連接配接數,包括Waiting等待連接配接數。
accepts # 已接受總的TCP連接配接數。
handled # 已處理總的TCP連接配接數。
requests # 用戶端總的http請求數。
Reading # 目前nginx讀取請求頭的連接配接數。
Writing # 目前nginx将響應寫回用戶端的連接配接數。
Waiting # 目前等待請求的空閑用戶端連接配接數。
# 注意, 一次TCP的連接配接,可以發起多次http的請求, 如下參數可配置進行驗證
keepalive_timeout 0; # 類似于關閉長連接配接
keepalive_timeout 65; # 65s沒有活動則斷開連接配接
3.Nginx通路控制
ngx_http_access_module子產品允許限制對某些用戶端位址的通路。
#允許配置文法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
#拒絕配置文法
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
2.示例配置,拒絕指定的IP通路該網站的/nginx_status, 其他IP全部允許通路
location /nginx_status {
stub_status;
#deny 192.168.5.4/32; #拒絕某個ip通路其他的都可以通路
#allow all;
allow 127.0.0.1; #監控nginx狀态時,僅允許該伺服器的回環位址通路
deny all;
}
3.示例配置,隻允許指定的來源IP能通路/nginx_status, 其它網段全部拒絕
[root@web ~]# cat /etc/nginx/conf.d/module.conf
server {
listen 80;
server_name module.bgx.com;
location /nginx_status {
stub_status;
allow 127.0.0.1; #監控nginx狀态時使用
allow 10.0.0.1/32; #允許位址或位址段
deny all; #拒絕所有人
}
}
注意:deny和allow的順序是有影響的
預設情況下,從第一條規則進行比對
如果比對成功,則不繼續比對下面的内容。
如果比對不成功,則繼續往下尋找能比對成功的内容。
4.Nginx資源限制
ngx_http_auth_basic_module子產品允許使用HTTP基本身份驗證,驗證使用者名和密碼來限制對資源的通路。
#使用HTTP基本身份驗證協定啟用使用者名和密碼驗證。
Syntax: auth_basic string| off;
Default: auth_basic off;
Context: http, server, location, limit_except
#指定儲存使用者名和密碼的檔案
Syntax: auth_basic_user_file file;
Default: -
Context: http, server, location, limit_except
2.指定儲存使用者名和密碼的檔案,格式如下:
#可以使用htpasswd程式或"openssl passwd"指令生成對應的密碼;
name1:passwd1
name2:passwd2
#使用htpaaswd建立新的密碼檔案, -c建立新檔案 -b允許指令行輸入密碼
[root@xuliangwei ~]# yum install httpd-tools
[root@xuliangwei ~]# htpasswd -b -c /etc/nginx/auth_conf xuliangwei 123456
3.示例配置,基于使用者名和密碼認證明踐
1.生成一個密碼檔案,密碼檔案的格式name:password(加密)(建議使用htpassword)
yum install httpd-tools -y
[root@db01 ~]# htpasswd -c -b /etc/nginx/auth_conf yin 123
[root@db01 ~]# cat /etc/nginx/auth_conf
yin:$apr1$H72OaMDg$z87.5/OEEkJ8XEmYWaQmm1
2.配置nginx 限制某個限制
4.Nginx通路限制
經常會遇到這種情況,伺服器流量異常,負載過大等等。對于大流量惡意的攻擊通路,會帶來帶寬的浪費,伺服器壓力,進而影響業務,針對這種情況我們可以考慮對同一個ip的連接配接數,請求數、進行限制。
ngx_http_limit_conn_module子產品用于限制定義key的連接配接數,特别是來自單個IP位址的連接配接數。
但并非所有連接配接都被計算在内,僅當連接配接已經讀取了整個請求頭時才計算連接配接。
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
2.設定共享記憶體區域和給定鍵值的最大允許連接配接數。超過此限制時,伺服器将傳回錯誤以回複請求
# http标簽段定義連接配接限制
http{
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
}
server {
# 同一時刻隻允許一個用戶端連接配接
limit_conn conn_zone 1;
location / {
root /code;
index index.html;
}
3).使用
ab
工具進行壓力測試
[root@xuliangwei ~]# yum install -y httpd-tools
[root@xuliangwei ~]# ab -n 20 -c 2 http://127.0.0.1/index.html
4).nginx日志結果
2018/10/24 18:04:49 [error] 28656#28656: *1148 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com"
2018/10/24 18:04:49 [error] 28656#28656: *1155 limiting connections by zone "conn_zone", client: 123.66.146.123, server: www.xuliangwei.com, request: "GET / HTTP/1.0", host: "www.xuliangwei.com"
ngx_http_limit_req_module子產品用于限制定義key請求的處理速率,特别單一的IP位址的請求的處理速率。
#子產品名ngx_http_limit_req_module
Syntax: limit_req_zone key zone=name:size rate=rate;
Default: —
Context: http
Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location
2.設定共享記憶體區域和請求的最大突發大小。過多的請求被延遲,直到它們的數量超過最大的限制,在這種情況下請求以錯誤終止。
# http标簽段定義請求限制, rate限制速率,限制一秒鐘最多一個IP請求
http {
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
}
server {
listen 80;
server_name module.bgx.com;
# 1r/s隻接收一個請求,其餘請求拒絕處理并傳回錯誤碼給用戶端
#limit_req zone=req_zone;
# 請求超過1r/s,剩下的将被延遲處理,請求數超過burst定義的數量, 多餘的請求傳回503
limit_req zone=req_zone burst=3 nodelay;
location / {
root /code;
index index.html;
}
}
ab
[root@xuliangwei ~]# yum install -y httpd-tools
[root@xuliangwei ~]# ab -n 500 -c 2 http://127.0.0.1/index.html
2018/10/24 07:38:53 [error] 81020#0: *8 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"
2018/10/24 07:38:53 [error] 81020#0: *9 limiting requests, excess: 3.998 by zone "req_zone", client: 10.0.0.10, server: module.bgx.com, request: "GET /index.html HTTP/1.0", host: "10.0.0.10"
Nginx連接配接限制沒有請求限制有效?
我們先來回顧一下http協定的連接配接與請求,首先HTTP是建立在TCP基礎之上, 在完成HTTP請求需要先建立TCP三向交握(稱為TCP連接配接),在連接配接的基礎上在完成HTTP的請求。
是以多個HTTP請求可以建立在一次TCP連接配接之上, 那麼我們對請求的精度限制,當然比對一個連接配接的限制會更加的有效,因為同一時刻隻允許一個TCP連接配接進入, 但是同一時刻多個HTTP請求可以通過一個TCP連接配接進入。是以針對HTTP的請求限制才是比較優的解決方案。
6.Nginx Location
使用Nginx Location可以控制通路網站的路徑, 但一個server允許出現多個location配置, 那多個location出現沖突誰的優先級會更高呢
1.
Location
文法示例
location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
}
2.
Location
文法優先級排列
3.配置網站驗證
Location
優先級
[root@Nginx conf.d]# cat testserver.conf
server {
listen 80;
server_name module.oldboy.com;
location / {
default_type text/html;
return 200 "location /";
}
location =/ {
default_type text/html;
return 200 "location =/";
}
location ~ / {
default_type text/html;
return 200 "location ~/";
}
# location ^~ / {
# default_type text/html;
# return 200 "location ^~";
# }
}
4.測試Location優先級
# 優先級最高符号=
[root@Nginx conf.d]# curl module.oldboy.com
location =/
# 注釋掉精确比對=, 重新開機Nginx
[root@Nginx ~]# curl module.oldboy.com
location ~/
# 注釋掉~, 重新開機Nginx
[root@Nginx ~]# curl module.oldboy.com
location /
5.Locaiton規則配置應用場景
# 通用比對,任何請求都會比對到
location / {
...
}
# 嚴格區分大小寫,比對以.php結尾的都走這個location
location ~ \.php$ {
...
}
# 嚴格區分大小寫,比對以.jsp結尾的都走這個location
location ~ \.jsp$ {
...
}
# 不區分大小寫比對,隻要使用者通路.jpg,gif,png,js,css 都走這條location
location ~* .*\.(jpg|gif|png|js|css)$ {
...
}
# 不區分大小寫比對
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
...
}