nginx常用rewrite

2021/5/22 7:27:50

本文主要是介绍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;
}

  

 



这篇关于nginx常用rewrite的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程