1、假設(shè)要把webroot/static/index.html訪問重定向到static/index.html
例如當(dāng)我們通過瀏覽器訪問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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。