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 ]
}
- 測驗:
oldboy.jpg ---> /html/www/oldboy/ www.oldboy.com/meinv01.html 顯示 oldboy.jpg 圖片
oldgirl.jpg ---> /html/www/oldgirl/ www.oldboy.com/meinv02.html 顯示 oldgirl.jpg 圖片
[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;
}
}
總結:
01. 配置多個location時, 需要有一個默認的location
02. 訪問的uri文件信息必須存在
03. 訪問指定資源不存在,可以利用return功能進行跳轉訪問
nginx程序rewrite跳轉功能
用戶瀏覽器 輸入域名地址信息A -- web服務器 -- 自動處理 -- 訪問域名地址信息B
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
regex: 正則匹配的信息(url/uri)
replacement: 替換成什么信息/跳轉成什么地址信息
[flag]: 指定進行跳轉的方式
- 方式一: last 用戶訪問網(wǎng)站,進行跳轉之后,會重啟再次發(fā)起訪問 不會改變url/uri信息
-
方式二: break 用戶訪問網(wǎng)站,進行跳轉之后,會直接訪問跳轉之后的資源信息 不會改變url/uri信息
實踐說明:
server {
listen 80;
server_name www.oldboy.com;
root /html/www;
# www.oldboy.com/break/ -- 跳轉(break) -- www.oldboy.com/test/
location ~ ^/break/ {
rewrite ^/break/ /test/ break;
}
# www.oldboy.com/last/ -- 跳轉(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 (臨時跳轉) 不會讓瀏覽器記錄跳轉信息(url信息跳轉)
- permanent(永久跳轉) 會讓瀏覽器記錄跳轉信息(url信息跳轉)
例子:
www.jd.com/shouji/index.html -- www.jd.com/shouji01/index.html
www.jd.com/shouji/index.html -- www.jd.com/shouji02/index.html
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
# www.oldboy.com/break/ -- 跳轉(break) -- www.oldboy.com/test/
location ~ ^/break/ {
root /html/www;
index index.html;
rewrite ^/break/ /test/ redirect;
}
# www.oldboy.com/last/ -- 跳轉(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地址跳轉練習
- 例1: 用戶訪問www.oldboy.com/abc/1.html 實際上真實訪問是/ccc/bbb/2.html
http://www.oldboy.com/abc/1.html ==> http://www.oldboy.com/ccc/bbb/2.html A -> B
http://www.oldboy.com/abc/1.html ==> http://www.oldboy.com/ccc/bbb/2.html A -> B
第一個歷程: 創(chuàng)建站點目錄環(huán)境信息
跳轉前環(huán)境: mkdir /html/www/abc -p; echo oldboy >/html/www/abc/1.html
跳轉后環(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;
}
}
- 例2:用戶訪問www.oldboy.com/2014/ccc/bbb/2.html 實際上真實訪問是/2018/ccc/bbb/2.html AB --> CB A(.*) -- C(\1)
http://www.oldboy.com/2014/ccc/bbb/2.html ==> http://www.oldboy.com/2018/ccc/bbb/2.html
第一個歷程: 創(chuàng)建站點目錄環(huá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;
}
}