nginx常用rewrite


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

 

// 根目錄放靜態文件重寫
location /web/{
index index.html;
try_files $uri $uri/ /web/index.html;
}

//重寫 二級目錄指向上級目錄
location ^~ /h5/upload/ {
rewrite /h5/upload/(.*)$ /upload/$1 last;
}


//反向代理

location /api {

rewrite ^.+api/?(.*)$ /$1 break;

proxy_pass http://www.baidu.com; #node api server 即需要代理的IP地址

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
location /undefined {

rewrite ^.+undefined/?(.*)$ /$1 break;

proxy_pass http://google.com; #node api server 即需要代理的IP地址

}

 

 

nginx目錄路徑重定向

1 nginx修改root映射

location  /api{
    root   /web;
}

2 通過nginx rewrite內部跳轉實現訪問重定向

location /api{
    rewrite ^/api/(.*)$     /web/api/$1 last;
}

3 nginx設置別名alias映射實現

location  /api{
    alias  /web/api;  #這里寫絕對路徑
}

4 通過nginx的permanent 301絕對跳轉實現

location /api{
    rewrite ^/api/(.*)$   http://demo.com/web/api/$1;
}

5 通過判斷uri實現頁面跳轉

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

  

 


免責聲明!

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



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