8.nginx開(kāi)啟目錄瀏覽 提供下載功能
默認(rèn)情況下,網(wǎng)站返回index指定的主頁(yè),但如果該網(wǎng)站不存在主頁(yè),則將請(qǐng)求交給autoindex模塊
##### 如果開(kāi)啟autoindex模塊,則提供一個(gè)下載的頁(yè)面, 如果沒(méi)有開(kāi)啟autoindex 則會(huì)報(bào)錯(cuò) 403
[root@web01 centos]# cat /etc/nginx/conf.d/mirror..conf
server {
listen 80;
server_name mirror.;
charset utf8; #字符集
location / {
root /code;
index index.html;
autoindex on; #開(kāi)啟目錄索引,提供下載
autoindex_exact_size off; #以人性化方式顯示大小
autoindex_localtime on; #與本地時(shí)間保持一致
}
}
9.nginx實(shí)現(xiàn)訪問(wèn)控制,基于來(lái)源IP控制、基于用戶名密碼控制
示例一.允許特定的IP訪問(wèn),其他全部拒絕
10.0.0.1 可以正常訪問(wèn) /centos
10.0.0.100 僅能訪問(wèn) /ubuntu /redhat
[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf
server {
listen 80;
server_name mirror.;
root /code;
charset utf8;
autoindex on; #開(kāi)啟目錄索引,提供下載
autoindex_exact_size off; #以人性化方式顯示大小
autoindex_localtime on; #與本地時(shí)間保持一致
location / {
index index.html;
}
location /centos {
allow 10.0.0.1/32;
deny all;
}
}
示例二.拒絕特定的IP訪問(wèn)(10.0.0.100),其他全部允許
[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf
server {
listen 80;
server_name mirror.;
root /code;
charset utf8;
autoindex on; #開(kāi)啟目錄索引,提供下載
autoindex_exact_size off; #以人性化方式顯示大小
autoindex_localtime on; #與本地時(shí)間保持一致
location / {
index index.html;
}
location /centos {
deny 10.0.0.100/32;
allow all;
}
}
注意:deny和allow的順序是有影響的
默認(rèn)情況下,從第一條規(guī)則進(jìn)行匹配
如果匹配成功,則不繼續(xù)匹配下面的內(nèi)容。
如果匹配不成功,則繼續(xù)往下尋找能匹配成功的內(nèi)容。
示例二.基于用戶名和密碼的方式限制 ( 針對(duì)個(gè)人 ) ( 針對(duì)運(yùn)維人員 )
1.安裝密碼生成工具
[root@web01 ~]# yum install httpd-tools -y
2.生成密碼
[root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf oldxu 123456
3.修改nginx配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf
server {
listen 80;
server_name mirror.;
root /code;
charset utf8;
autoindex on; #開(kāi)啟目錄索引,提供下載
autoindex_exact_size off; #以人性化方式顯示大小
autoindex_localtime on; #與本地時(shí)間保持一致
location / {
index index.html;
}
location /centos {
auth_basic "hello test";
auth_basic_user_file "/etc/nginx/auth_conf";
}
}
10.nginx實(shí)現(xiàn)限速 ( 下載限速 限制單位時(shí)間內(nèi)的Http請(qǐng)求 連接限制 )
1.請(qǐng)求頻率限制 Http
[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf
limit_req_zone $binary_remote_addr zone=req_od:10m rate=1r/s;
server {
listen 80;
server_name mirror.;
root /code;
charset utf8;
autoindex on; #開(kāi)啟目錄索引,提供下載
autoindex_exact_size off; #以人性化方式顯示大小
autoindex_localtime on; #與本地時(shí)間保持一致
limit_req zone=req_od burst=3 nodelay;
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表示通過(guò)這個(gè)標(biāo)識(shí)來(lái)做限制,限制同一客戶端ip地址。
#第二個(gè)參數(shù):zone=req_one:10m表示生成一個(gè)大小為10M,名為req_one的內(nèi)存區(qū)域,用來(lái)存儲(chǔ)訪問(wèn)的頻次信息。
#第三個(gè)參數(shù):rate=1r/s表示允許相同標(biāo)識(shí)的客戶端的訪問(wèn)頻次,這里限制的是每秒1次,還可以30r/m。
limit_req zone=req_one burst=3 nodelay;
#第一個(gè)參數(shù):zone=req_one 設(shè)置使用哪個(gè)配置區(qū)域來(lái)做限制,與上面limit_req_zone 里的name對(duì)應(yīng)。
#第二個(gè)參數(shù):burst=3,設(shè)置一個(gè)大小為3的緩沖區(qū),當(dāng)有大量請(qǐng)求過(guò)來(lái)時(shí),超過(guò)了訪問(wèn)頻次限制的請(qǐng)求可以先放到這個(gè)緩沖區(qū)內(nèi)。
#第三個(gè)參數(shù):nodelay,超過(guò)訪問(wèn)頻次并且緩沖區(qū)也滿了的時(shí)候,則會(huì)返回503,如果沒(méi)有設(shè)置,則所有請(qǐng)求會(huì)等待排隊(duì)。
2.連接限制
[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server {
listen 80;
server_name mirror.;
root /code;
charset utf8;
autoindex on; #開(kāi)啟目錄索引,提供下載
autoindex_exact_size off; #以人性化方式顯示大小
autoindex_localtime on; #與本地時(shí)間保持一致
limit_conn conn_od 2;
location / {
index index.html;
}
location /centos {
auth_basic "hello test";
auth_basic_user_file "/etc/nginx/auth_conf";
}
}
3.速率限制
[root@web01 ~]# cat /etc/nginx/conf.d/mirror..conf
limit_conn_zone $binary_remote_addr zone=conn_od:10m;
server {
listen 80;
server_name mirror.;
root /code;
charset utf8;
autoindex on; #開(kāi)啟目錄索引,提供下載
autoindex_exact_size off; #以人性化方式顯示大小
autoindex_localtime on; #與本地時(shí)間保持一致
limit_conn conn_od 2;
limit_rate_after 100m;
limit_rate 100k;
location / {
index index.html;
}
location /centos {
auth_basic "hello test";
auth_basic_user_file "/etc/nginx/auth_conf";
}
}
6.綜合案例、限制web服務(wù)器請(qǐng)求數(shù)處理為1秒一個(gè),觸發(fā)值為5、限制并發(fā)連接數(shù)為1、限制下載速度為100k
如果超過(guò)下載次數(shù),則返回提示 "請(qǐng)充值會(huì)員"
[root@web01 conf.d]# cat mirror..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.;
root /code;
charset utf8;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
limit_req zone=req_od burst=5 nodelay;
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提示--->請(qǐng)充值會(huì)員';
}
location / {
index index.html;
}
}
11.nginx狀態(tài)指標(biāo),俗稱7種狀態(tài) 監(jiān)控Nginx
location /nginx_status {
stub_status;
}
Active connections: 2
server accepts handled requests
2 2 17
Reading: 0 Writing: 1 Waiting: 1
Active connections 活躍的連接數(shù)
accepts 總的TCP連接數(shù)
handled 成功握手的TCP連接數(shù)
accepts - handled 失敗的TCP連接數(shù)
requests 總的請(qǐng)求數(shù)
Reading 讀取到請(qǐng)求頭的數(shù)量。
Writing 響應(yīng)客戶端到的數(shù)量。
Waiting 客戶端與服務(wù)端的連接數(shù)
vim /etc/nginx/nginx.conf
keepalive_timeout 65; #長(zhǎng)連接超時(shí)時(shí)間
keepalive_timeout 0; #模擬短連接效果
12.nginx location匹配、匹配優(yōu)先級(jí)
location是用來(lái)控制用戶請(qǐng)求的uri路徑的
語(yǔ)法:
location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... } #用戶內(nèi)部重定向
= 精確匹配
~ 正則匹配
~* 正則匹配(忽略大小寫(xiě))
^~ 以字符串方式匹配
/ 通用匹配
編寫(xiě)實(shí)例:
[root@web01 conf.d]# cat location..conf
server {
listen 80;
server_name location.;
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)';
}
}
測(cè)試:
1.請(qǐng)求 http://location./ 會(huì)被 location =/ 匹配
2.請(qǐng)求 http://location./index.html 會(huì)被 location / 匹配
3.請(qǐng)求 http://location./documents/test.html 會(huì)被 location /documents/ 匹配
4.請(qǐng)求 http://location./images/test.gif 會(huì)被 location ^~ /images/ 匹配
5.請(qǐng)求 http://location./documents/1.jpg 會(huì)被 location ~* \.(gif|jpg|jpeg)$ 匹配
優(yōu)先級(jí):
匹配符 匹配規(guī)則 優(yōu)先級(jí)
= 精確匹配 1
^~ 以某個(gè)字符串開(kāi)頭 2
~ 區(qū)分大小寫(xiě)的正則匹配 3
~* 不區(qū)分大小寫(xiě)的正則匹配 4
/ 通用匹配,任何請(qǐng)求都會(huì)匹配到 5
[root@web01 conf.d]# cat location2..conf
server {
listen 80;
server_name location2.;
通用匹配,任何請(qǐng)求都會(huì)匹配到
location / {
root html;
index index.html;
}
精準(zhǔn)匹配,必須請(qǐng)求的uri是/nginx_status
location = /nginx_status {
stub_status;
}
嚴(yán)格區(qū)分大小寫(xiě),匹配以.php結(jié)尾的都走這個(gè)location
location ~ \.php$ {
default_type text/html;
return 200 'php訪問(wèn)成功';
}
嚴(yán)格區(qū)分大小寫(xiě),匹配以.jsp結(jié)尾的都走這個(gè)location
location ~ \.jsp$ {
default_type text/html;
return 200 'jsp訪問(wèn)成功';
}
不區(qū)分大小寫(xiě)匹配,只要用戶訪問(wèn).jpg,gif,png,js,css 都走這條location
location ~* \.(jpg|gif|png|js|css)$ {
return 403;
}
不區(qū)分大小寫(xiě)匹配
location ~* \.(sql|bak|tgz|tar.gz|.git)$ {
deny all;
}
}
location @name { ... }
@”前綴定義命名位置。這樣的位置不用于常規(guī)請(qǐng)求處理,而是用于請(qǐng)求重定向.
server {
listen 80;
mirror.;
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 '你可能是瞎訪問(wèn),走丟了。但是不要以為瞎訪問(wèn)就能找到Bug.....';
? }
}
13.nginx 日志、訪問(wèn)日志、錯(cuò)誤日志、日志過(guò)濾、日志切割
統(tǒng)計(jì) 分析 那個(gè) uri請(qǐng)求的次數(shù)最多
錯(cuò)誤日志 用來(lái)排除故障
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format ttt '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent';
access_log /var/log/nginx/access.log main;
$remote_addr # 來(lái)源的客戶端IP ( user--->web )
$remote_user # 登錄的用戶名 Http基本認(rèn)證才會(huì)有 -
[$time_local] # 時(shí)間
$request # 請(qǐng)求uri 請(qǐng)求的方法 請(qǐng)求的協(xié)議
$status # 狀態(tài)碼
$body_bytes_sent # 發(fā)送的字節(jié)
$http_referer # 從那個(gè)url過(guò)來(lái)的
$http_user_agent # 來(lái)源的設(shè)備
$http_x_forwarded_for # 記錄真實(shí)的客戶端IP ( user--->proxy--->web )
日志過(guò)濾
location = /favicon.ico {
access_log off;
access_log /dev/null;
}
|