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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29fail2ban nginx
- 2024-03-18install nginx proxy manager
- 2024-01-2444从零开始用Rust编写nginx,命令行参数的设计与解析及说明
- 2024-01-22比Nginx更好用的Gateway!
- 2024-01-17nginx daemon off
- 2023-12-30Nginx Vhost Traffic Status 是什么意思-icode9专业技术文章分享
- 2023-12-26使用nginx搭建creates.io镜像(稀疏索引)
- 2023-11-18centos7编译安装Nginx教程。
- 2023-11-02nginx启动命令和停止命令
- 2023-10-25Centos7系统Nginx负载均衡如何安装和配置?