nginx跳轉案例詳解


一、跳轉狀態

nginx的跳轉狀態有兩種,301重定向是永久的重定向,搜索引擎會抓取新的內容的同時將舊的地址替換為重定向后的網址;302重定向是暫時的重定向,搜索引擎會抓取新的內容而保留舊的地址。因為服務器返回302,所以搜索引擎會認為新的網址是暫時的。

二、跳轉狀態的寫法

return指令在301跳轉上比rewrite指令性能上更加有優勢。雖然在訪問量不大的情況下幾種寫法的性能表現上區別並不大,不過在海量訪問中一個小小的優化也能在提升業務系統性能上起到不小的作用

301跳轉的兩種寫法:

rewrite ^(.*) https://www.test.com$1 permanent;
rewrite ^(.*)$ $host$1 permanent;
rewrite ^ https://www.test.com$request_uri? permanent;

return 301 $scheme://www.test.com$request_uri;
return 301 https://www.test.com$request_uri;

302跳轉的兩種寫法:

rewrite ^(.*) https://www.test.com$1 redirect;
rewrite ^(.*)$ $host$1 redirect;
rewrite ^ https://www.test.com$request_uri? redirect;

return 302 $scheme://www.test.com$request_uri;
return 302 https://www.test.com$request_uri;

三、匹配規則

正則表達式匹配,其中:

  • ~ 為區分大小寫匹配

  • ~* 為不區分大小寫匹配

  • !和!*分別為區分大小寫不匹配及不區分大小寫不匹配

文件及目錄匹配,其中:

  • -f和!-f用來判斷是否存在文件

  • -d和!-d用來判斷是否存在目錄

  • -e和!-e用來判斷是否存在文件或目錄

  • -x和!-x用來判斷文件是否可執行

flag標記有:

  • last 相當於Apache里的[L]標記,表示完成rewrite

  • break 終止匹配, 不再匹配后面的規則

  • redirect 返回302臨時重定向 地址欄會顯示跳轉后的地址

  • permanent 返回301永久重定向 地址欄會顯示跳轉后的地址

四、跳轉實例

1、訪問www.a.com域名全部跳轉到www.b.com

server_name      www.a.com;
rewrite  ^/(.*)$   $scheme://www.b.com/$1 permanent;

2、主域名跳轉www

server_name      a.com www.a.com;
return 301 $scheme://www.a.com$request_uri;

3、主目錄跳轉,子目錄不跳轉,a.com和www.a.com都跳到www.b.com,而www.a.com/123不跳

server {
    listen               80;
    server_name  a.com www.a.com;
    #根目錄跳轉
    location / {
            rewrite .+ http://www.b.com/ permanent;
    }
    #非根目錄本地執行
    location ~* /.+ {
    #已省略余下通用配置內容
    }
}

4、如果頁面或目錄不存在將直接跳轉至網站首頁

location / {
	root    /usr/share/nginx/html/;
    index  index.html index.htm;
	if (!-e $request_filename) {
    return 301 $scheme://$host;
    break;
   }
}

5、所有頁面跳轉指定頁面

(1)對於靜態頁面

if ($request_uri !~* "^/test/$") {
   rewrite ^(.*) https://www.a.com/repair/ permanent;
}

(2)對於非靜態頁面(含有css和圖片等非鏈接地址)

set $flag 0;
if ($request_uri !~* "\.(jpg|jpeg|png|gif|ico|css|js|gz)$") {
	set $flag "${flag}1";
}
if ($request_uri !~* "repair") {
	set $flag "${flag}2";
}
if ($flag = "012") {
	rewrite ^/(.*) /test/index.html permanent;
}

6、www.a.com/test/123.html 跳轉為 test/?cd=123

rewrite "/test/(.*).html$" /test/?cd=$1 last;

7、www.a.com/test/ 下跳轉為www.a.com/test/index.php?s=

if (!-e $request_filename){
rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;
}

8、在后面添加/index.php?s=

if (!-e $request_filename){
  rewrite ^/(.*)$ /index.php?s=/$1 last;
}

9、www.a.com/test/123.html 等test下面數字+html的鏈接跳轉為404

rewrite ^/test/([0-9]+)\.html$ /404.html last;

10、nginx 強制跳轉https

    if ($server_port = 80){ 
	return 301 https://$server_name$request_uri;} 
        或者
	if ($scheme = http){ 
	return 301 https://$server_name$request_uri;
} 

五、實例配置

1、文件服務器配置

location / {
          root /data/software/;
          index _;
          autoindex on; # 開啟目錄文件列表
          autoindex_exact_size on; # 顯示出文件的確切大小,單位是bytes
          autoindex_localtime on; # 顯示的文件時間為文件的服務器時間
          charset utf-8,gbk; # 避免中文亂碼
     }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM