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 重定向相关推荐

  1. openresty开发系列33--openresty执行流程之3重写rewrite和重定向

    openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向 2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令 语法 ...

  2. openresty ngx_lua重定向

    openresty ngx_lua重定向 ngx.redirect:https://github.com/openresty/lua-nginx-module#ngxredirect ngx.req. ...

  3. OpenResty上各种测试用例实操(1)

    下面记录了我阅读<OpenResty最佳实践>过程中对一些例子的实践操作示例,记录以作备忘. 一.Location的组合使用 演示一个location只作为内部调用接口,被另一个locat ...

  4. Openresty最佳案例 | 第4篇:OpenResty常见的api

    获取请求参数 vim  /usr/example/example.conf location /lua_var {default_type 'text/plain';content_by_lua_bl ...

  5. Openresty最佳案例 | 第1篇:Nginx介绍

    Nginx 简介 Nginx是一个高性能的Web 服务器,同时是一个高效的反向代理服务器,它还是一个IMAP/POP3/SMTP 代理服务器. 由于Nginx采用的是事件驱动的架构,能够处理并发百万级 ...

  6. 网关技术选型,为什么选择 Openresty ?事件驱动、协程...

    今天跟大家聊下关于网关的话题 互联网公司,不论体量大小如何,其内部的技术架构基本都是相似的,体现在以下几个方面: 数据量过大,如何定制化存储 访问量高了,如何集群化部署,流量负载均衡 响应速度慢了,如 ...

  7. Openresty使用

    OpenResty是一个全功能的 Web 应用服务器.它打包了标准的 Nginx 核心,常用的第三方模块以及大多数依赖项. 可以把它看成是Nginx附加众多的第三方插件的合集.其主体是嵌入lua脚本的 ...

  8. linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍

    Nginx介绍 Nginx官网(http://nginx.org),最新版1.13,最新稳定版1.12 Nginx应用场景:web服务.反向代理.负载均衡 Nginx著名分支,淘宝基于Nginx开发的 ...

  9. OpenResty 概要及原理科普

    OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并发.扩展性极高的动态 W ...

最新文章

  1. Struts2中导入外部xml文件出现Included file cannot be found错误!
  2. 42、Power Query-Text.Remove函数应用
  3. VMware中怎样克隆虚拟机
  4. 操作系统 第五章【虚拟存储器】【MOOC答案】
  5. ldconfig deferred processing now taking place
  6. 【转】EF三种编程方式的区别Database first ,Model first ,code first
  7. orderd mysql_Oracle入门教程:leading vs ordered hint
  8. mysql 5.6 设置long_query_time的值无效的原因
  9. 有没有人带?这些都是学习生信的一大助力!
  10. 征服Perl——Linux下安装Perl——里程碑M2
  11. java jhap_Java-Live与JVAP工具的Max
  12. UML图各类符号含义
  13. 计算机信应用技术,计算机信息应用技术.ppt
  14. Graphpad prism 使用教程汇总(更新)
  15. 海康威视摄像头-chrome谷歌浏览器高版本局域网下海康摄像头web网页实时播放
  16. 系统优化的基本思想点
  17. led伏安特性实验误差分析_为什么非线性电阻元件伏安特性曲线的误差分析?
  18. Javaweb酒店预约管理系统(框架SpringBoot+Vue)
  19. UI必备 PS圆角Corner Editor插件
  20. 晶豪(ESMT)nand flash对内存页的读取、ECC校验 F59L2G81A

热门文章

  1. 计算机网络技术专业做销售,现在在书店做销售员的实习报告怎样写?
  2. 一种自动化运维架构的概述
  3. dota 屏蔽快键键
  4. jsp 事务 反射 基础基础知识
  5. Spring @Conditional注解 详细讲解及示例
  6. 如何修改wincc服务器画面,wincc画面怎么调大
  7. Android MediaPlayer seekTo不准确问题
  8. SQLAlchemy使用教程
  9. 支付宝外卡收单 使用手册(一) COPYandPAY
  10. 解决 “ping: www.baidu.com: 未知的名称或服务”的问题