課程知識回顧
1.nginx服務訪問的流量控制
1.利用ip地址連接控制
http_limit_conn_module
key zone .name 存儲的空間信息 .size 存儲的空間大小
zone 調(diào)用相應的存儲空間 number: 限制存儲空間的ip地址的處理數(shù)量 503錯
2.利用用戶訪問請求頻次
limit_req_zone
rate:間隔的時間
zone [burst=number]
3. 1)訪問日志
2)錯誤日志
nginx程序location配置方法
作用: 匹配指定的uri信息,可以根據(jù)訪問不同的uri信息,做出不同處理方案
舉例:
訪問/oldboy 目錄時, 禁止10.0.0.0/24 網(wǎng)段訪問
訪問/oldgirl目錄時, 禁止172.16.1.0/24 網(wǎng)段訪問
訪問站點下面其他目錄, 沒有任何限制
如何匹配uri信息:
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
Default: —
Context: server, location
= 精確匹配指定uri信息 == grep -o 只要匹配上的信息
~ 模糊匹配指定uri信息(區(qū)分信息大小寫) == grep 匹配過濾信息
~* 模糊匹配指定uri信息(不區(qū)分大小寫) == grep -i 匹配過濾信息
^~ 進行優(yōu)先匹配/不識別擴展正則信息
location = / {
[ configuration A ]
}
location / { --- 進行默認匹配???
[ configuration B ]
}
location /documents/ { --- 根據(jù)資源目錄信息進行匹配
[ configuration C ]
}
location ^~ /images/ { --- 進行優(yōu)先匹配 /images/目錄
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
實踐配置:
第一個歷程: 編寫配置文件
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
# www.oldboy.com / --> index.html
location / { --- 默認匹配
root /html/www;
index index.html;
}
# www.oldboy.com / --> oldboy.html --- 精確匹配的location 會最優(yōu)先 01
location = / {
root /html/www;
index oldboy.html;
}
# www.oldboy.com/documents/ --> oldgirl.html --- 匹配指定目錄,顯示目錄下面的首頁文件信息
location /documents/ {
root /html/www;
index oldgirl.html;
}
# www.oldboy.com/images/ --> oldboy.jpg --- 優(yōu)先匹配 02
location ^~ /images/ {
root /html/www;
index oldboy.jpg;
}
# www.oldboy.com/xxxx.jpg --> xxx.jpg
location ~* \.(jpg|png|gif)$ {
root /html/www;
}
}
第二個歷程: 根據(jù)配置文件信息創(chuàng)建相應目錄和文件
cd /html/www
echo default-page >index.html
echo oldboy-page >oldboy.html
mkdir documents ; echo oldgirl-page > documents/oldgirl.html
mkdir images
測驗:
oldboy.jpg ---> /html/www/oldboy/ www.oldboy.com/meinv01.html 顯示 oldboy.jpg 圖片
oldgirl.jpg ---> /html/www/oldgirl/ www.oldboy.com/meinv02.html 顯示 oldgirl.jpg 圖片
思路
第一個步驟: 根據(jù)不同uri顯示不同頁面信息
location /meinv01.html {
return meinv01
}
location /meinv02.html {
return meinv02
}
第二個步驟: 完善配置文件
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
}
location /meinv01.html {
return 301 http://www.oldboy.com/oldboy/oldboy.jpg;
}
location /meinv02.html {
return 301 http://www.oldboy.com/oldgirl/oldgirl.jpg;
}
}
總結(jié):
01. 配置多個location時, 需要有一個默認的location
02. 訪問的uri文件信息必須存在
03. 訪問指定資源不存在,可以利用return功能進行跳轉(zhuǎn)訪問
nginx程序rewrite跳轉(zhuǎn)功能
用戶瀏覽器 輸入域名地址信息A -- web服務器 -- 自動處理 -- 訪問域名地址信息B
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
regex: 正則匹配的信息(url/uri)
replacement: 替換成什么信息/跳轉(zhuǎn)成什么地址信息
[flag]: 指定進行跳轉(zhuǎn)的方式
方式一: last 用戶訪問網(wǎng)站,進行跳轉(zhuǎn)之后,會重啟再次發(fā)起訪問 不會改變url/uri信息
方式二: break 用戶訪問網(wǎng)站,進行跳轉(zhuǎn)之后,會直接訪問跳轉(zhuǎn)之后的資源信息 不會改變url/uri信息
實踐說明:
server {
listen 80;
server_name www.oldboy.com;
root /html/www;
# www.oldboy.com/break/ -- 跳轉(zhuǎn)(break) -- www.oldboy.com/test/
location ~ ^/break/ {
rewrite ^/break/ /test/ break;
}
# www.oldboy.com/last/ -- 跳轉(zhuǎn)(last) -- www.oldboy.com/test/
location ~ ^/last/ {
rewrite ^/last/ /test/ last;
}
# www.oldboy.com/test/ --- 頁面顯示 ok
location /test/ {
default_type application/json;
return 200 'ok';
}
}
方式三: redirect (臨時跳轉(zhuǎn)) 將地址url/uri信息進行跳轉(zhuǎn)變化
方式四: permanent(永久跳轉(zhuǎn)) 將地址url/uri信息進行跳轉(zhuǎn)變化
臨時跳轉(zhuǎn): 不會讓瀏覽器記錄跳轉(zhuǎn)信息 (uri信息跳轉(zhuǎn))
用戶端訪問網(wǎng)站 --- 瀏覽器 --- 網(wǎng)站服務器 --- 會將訪問地址進行跳轉(zhuǎn)
瀏覽器(訪問跳轉(zhuǎn)后地址) --- 網(wǎng)站服務器 --- 返回頁面信息
用戶端訪問網(wǎng)站 --- 瀏覽器 --- 網(wǎng)站服務器 --- 會將訪問地址進行跳轉(zhuǎn)
瀏覽器(訪問跳轉(zhuǎn)后地址) --- 網(wǎng)站服務器 --- 返回頁面信息
www.jd.com/shouji/index.html -- www.jd.com/shouji01/index.html
www.jd.com/shouji/index.html -- www.jd.com/shouji02/index.html
永久跳轉(zhuǎn): 會讓瀏覽器記錄跳轉(zhuǎn)信息 (url信息跳轉(zhuǎn))
用戶端訪問網(wǎng)站 --- 瀏覽器 --- 網(wǎng)站服務器 --- 會將訪問地址進行跳轉(zhuǎn)(告知瀏覽器進行記錄)
瀏覽器(訪問跳轉(zhuǎn)后地址) --- 網(wǎng)站服務器 --- 返回頁面信息
(記錄跳轉(zhuǎn)信息)
用戶端訪問網(wǎng)站 --- 瀏覽器(自動訪問跳轉(zhuǎn)后地址) --- 網(wǎng)站服務器 --- 返回頁面信息
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
# www.oldboy.com/break/ -- 跳轉(zhuǎn)(break) -- www.oldboy.com/test/
location ~ ^/break/ {
root /html/www;
index index.html;
rewrite ^/break/ /test/ redirect;
}
# www.oldboy.com/last/ -- 跳轉(zhuǎn)(last) -- www.oldboy.com/test/
location ~ ^/last/ {
root /html/www;
index index.html;
rewrite ^/last/ /test/ permanent;
}
# www.oldboy.com/test/ --- 頁面顯示 ok
location /test/ {
root /html/www;
index index.html;
default_type application/json;
return 200 'ok';
}
}
uri地址跳轉(zhuǎn)練習:
http://www.oldboy.com/abc/1.html ==> http://www.oldboy.com/ccc/bbb/2.html A -> B
第一個歷程: 創(chuàng)建站點目錄環(huán)境信息
跳轉(zhuǎn)前環(huán)境: mkdir /html/www/abc -p; echo oldboy >/html/www/abc/1.html
跳轉(zhuǎn)后環(huán)境: mkdir /html/www/ccc/bbb -p; echo oldboy >/html/www/ccc/bbb/2.html
第二個歷程: 編寫配置文件
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
}
location /abc/ {
root /html/www;
index index.html;
rewrite (.*) /ccc/bbb/2.html redirect;
}
#http://www.oldboy.com/2014/ccc/bbb/2.html ==> http://www.oldboy.com/2018/ccc/bbb/2.html
第一個歷程: 創(chuàng)建站點目錄環(huán)境信息
跳轉(zhuǎn)后環(huán)境: mkdir /html/www/2018/ccc/bbb/ -p; echo 2018 >/html/www/2018/ccc/bbb/2.html
第二個歷程: 編寫配置文件信息
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
}
location /2014/ {
root /html/www;
index index.html;
rewrite ^/2014/(.*) /2018/$1 redirect;
}
}
. 課程知識總結(jié)說明
1) nginx 程序 location指令配置方法
location 匹配uri的方式: = ~ ~* ^~ / /oldboy/ \.jpg$
2) nginx 程序 rewrite指令配置方法
flag標簽信息 == 跳轉(zhuǎn)的方式
last/break/redirect/permanent
3) 完成了跳轉(zhuǎn)練習 uri信息跳轉(zhuǎn)