openresty 重定向
openresty 重定向
重定向
重定向:避免网站路径、域名修改后,网站中原有链接失效
命令格式:rewrite old_url new_url [flag]
old_url:旧的url路径,可为正则表达式
new_url:新的跳转的url,匹配正则表达式后,跳转到该地址# flag:进一步处理的标识
last:终止rewrite,服务端跳转到指定路径
break:终止rewrite,不再继续匹配
redirect:临时重定向,返回响应码302
permanent:永久重定向,返回响应码301
示例
default.conf
server {listen 80;server_name localhost;location / {root /usr/local/openresty/nginx/html;index index.html index.htm;}#location 1location /break {root /usr/share/nginx/html2;if ( !-e $request_filename ){ #root目录下查找文件/default/inforewrite ^/break/(.*) /default/info break;}}#location 2location /break2 {root /usr/share/nginx/html2;if ( !-e $request_filename ){ #root目录下查找文件/default/info,#随后继续执行下面语句,echo "break"rewrite ^/break2/(.*) /default/info break;echo "break";}}#location 3location /last {if ( !-e $request_filename ){ #匹配后,跳转到/test/$1路径rewrite ^/last/(.*) /test/$1 last;echo "last";}}#location 4location /test {echo "test";}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/local/openresty/nginx/html;}}
本地文件
huli@hudeMacBook-Pro redirect % pwd
/Users/huli/lua/openresty/redirecthuli@hudeMacBook-Pro redirect % ls html
default
huli@hudeMacBook-Pro redirect % ls html/default
info
huli@hudeMacBook-Pro redirect % cat html/default/info
break info
创建容器
docker run -it -d -p 1000:80 \
-v /Users/huli/lua/openresty/redirect/html:/usr/local/openresty/nginx/html2 \
-v /Users/huli/lua/openresty/redirect/default.conf:/etc/nginx/conf.d/default.conf \
--name open2 lihu12344/openresty
使用测试
huli@hudeMacBook-Pro redirect % curl localhost:1000/break/1
break infohuli@hudeMacBook-Pro redirect % curl localhost:1000/break2/1
breakhuli@hudeMacBook-Pro redirect % curl localhost:1000/last
last
huli@hudeMacBook-Pro redirect % curl localhost:1000/last/1
testhuli@hudeMacBook-Pro redirect % curl localhost:1000/test
test
示例 2
default.conf
server {listen 80;server_name localhost;#charset koi8-r;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}location /redirect {if ( !-e $request_filename ) {rewrite ^/redirect/(.*) /test/$1 redirect;}}location /permanent {if ( !-e $request_filename ) {rewrite ^/permanent/(.*) /test/$1 permanent;}}location /test {echo "test";}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}}
创建容器
docker run -it -d -p 2000:80 \
-v /Users/huli/lua/openresty/redirect/default2.conf:/etc/nginx/conf.d/default.conf \
--name open3 lihu12344/openresty
使用测试
huli@hudeMacBook-Pro redirect % curl -I localhost:2000/redirect/1
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.21.4.1
Date: Mon, 04 Jul 2022 04:50:08 GMT
Content-Type: text/html
Content-Length: 151
Location: http://localhost/test/1
Connection: keep-alivehuli@hudeMacBook-Pro redirect % curl -I localhost:2000/permanent/1
HTTP/1.1 301 Moved Permanently
Server: openresty/1.21.4.1
Date: Mon, 04 Jul 2022 04:50:17 GMT
Content-Type: text/html
Content-Length: 175
Location: http://localhost/test/1
Connection: keep-alive
openresty 重定向相关推荐
- openresty开发系列33--openresty执行流程之3重写rewrite和重定向
openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向 2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令 语法 ...
- openresty ngx_lua重定向
openresty ngx_lua重定向 ngx.redirect:https://github.com/openresty/lua-nginx-module#ngxredirect ngx.req. ...
- OpenResty上各种测试用例实操(1)
下面记录了我阅读<OpenResty最佳实践>过程中对一些例子的实践操作示例,记录以作备忘. 一.Location的组合使用 演示一个location只作为内部调用接口,被另一个locat ...
- Openresty最佳案例 | 第4篇:OpenResty常见的api
获取请求参数 vim /usr/example/example.conf location /lua_var {default_type 'text/plain';content_by_lua_bl ...
- Openresty最佳案例 | 第1篇:Nginx介绍
Nginx 简介 Nginx是一个高性能的Web 服务器,同时是一个高效的反向代理服务器,它还是一个IMAP/POP3/SMTP 代理服务器. 由于Nginx采用的是事件驱动的架构,能够处理并发百万级 ...
- 网关技术选型,为什么选择 Openresty ?事件驱动、协程...
今天跟大家聊下关于网关的话题 互联网公司,不论体量大小如何,其内部的技术架构基本都是相似的,体现在以下几个方面: 数据量过大,如何定制化存储 访问量高了,如何集群化部署,流量负载均衡 响应速度慢了,如 ...
- Openresty使用
OpenResty是一个全功能的 Web 应用服务器.它打包了标准的 Nginx 核心,常用的第三方模块以及大多数依赖项. 可以把它看成是Nginx附加众多的第三方插件的合集.其主体是嵌入lua脚本的 ...
- linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍
Nginx介绍 Nginx官网(http://nginx.org),最新版1.13,最新稳定版1.12 Nginx应用场景:web服务.反向代理.负载均衡 Nginx著名分支,淘宝基于Nginx开发的 ...
- OpenResty 概要及原理科普
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 W ...
最新文章
- 你曾经是那手握烙铁的少年
- http响应头里没有或者有content-length的几种可能性
- boost::mp11::mp_max_element相关用法的测试程序
- 使用Retrofit和RxJava
- ST:没错,又是我!继续涨价!
- C#系列五《程序的分支》
- CWE视图层级关系解析:节点关系查询
- 小括号教学设计导入_丁文丽《含有小括号的混合运算》教学设计
- 关闭计算机端口的命令行,关闭端口命令,小编教你如何关闭电脑80端口
- 电感式传感器工作原理与电感式传感器应用案例-博扬智能
- 使用detectron2,出现:Consider using one of the following signatures instead:错误解决方法
- THREE 使用Tween插件实现动画
- IP输出不足,端游手游化时代的单一手游企业如何破局?
- 席绢言情系列书评总序
- 服务器 台式机性能比较,服务器与台式机的区别
- 昇腾Mindstudio官方样例黑白图片上色
- 深度学习中AP、mAP、recall、IoU、NMS的评价指标介绍
- Others2_谈谈个人常用的软件
- 什么是电压跟随器?电压跟随器计算方法讲解
- java如何将中文转换成byte数组