利用location區塊可以用於定位或者匹配網站資源信息
企業需求解決:
搭建好一臺nginx的web服務器。配置好內網卡地址與外網卡地址
web服務的網站域名爲www.etiantian.org,站點目錄爲html/www
要求內網用戶可以訪問網站http://www.etiantian.org/AV資源信息
要求外網用戶禁止訪問網站http://www.etiantian.org/AV資源信息
①. 如何利用nginx進行訪問控制
deny allow
ngx_http_access_module --- 實現訪問控制模塊
官方鏈接:nginx.org/en/docs/http/ngx_http_access_module.html
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
②. 如何定位站點目錄資源信息
location區塊進行定位站點目錄下資源信息
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
官方鏈接:http://nginx.org/en/docs/http/ngx_http_core_module.html#location
第一個里程:編寫nginx配置文件
server {
listen 80;
server_name www.etiantian.org;
root html/www;
index index.html index.htm;
location /AV {
allow 172.16.1.0/24;
deny 10.0.0.0/24;
}
}
第二個里程:創建測試訪問資源
cd mkdir /application/nginx/html/www 注意:需要切換到站點目錄下創建目錄
mkdir AV
echo "AV info" >AV/oldboy.html
cat AV/oldboy.html
第三個里程:重啓nginx服務
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
location [ = | ~ | ~* | ^~ ] uri { ... }
= --- 精確匹配網站uri資源信息
~ --- 區分大小寫匹配網站uri資源信息
~* --- 不區分大小寫匹配網站uri資源信息
^~ --- 優先匹配網站uri資源信息
/AV/ --- 指定匹配網站資源目錄信息
/ --- 默認匹配網站資源信息
! --- 對匹配的內容進行取反
location = / {
[ configuration A ] --- 優先級最高 ①
}
location / { --- 所有匹配都不滿足時候,匹配默認location ④
[ configuration B ]
}
location /documents/ { --- 根據資源目錄進行匹配 ③
[ configuration C ]
}
location ^~ /images/ { --- 優先匹配 ②
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ { --- 不區分大小寫匹配網站資源 ③
[ configuration E ]
}
Nginx服務rewrite模塊功能說明
- 實現域名地址信息跳轉
-
用於做僞靜態
www.etiantian.org/oldboy?edu.html ---動態資源
www.etiantian.org/oldboy-edu.html ---僞靜態實現類似百度重寫域名的功能?
baidu.com ===> www.baidu.com
etiantian.org ===> www.etiantian.orgrewrite
Syntax: rewrite regex(perl語言的正則表達式) replacement [flag];
Default: —
Context: server, location, iflast 持續匹配新的uri資源
stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
break 匹配到第一個uri資源後就終止
stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
redirect 臨時跳轉到uri資源信息
returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://”, “https://”, or “$scheme”;
permanent 永久跳轉
returns a permanent redirect with the 301 code.rewrite指令實踐操作一:(錯誤)
[root@web01 extra]# cat bbs.conf
server {
listen 80;
server_name www.etiantian.org bbs.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent; ----此處用到了perl語言的正則表達式
root html/bbs;
index index.html index.htm;
}[root@web01 extra]# curl -L etiantian.org
curl: (47) Maximum (50) redirects followed
[root@web01 extra]# curl -Lv etiantian.org --- 顯示無限循環過程
說明:以上配置進入了無限循環狀態rewrite指令實踐操作二:(正確)
cat bbs.conf
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*) http://bbs.etiantian.org/$1 permanent;
}
server {
listen 80;
server_name bbs.etiantian.org bbs.org;
root html/bbs;
index index.html index.htm;
}rewrite指令實踐操作三:(正確)
[root@web01 extra]# cat bbs.conf
server {
listen 80;
server_name bbs.etiantian.org bbs.org;
if ($host ~ "^etiantian.org$") {
rewrite ^/(.) http://bbs.etiantian.org/$1 permanent;
}
root html/bbs;
index index.html index.htm;
}