天天看點

Nginx中location與rewrite的運用

location

llocation與rewrite都能實作跳轉,但rewrite是在同一域名内更改擷取資源的路徑,而location是對一類路徑做控制通路或反向代理,還可以proxy_pass到其他機器。

location三類

精确比對:location = / {}

一般比對:location / {}

正則比對:location ~ / {}

location常用的比對規則

= :進行普通字元精确比對,也就是完全比對。
^~ :表示普通字元比對。使用字首比對。如果比對成功,則不再比對其它 location。
~ :區分大小寫的比對。
~* :不區分大小寫的比對。
!~ :區分大小寫的比對取非。
!~* :不區分大小寫的比對取非。
           

location優先級:

首先精确比對 =
其次字首比對 ^~
其次是按檔案中順序的正則比對 ~或~*
然後比對不帶任何修飾的字首比對
最後是交給 / 通用比對
           

location 示例說明

(1)location = / {}
=為精确比對 / ,主機名後面不能帶任何字元串,比如通路 / 和 /data,則 / 比對,/data 不比對
再比如 location = /abc,則隻比對/abc ,/abc/或 /abcd不比對。若 location  /abc,則即比對/abc 、/abcd/ 同時也比對 /abc/。

(2)location / {}
因為所有的位址都以 / 開頭,是以這條規則将比對到所有請求 比如通路 / 和 /data, 則 / 比對, /data 也比對,
但若後面是正規表達式會和最長字元串優先比對(最長比對)

(3)location /documents/ {}
比對任何以 /documents/ 開頭的位址,比對符合以後,還要繼續往下搜尋其它 location
隻有其它 location後面的正規表達式沒有比對到時,才會采用這一條

(4)location /documents/abc {}
比對任何以 /documents/abc 開頭的位址,比對符合以後,還要繼續往下搜尋其它 location
隻有其它 location後面的正規表達式沒有比對到時,才會采用這一條

(5)location ^~ /images/ {}
比對任何以 /images/ 開頭的位址,比對符合以後,停止往下搜尋正則,采用這一條

(6)location ~* \.(gif|jpg|jpeg)$ {}
比對所有以 gif、jpg或jpeg 結尾的請求
然而,所有請求 /images/ 下的圖檔會被 location ^~ /images/ 處理,因為 ^~ 的優先級更高,是以到達不了這一條正則

(7)location /images/abc {}
最長字元比對到 /images/abc,優先級最低,繼續往下搜尋其它 location,會發現 ^~ 和 ~ 存在

(8)location ~ /images/abc {}
比對以/images/abc 開頭的,優先級次之,隻有去掉 location ^~ /images/ 才會采用這一條

(9)location /images/abc/1.html {}
比對/images/abc/1.html 檔案,如果和正則 ~ /images/abc/1.html 相比,正則優先級更高
           

在實際網站中,至少要有三個比對規則定義

#第一個必選規則
直接比對網站根,通過域名通路網站首頁比較頻繁,使用這個會加速處理,比如說官網。
這裡是直接轉發給後端應用伺服器了,也可以是一個靜态首頁
location = / {
    proxy_pass http://tomcat_server/;
}

#第二個必選規則是處理靜态檔案請求,這是nginx作為http伺服器的強項
有兩種配置模式,目錄比對或字尾比對,任選其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}

location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

#第三個規則就是通用規則,比如用來轉發帶.php、.jsp字尾的動态請求到後端應用伺服器
非靜态檔案請求就預設是動态請求
location / {
    proxy_pass http://tomcat_server;
}
           

rewrite

功能:使用nginx提供的全局變量或是自己設定的變量,結合正規表達式和标志位實作URL重寫以及重定向

執行順序
(1) 執行 server 塊裡面的 rewrite 指令。
(2) 執行 location 比對。
(3) 執行標明的 location 中的 rewrite 指令。
           

文法rewrite [flag];

regex :表示正則比對規則。
replacement :表示跳轉後的内容。
flag :表示 rewrite 支援的 flag 标記。
           

flag标記說明

last :本條規則比對完成後,繼續向下比對新的location URI規則,一般用在 server 和 if 中。
break :本條規則比對完成即終止,不再比對後面的任何規則,一般使用在 location 中。
redirect :傳回302臨時重定向,浏覽器位址會顯示跳轉後的URL位址。
permanent :傳回301永久重定向,浏覽器位址欄會顯示跳轉後的URL位址。
           

示例

1,基于域名的跳轉

要求http://www.kgc.com/test/1.html跳轉到www.benet.com/test/1.html.

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.kgc.com;		#域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.kgc.com-access.log  main;		#日志修改
	location / {
	#添加域名重定向
        if ($host = 'www.kgc.com'){
           rewrite ^/(.*)$ http://www.benet.com/$1 permanent;
        }
        root   html;
        index  index.html index.htm;
    }
}
           

echo “192.168.226.50 www.kgc.com www.benet.com” >> /etc/hosts

systemctl restart nginx

Nginx中location與rewrite的運用

出現301實作了永久重定向跳轉。

2,基于用戶端的跳轉

要求隻有指定主機IP能通路

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.kgc.com;		#域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.kgc.com-access.log  main;		#日志修改

	#設定是否合法的IP标記
    set $rewrite true;							#設定變量$rewrite,變量值為boole值true
    #判斷是否為合法IP
	if ($remote_addr = "192.168.226.50"){
        set $rewrite false;
    }
	#除了合法IP,其它都是非法IP,進行重寫跳轉維護頁面
    if ($rewrite = true){
        rewrite (.+) /weihu.html;	
    }
    location = /weihu.html {
        root /var/www/html;
    }
	
	location / {
        root   html;
        index  index.html index.htm;
    }
}
           
mkdir -p /var/www/html/
echo "<h1>We are maintaining now!<\h1>" > /var/www/html/weihu.html
           

3,基于舊域名跳轉到新域名

要求使用舊域名會跳轉到新域名

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  bbs.kgc.com;
	charset utf-8;
	access_log  /var/log/nginx/www.kgc.com-access.log;
	location /post {
        rewrite (.+) http://www.kgc.com/bbs$1 permanent;
    }
	
	location / {
        root   html;
        index  index.html index.htm;
    }
}

           
mkdir -p /usr/local/nginx/html/bbs
echo "this is 1.html"  >> /usr/local/nginx/html/bbs/1.html
echo "192.168.226.40 bbs.kgc.com"  >> /etc/hosts
systemctl restart nginx
           
Nginx中location與rewrite的運用
Nginx中location與rewrite的運用

4,基于參數比對的跳轉

要求通路http://www.kgc.com/100-(100|200)-100.html 跳轉到http://www.kgc.com頁面

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.kgc.com;		#域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.kgc.com-access.log  main;
	
	if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.*) http://www.kgc.com permanent;
    }

	location / {
        root   html;
        index  index.html index.htm;
    }
}
           
systemctl restart nginx.service
           
Nginx中location與rewrite的運用
Nginx中location與rewrite的運用

5,基于目錄下所有php結尾的檔案跳轉

要求通路 http://www.kgc.com/upload/123.php 跳轉到首頁。

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.kgc.com;		#域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.kgc.com-access.log  main;
	
	location ~* /upload/.*\.php$ {
        rewrite (.+) http://www.kgc.com permanent;
    }

	location / {
        root   html;
        index  index.html index.htm;
    }
}
           
Nginx中location與rewrite的運用
Nginx中location與rewrite的運用