主頁 > 知識庫 > nginx地址重定向的方法

nginx地址重定向的方法

熱門標簽:阿里云 使用U盤裝系統(tǒng) 硅谷的囚徒呼叫中心 智能手機 檢查注冊表項 百度競價點擊價格的計算公式 網(wǎng)站建設(shè) 美圖手機

1、假設(shè)要把webroot/static/index.html訪問重定向到static/index.html

例如當我們通過瀏覽器訪問http://192.168.11.210/webroot/static/index.html,實際訪問的是web目錄下面的static/index.html文件,也及去掉了webroot這個目錄,使用alias

location ^~ /webroot/ {
 alias /data/www/web/WebContent/;
}

注意:

1. 使用alias時,目錄名后面一定要加"/"。

2. alias可以指定任何名稱。

3. alias在使用正則匹配時,必須捕捉要匹配的內(nèi)容并在指定的內(nèi)容處使用。

4. alias只能位于location塊中。[/warning]

http://192.168.11.210/webroot/test/static/index.html

location ^~ /webroot/test/ {
 alias /data/www/web/WebContent/;
}

這樣也是可以的,最終訪問的文件跟上面是一樣的。

2、把對webroot/static/index.html的訪問重定向到web目錄下面的test目錄下

location ~ ^/webroot/ {
 root /data/www/web/WebContent/test/;
}

http://192.168.11.210/webroot/static/index.html 實際訪問的是web目錄下testwebroot/static/index.html
及使用root一般是把訪問目錄重定向到某個目錄下,但是訪問的路徑必須在重新定位的目錄下

注意區(qū)分跟alias的區(qū)別

轉(zhuǎn)載一個:

訪問域名 

www.adc.com/image  自動跳轉(zhuǎn)到  www.adc.com/make/image 

這個如何寫

這種需求有多種方法可以實現(xiàn):

1. 利用Nginx rewrite 內(nèi)部跳轉(zhuǎn)實現(xiàn):

location /image {
     rewrite ^/image/(.*)$   /make/image/$1 last;
}

2.利用alias映射

location /image {
    alias /make/image; #這里寫絕對路徑
}

3.利用root映射:

location /image {
   root  /make;
}

4.利用nginx的permanent 301絕對跳轉(zhuǎn)實現(xiàn)

location /image {
    rewrite ^/image/(.*)$  http://www.adc.com/make/image/$1;
}

5.判斷uri實現(xiàn)

if ( $request_uri ~* ^(/image)){
    rewrite ^/image/(.*)$ /make/image/$1 last; 
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

標簽:山南 通遼 湘潭 煙臺 懷化 湖北 賀州 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標題《nginx地址重定向的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266