rewrite和return筆記

linux9期架構-宋陽陽-day21

課前回顧

KVM服務能夠在linux上作虛擬機

禁ping(centos開啓或關閉ping)
	
集羣先後端分離接口(api接口):
	做用:前端服務器(web)的api接口與後端服務器的(tomcat,)api接口的服務器鏈接

SEO與瀏覽器搜索出來的網站排名有關

瀏覽器URL中的變量是用來調用數據庫的

關係型數據庫:
非關係型數據庫:

tNefl4.md.png

網頁

#什麼是動態頁,靜態頁,僞靜態頁
URL中,'須要調用數據庫',返回的網頁,就是動態頁(?就是傳參,就是在訪問數據庫)
URL中,'靜態頁不須要調用數據庫',URL中顯示準確的路徑
URL中,靜態頁須要調用數據庫,'URL中顯示僞造的路徑',那麼這個網頁叫僞靜態頁
訪問某一資源,須要調用數據庫,那麼這個資源就是動態的,不然就是靜態的,'靜態與動態的區分與文件的種類無關,只和資源存放位置有關'

#爲何要作僞靜態:
1.安全
2.爲了SEO,百度curl的是靜態網頁,作僞靜態能夠利用百度的抓取推廣
	
#不能經過URL準確的判斷一個網頁是靜態頁仍是動態頁	
不一樣的服務,僞靜態的作法不同,具體參考百度

#僞靜態的優缺點:
1,文件存檔,靜態化頁面存在服務器,僞靜態化調用數據庫虛擬生成的(實際仍是動態)
2,性能優點,在用戶訪問量大的時候,靜態頁面比僞靜態更具優點(由於僞靜態須要嗲用數據庫)
3,安全性能,基本上差很少都是靜態化狀態(僞靜態僞造了服務器的真實路徑)
4,實際適用,僞靜態比靜態化更靈活,部分無法靜態化的,均可以僞靜態化。
5.僞靜態不能被超連接

rewrite 企業應用場景

Nginx的rewrite功能在企業裏應用很是普遍:php

能夠**調整用戶瀏覽的URL**,看起來更規範,合乎開發及產品人員的需求。

爲了讓搜索引擎搜錄網站內容及用戶體驗更好,企業會**將動態URL地址假裝成靜態地址**提供服務。

網址換新域名後,讓舊的訪問**跳轉**到新的域名上。例如,訪問京東的360buy.com會跳轉到jd.com

根據特殊變量、目錄、客戶端的信息進行URL**調整**等

Rewrite配置示例html

句法:Syntax:  rewrite regex replacement [flag]
默認:Default: --
語境:Context: server,location,if

#用於切換維護頁面場景
#rewrite ^(.*)$ /page/maintain.html break;		#$1?

ngx_http_rewrite_module模塊----Rewrite標記Flag

rewrite指令根據表達式來重定向URL,或者修改字符串,能夠應用於server,location,if環境下,每行rewrite指令最後跟一個flag標記,支持的flag標記有以下表格所示:前端

flag 做用
last 本條規則匹配完成後,中止匹配,再匹配後面的規則(由於從新發送了一次請求)
break 本條規則匹配完成後,中止匹配,再也不匹配後面的規則(404)
redirect 返回302臨時重定向,地址欄會顯示跳轉後的地址
permanent 返回301永久重定向,地址欄會顯示跳轉後的地址(默認)

^(.*) URLlinux

^(.*)$ URLnginx

^/2018/(.*)$ 某一段URIweb

(.*) URI數據庫

rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect; 變量json

^(.*) https://$server_name$1vim


last與break區別對比示例後端

[root@web01 conf.d]# vim /etc/nginx/conf.d/rewrite.conf 
server {
        listen 80;
        server_name rw.com;

        location ~ ^/break {
                rewrite ^/break /test/ break;
        }
        location ~ ^/last {
                rewrite ^/last /test/ last;
        }
        location /test/ {
                default_type application/json;
                return 200 "ok";
        }
}

#重啓nginx服務
[root@web01 conf.d]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# nginx -s reload

#/test目錄不存在,也能夠return
return不支持正則,因此不支持複雜的重定向 ,
return須要指定狀態碼,rewrite不須要指定狀態碼

#有沒有root ,不會影響URL的跳轉,只是沒有默認的location

QQ截圖20200602183214.png
QQ截圖20200602183142.png
QQ截圖20200602183120.png

redirect與permanent區別對比示例

域名的跳轉

[root@web01 conf.d]# vim /etc/nginx/conf.d/rw.conf 
server {
        listen 80;
        server_name rw.com;
        root /code;

        location /test {
                rewrite ^(.*)$  https://blog.driverzeng.com redirect;
                #rewrite ^(.*)$  https://blog.driverzeng.com permanent;
                #return 302 https://blog.driverzeng.com;
                #return 301 https://blog.driverzeng.com;
        }
}

redirect: 每次請求都會詢問服務器,若是當服務器不可用時,則會跳轉失敗。

permanent: 第一次請求會詢問客戶端,瀏覽器會記錄跳轉的地址,第二次則再也不詢問服務器,直接經過瀏覽器緩存的地址跳轉。第二次的訪問與nginx服務器無關,(除非客戶端清除緩存)

臨時重定向:
	1.用戶的訪問(考慮到用戶是否刷新瀏覽器)
	2.http 轉https

永久重定向:

#瀏覽器輸入 rw.com或者rw.com/test	都是一樣的跳轉

QQ截圖20200602184503.png
QQ截圖20200602184748.png


開啓rewrite日誌

[root@web01 code]# vim /etc/nginx/nginx.conf

/var/log/nginx/error.log warn; ==> /var/log/nginx/error.log notice;

http{
    rewrite_log on;
}

    注意兩點:
    1)在http字段加,反正我試了一下,在配置文件的開始位置,nginx會報rewrite_log 是一個不識別的變量。
        
     2)注意 error_log的級別是notice,不然不會顯示其具體的匹配過程,但須要注意,最好在測試環境下調試,error_log會增加的很快,咱們網站20分鐘5G多,因此在線上調試完了,儘快關閉!!!或說改爲error級別的。

tNeswq.md.png
tNeyT0.md.png


案例一----------------文件的跳轉

用戶訪問/abc/1.html實際上真實訪問的是/ccc/bbb/2.html

#http://rw.com/abc/1.html  ==>  http://rw.com/ccc/bbb/2.html

#1.準備真實訪問路徑
[root@web03 ~]# mkdir /code/ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html

#2.Nginx跳轉配置
[root@web03 conf.d]# vim /etc/nginx/conf.d/rw.conf 
server {
        listen 80;
	    server_name rw.com/;
	    
        location / {
                root /code;
                index index.html;
        }
        location /abc {
                rewrite (.*) /ccc/bbb/2.html redirect;
                rewrite (.*) https://$host/ccc/bbb/2.html redirect;
                #rewrite ^(.*)$ https://$host//ccc/bbb/2.html redirect;
                #return 302 /ccc/bbb/2.html;
        }
}

#3.重啓Nginx服務
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.d]# nginx -s reload

location /abc === location ^/abc 
(.*) 指的是URL,不帶http://
^(.*)$ 指的是URL,帶http://

$host 域名

案例二-------文件的跳轉

用戶訪問/2018/ccc/2.html實際上真實訪問的是/2014/ccc/bbb/2.html

##http://rw.com/2018/ccc/2.html  ==>  http://rw.com/2014/ccc/bbb/2.html

#1.準備真是的訪問路徑
[root@web03 conf.c]# mkdir /code/2014/ccc/bbb -p 
[root@web03 conf.c]# echo "2014_ccc_bbb_2" > /code/2014/ccc/bbb/2.html

#2.Nginx跳轉配置
[root@web03 conf.d]# vim /etc/nginx/conf.d/rw.conf 
server {
        listen 80;
	    server_name rw.com;
	    
        location / {
                root /code;
                index index.html;
        }
        location /2018 {
                rewrite /ccc/2.html /ccc/bbb/2.html redirect;
                #rewrite ^/2018/(.*)$ /2014/$1 redirect;
        }
}

#3.重啓nginx服務
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.d]# nginx -s reload

^/2018/(.*)$中,^$能夠省略
$1至關於sed的後向引用

案例三---------域名的跳轉

用戶訪問/test實際上真實訪問的是https://blog.driverzeng.com

#1.Nginx跳轉配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;
		server_name localhost;
        location /test {
                rewrite (.*) https://blog.driverzeng.com redirect;
        }
}

#2.重啓nginx服務
[root@web03 conf.d]# nginx -s reload

案例四--文件的跳轉

用戶訪問couese-11-22-33.html實際上真實訪問的是/course/11/22/33/course_33.html

#http://www.drz.com/couese-11-22-33.html  ==>  http://www.drz.com/course/11/22/33/course_33.html

#1.準備真是的訪問路徑
[root@web03 ~]# mkdir /code/course/11/22/33 -p
[root@web03 ~]# echo "curl docs.etiantian.org" > /code/course/11/22/33/course_33.html

#2.Nginx跳轉配置
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;
		server_name localhost;
        root /code;
        index index.html;
        
        location / {
                #靈活配法
                rewrite ^/course-(.*)-(.*)-(.*).html$ 	/course/$1/$2/$3/course_$3.html redirect;
                #固定配法
                #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
        }
}

#3.重啓nginx服務
[root@web03 conf.d]# nginx -s reload

案例五--------------域名和端口的跳轉

http請求跳轉到https

#Nginx跳轉配置
server {
        listen 80;
        server_name rw.com;
        rewrite ^(.*) https://$server_name$1 redirect;
        #return 302 https://$server_name$request_uri;
}       

server {
        listen 443;
        server_name blog.driverzeng.com;
        ssl on;
}

$server_name$1,加不加$1 同樣,可是儘可能加上

實戰---僞靜態

server {
        listen 80;
        server_name discuz.drz.com;

        location / {
                root /code/discuz/upload;
                index index.php index.html;
                rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
                rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
                rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
                rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
                rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
                rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
                rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
                rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
                if (!-e $request_filename) {
                    return 404;
                }
        }

        location ~ \.php$ {
                root /code/discuz/upload;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

Rewrite與Nginx全局變量

Rewrite在匹配過程當中,會用到一些Nginx全局變量

$server_name    #當前用戶請求的域名

server {
        listen 80;
        server_name test.drz.com;
        rewrite ^(.*)$ https://$server_name$1;
}
$request_filename 請求的文件路徑名(帶網站的主目錄/code/images/test.jpg)	#
$request_uri 當前請求的文件路徑(不帶網站的主目錄/inages/test.jpg)

#大多數用於http協議轉https協議
server {
        listen 80;
        server_name php.drz.com;
        return 302 https://$server_name$request_uri;
}
$scheme 用的協議,好比http或者https

如何更加規範的書寫Rewrite規則

server {
        listen 80;
        server_name www.drz.com drz.com;
        if ($http_host = drz.com){
            rewrite (.*) http://www.drz.com$1;
        }
}

# $http_host 

#推薦書寫格式
server {
        listen 80;
        server_name drz.com;
        rewrite ^(.*)$ http://www.drz.com$request_uri;
}
server {
        listen 80;
        server_name www.drz.com;
}
相關文章
相關標籤/搜索