匹配這兩種特殊字符「~」或「~*」的區別爲:「~」用於區分大小寫(大小寫敏感)的匹配;「~*」用於不區分大小寫的匹配。還能夠用邏輯操做符「!」對上面的匹配取反,即「!~」和「!~*」。此外,「^~」的做用是先進行字符串的前綴匹配(必須之後邊的字符串開頭),若是能匹配到,就再也不進行其餘location的正則匹配了。php
6.5.2 location匹配示例
[root@localhost nginx]# cat /usr/local/nginx/conf/extra/www.conf server { listen 80; server_name www.yunjisuan.com; root /var/www/html/wwwcom; location / { return 401; } location = / { return 402; } location = /images/ { return 501; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } }
正則匹配結果以下:html
[root@localhost nginx]# curl -s -o /dev/null -w "%{http_code}\n" www.yunjisuan.com 402 #匹配了=的狀況 [root@localhost nginx]# curl -s -o /dev/null -w "%{http_code}\n" www.yunjisuan.com/ 402 #匹配了=的狀況 [root@localhost nginx]# curl -s -o /dev/null -w "%{http_code}\n" www.yunjisuan.com/xxxx 401 #匹配不到默認匹配 /的狀況 [root@localhost nginx]# curl -s -o /dev/null -w "%{http_code}\n" www.yunjisuan.com/documents/ 403 #匹配字符串 [root@localhost nginx]# curl -s -o /dev/null -w "%{http_code}\n" www.yunjisuan.com/images/ 501 #優先匹配=的狀況 [root@localhost nginx]# curl -s -o /dev/null -w "%{http_code}\n" www.yunjisuan.com/images/1.jpg 404 #匹配 [root@localhost nginx]# curl -s -o /dev/null -w "%{http_code}\n" www.yunjisuan.com/documents/images/1.jpg 500 #匹配~*的狀況
從多個location的配置匹配能夠看出匹配的優先順序nginx
順序 | 匹配標識的location | 匹配說明 |
---|---|---|
1 | " location = / { " | 精確匹配 |
2 | " location ^~ /images/ { " | 先進行字符串的前綴匹配,若是匹配到就不作正則匹配檢查 |
3 | " loction ~* \.(gif | jpg | jpeg)$ { " | 正則匹配,*爲不區分大小寫 |
4 | " location /documents/ { " | 匹配常規字符串,模糊匹配,若是有正則檢查,正則優先 |
5 | " location / { " | 全部location都不能匹配後的默認匹配原則 |
6.6 Nginx rewrite
6.6.1 什麼是Nginx rewrite?
和Apache等Web服務軟件同樣,Nginx rewrite的主要功能也是實現URL地址重寫。Nginx的rewrite規則須要PCRE軟件的支持,即經過Perl兼容正則表達式語法進行規則匹配。默認參數編譯時,Nginx就會安裝支持rewrite的模塊,可是,也必需要有PCRE軟件的支持。面試
6.6.2 Nginx rewrite 語法
(1)rewrite指令語法正則表達式
指令語法:rewrite regex replacement 【flag】;
默認值:none
應用位置:server,location,if
rewrite是實現URL重寫的關鍵指令,根據regex(正則表達式)部分的內容,重定向到replacement部分,結尾是flag標記。下面是一個簡單的URL rewrite跳轉例子:apache
rewrite ^/(.*) http://www.baidu.com/$1 permanent;
在上述指令中,rewrite爲固定關鍵字,表示開啓一條rewrite匹配規則,regex部分是^(.*),這是一個正則表達式,表示匹配全部,匹配成功後跳轉到http://www.baidu.com/$1 。這裏的$1是取前面regex部分括號裏的內容,結尾的permanent;是永久301重定向標記,即跳轉到後面的http://www.baidu.com/$1 地址上。瀏覽器
(2)regex經常使用正則表達式說明ruby
(3)rewrite指令的最後一項參數flag標記的說明bash
- 在以上的flag標記中,last和break用來實現URL重寫,瀏覽器地址欄的URL地址不變,但在服務器端訪問的程序及路徑發生了變化。redirect和permanent用來實現URL跳轉,瀏覽器地址欄會顯示跳轉後的URL地址。
- last和break標記的實現功能相似,但兩者之間有細微的差異,使用alias指令時必須用last標記,使用proxy_pass指令時要使用break標記。last標記在本條rewrite規則執行完畢後,會對其所在的server{...}標籤從新發起請求,而break標記則會在本條規則匹配完成後,終止匹配,再也不匹配後面的規則。
6.6.3 Nginx rewrite 的企業應用場景
Nginx的rewrite功能在企業裏應用很是普遍:服務器
- 能夠調整用戶瀏覽的URL,使其看起來更規範,合乎開發及產品人員的需求。
- 爲了讓搜索引擎收錄網站內容,並讓用戶體驗更好,企業會將動態URL地址假裝成靜態地址提供服務
- 網站換新域名後,讓舊域名的訪問跳轉到新的域名上,例如:讓京東的360buy換成了jd.com
- 根據特殊變量,目錄,客戶端的信息進行URL跳轉等。
6.6.4 Nginx rewrite 301 跳轉
以往咱們是經過別名方式實現yunjisuan.com和www.yunjisuan.com訪問同一個地址的,事實上,除了這個方式外,還可使用nginx rewrite 301 跳轉的方式來實現。實現的配置以下:
[root@localhost nginx]# cat conf/extra/www.conf #www virtualhost by Mr.chen server { listen 80; server_name www.yunjisuan.com; root /var/www/html/wwwcom; location / { index index.html index.htm; } # location = / { # return 402; # } location = /images/ { return 501; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } } server{ listen 80; server_name yunjisuan.com; rewrite ^/(.*) http://www.yunjisuan.com/$1 permanent; #當用戶訪問yunjisuan.com及下面的任意內容時,都會經過這條rewrite跳轉到www.yunjisuan.com對應的地址 }
客戶端訪問測試結果
6.6.5 實現不一樣域名的URL跳轉
示例:實現訪問http://mail.yunjisuan.com時跳轉到http://www.yunjisuan.com/mail/yunjisuan.html
外部跳轉時,使用這種方法可讓瀏覽器地址變爲跳轉後的地址,另外,要事先設置http://www.yunjisuan.com/mail/yunjisuan.html有結果輸出,否則會出現401等權限錯誤。
(1)配置Nginx rewrite規則
[root@localhost nginx]# cat conf/extra/mail.conf server { listen 80; server_name mail.yunjisuan.com; location / { root /var/www/html/mailcom; index index.html index.htm; } if ( $http_host ~* "^(.*)\.yunjisuan\.com$") { set $domain $1; rewrite ^(.*) http://www.yunjisuan.com/$domain/yunjisuan.html break; } }
客戶端訪問測試
6.6.6 rewrite跳轉標記flag使用總結
1,在根location(即location / {...})中或server{...} 標籤中編寫rewrite規則,建議使用last標記
2,在普通的location(例 location/yunjisuan/{...}或if{}中編寫rewrite規則,則建議使用break標記)
6.7 Nginx訪問認證
有時,在實際工做中企業要求咱們爲網站設置訪問帳號和密碼權限,這樣操做後,只有擁有帳號密碼的用戶才能夠訪問網站內容。
這種使用帳號密碼才能夠訪問網站的功能主要應用在企業內部人員訪問的地址上,例如:企業網站後臺,MySQL客戶端phpmyadmin,企業內部的CRM,WIKI網站平臺。
6.7.1 建立密碼文件
咱們能夠借用apache的htpasswd軟件,來建立加密的帳號和密碼
[root@localhost ~]# which htpasswd [root@localhost ~]# htpasswd -bc /usr/local/nginx/conf/htpasswd yunjisuan 123123 Adding password for user yunjisuan [root@localhost ~]# cat /usr/local/nginx/conf/htpasswd yunjisuan:FC1/eEc/iK0Mo #帳號密碼是加密的(htpasswd是文件的名字)
6.7.2 在虛擬主機配置文件里加入兩條配置信息
[root@localhost html]# cat /usr/local/nginx/conf/extra/blog.conf server { listen 80; server_name blog.yunjisuan.com; location / { root /var/www/html/blogcom; index index.html index.htm; auth_basic "yunjisuan training"; #加入這條配置 auth_basic_user_file /usr/local/nginx/conf/htpasswd; #加入這條配置 } } #配置解釋: auth_basic :驗證的基本信息選項(後邊跟着的雙引號裏就是驗證窗口的名字) auth_basic_user_file :驗證的用戶文件(後邊根帳號密碼文件的絕對路徑)
6.7.3 網頁登錄驗證
6.8 Nginx相關問題解答
6.8.1 Tengine和Nginx是什麼關係?
Tengine是淘寶開源Nginx的分支,官方站點爲http://tengine.taobao.org/
6.8.2 訪問Nginx時出現狀態碼「403 forbidden」的緣由
(1)Nginx配置文件裏沒有配置默認首頁參數,或者首頁文件在站點目錄下沒有以下內容:
index index.php index.html index.htm;
(2)站點目錄或內部的程序文件沒有Nginx用戶訪問權限
(3)Nginx配置文件中設置了allow,deny等權限控制,致使客戶端沒有訪問權限。
7 本章知識點回顧
- Nginx的特性優勢
- 主流Web動態靜態性能對比
- Apache select 和Nginx epoll 模型的區別(面試常考)
- 虛擬主機概念及類型分類詳解
- 基於域名和端口虛擬主機的介紹及搭建
- Nginx錯誤,訪問日誌,以及訪問日誌切割
- Nginx訪問狀態信息介紹及配置實踐。
- Nginx location介紹及配置實踐
- Nginx rewrite介紹及配置實踐
- Nginx Web訪問認證介紹及配置實踐