溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Nginx網站常見的跳轉配置實例

發布時間:2020-07-04 18:54:14 來源:網絡 閱讀:28080 作者:2012hjtwyf 欄目:建站服務器


相信大家在日常運維工作中如果你用到nginx作為前端反向代理服務器的話,你會對nginx的rewrite又愛又恨,愛它是因為你搞定了它,完成了開發人員的跳轉需求后你會覺得很爽,覺得真的很強大,恨它是因為當一些稀奇古怪跳轉的需求時候你會沒有頭緒、百般調試、上網求神拜佛都搞不定的時候真是想死的心都有了,當然網上也有很多nginx rewrite的經典示例,但是我感覺對我的工作都沒有太大的幫助

下面是我工作中遇到的一些rewrite示例。提供給大家分享


正則表達式匹配,其中:

  1. * ~ 為區分大小寫匹配

  2. * ~* 為不區分大小寫匹配

  3. * !~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配

文件及目錄匹配,其中:

  1. * -f和!-f用來判斷是否存在文件

  2. * -d和!-d用來判斷是否存在目錄

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

  4. * -x和!-x用來判斷文件是否可執行

flag標記有:

  1. * last 相當于Apache里的[L]標記,表示完成rewrite

  2. * break 終止匹配, 不再匹配后面的規則

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

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


1、換域名后導流到新域名

需要將之前用的www.a.com域名的流量全部跳轉到www.b.com
實現效果:比如訪問 www.a.com/123.html自動跳到www.b.com/123.html

我們用nginx實現

cd /etc/nginx/sites-available

vi mysite


增加rewrite命令

server {

    listen                   80;

    server_name      www.a.com;

    rewrite  ^/(.*)$     http://www.b.com/$1 permanent;

}


2、主域名跳轉到www域名

比如將主域名xxx.com 跳轉到www.xxx.com

cd /etc/nginx/sites-available

vi mysite


#主域名跳轉到www域名

server {

    listen                80;

    server_name    xxx.com;

    rewrite  ^/(.*)$  http://www.xxx.com/$1 permanent;

}


server {

    listen                80;

    server_name   www.xxx.com;

    #已省略余下通用配置內容

}


3、主目錄跳轉,子目錄不跳轉

a.com和www.a.com都跳到www.b.com
www.a.com/123不跳

server {

        listen               80;

        server_name  www.a.us a.us;


        #根目錄跳轉

        location / {

                rewrite .+ http://www.b.com/ permanent;

        }


        #非根目錄本地執行

        location ~* /.+ {

            #已省略余下通用配置內容

        }

}


4、301跳轉設置:

server {
       listen                   80;
       server_name     123.com;
       rewrite ^/(.*) http://456.com/$1 permanent;
       access_log off;
}

permanent – 返回永久重定向的HTTP狀態301


5、302跳轉設置:

server {
       listen                    80;
       server_name      123.com;
       rewrite ^/(.*) http://456.com/$1 redirect;
       access_log off;
}

redirect – 返回臨時重定向的HTTP狀態302


6、禁止htaccess

location ~//.ht {

         deny all;

}


7、禁止多個目錄

location ~ ^/(cron|templates)/ {

         deny all;

         break;

 }


8、禁止以/data開頭的文件
可以禁止/data/下多級目錄下.log.txt等請求;

location ~ ^/data {

         deny all;

}


9、禁止單個目錄
不能禁止.log.txt能請求

location /searchword/cron/ {

         deny all;

 }


10、禁止單個文件

location ~ /data/sql/data.sql {

         deny all;

}


11、給favicon.ico和robots.txt設置過期時間;
這里為favicon.ico為99天,robots.txt為7天并不記錄404錯誤日志

location ~(favicon.ico) {

                 log_not_found off;

                 expires 99d;

                 break;

}

 

location ~(robots.txt) {

                 log_not_found off;

                 expires 7d;

                 break;

}


12、設定某個文件的過期時間;這里為600秒,并不記錄訪問日志

location ^~ /html/scripts/loadhead_1.js {

                 access_log   off;

                 root /opt/lampp/htdocs/web;

                 expires 600;

                 break;

}


13、文件反盜鏈并設置過期時間
這里的return 412 為自定義的http狀態碼,默認為403,方便找出正確的盜鏈的請求
“rewrite ^/ https://cache.yisu.com/upload/information/20200309/32/49780.jpg;”顯示一張防盜鏈圖片
“access_log off;”不記錄訪問日志,減輕壓力
“expires 3d”所有文件3天的瀏覽器緩存


location ~* ^.+/.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {

valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;

if ($invalid_referer) {

    rewrite ^/ https://cache.yisu.com/upload/information/20200309/32/49780.jpg;

    return 412;

    break;

}

    access_log   off;

    root /opt/lampp/htdocs/web;

    expires 3d;

    break;

}


14、只充許固定ip訪問網站,并加上密碼

root  /opt/htdocs/www;

allow   208.97.167.194;

allow   222.33.1.2;

allow   231.152.49.4;

deny    all;

auth_basic "C1G_ADMIN";

auth_basic_user_file htpasswd;


15、將多級目錄下的文件轉成一個文件,增強seo效果

/job-123-456-789.html 指向/job/123/456/789.html

rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)/.html$ /job/$1/$2/jobshow_$3.html last;


16、將根目錄下某個文件夾指向2級目錄
如/shanghaijob/ 指向 /area/shanghai/
如果你將last改成permanent,那么瀏覽器地址欄顯是/location/shanghai/


rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

上面例子有個問題是訪問/shanghai 時將不會匹配


rewrite ^/([0-9a-z]+)job$ /area/$1/ last;

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

這樣/shanghai 也可以訪問了,但頁面中的相對鏈接無法使用,
如./list_1.html真實地址是/area/shanghia/list_1.html會變成/list_1.html,導至無法訪問


那我加上自動跳轉也是不行咯
(-d $request_filename)它有個條件是必需為真實目錄,而我的rewrite不是的,所以沒有效果

if (-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}


知道原因后就好辦了,讓我手動跳轉吧

rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;


17、文件和目錄不存在的時候重定向:

if (!-e $request_filename) {

         proxy_pass http://127.0.0.1;

}


18、域名跳轉

server

     {

             listen       80;

             server_name  jump.c1gstudio.com;

             index index.html index.htm index.php;

             root  /opt/lampp/htdocs/www;

             rewrite ^/ http://www.c1gstudio.com/;

             access_log  off;

}


19、多域名轉向

server_name  www.c1gstudio.com www.c1gstudio.net;

             index index.html index.htm index.php;

             root  /opt/lampp/htdocs;

             if ($host ~ "c1gstudio/.net") {

                 rewrite ^(.*) http://www.c1gstudio.com$1 permanent;

}


20、三級域名跳轉

if ($http_host ~* "^(.*)/.i/.c1gstudio/.com$") {

    rewrite ^(.*) http://top.yingjiesheng.com$1;

    break;

}



向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女