openresty ngx_lua重定向

ngx.redirect:https://github.com/openresty/lua-nginx-module#ngxredirect

ngx.req.set_uri:https://github.com/openresty/lua-nginx-module#ngxreqset_uri

ngx.req.set_uri_args:http://​ https://github.com/openresty/lua-nginx-module#ngxreqset_uri_args

ngx.exec:http://​ https://github.com/openresty/lua-nginx-module#ngxexec

重定向

ngx.redirect:请求重定向

语法格式:ngx.redirect(uri, status?)
* 如果uri包含不安全的字符(控制字符),函数返回lua error
* status可选值:301、302(默认,临时重定向)、303、307、308环境:rewrite_by_lua*, access_by_lua*, content_by_lua*# 示例
return ngx.redirect("/foo")
return ngx.redirect("/foo", 301)
return ngx.redirect("/foo", 302)
return ngx.redirect("/foo", ngx.HTTP_MOVED_TEMPORARILY)return ngx.redirect("http://www.google.com")rewrite ^ /foo? redirect;  <==> return ngx.redirect('/foo')
rewrite ^ /foo? permanent; <==> return ngx.redirect('/foo', 301)return ngx.redirect('/foo?a=3&b=4')
* 带参数重定向

ngx.req.set_uri:请求重定向

语法格式:ngx.req.set_uri(uri, jump?, binary?)
* uri重写的路径参数,长度不能为0,否则会报错
* jump:是否进行服务端跳转,默认false环境:set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*# 示例
rewrite ^ /foo last;   <==>  ngx.req.set_uri("/foo", true)rewrite ^ /foo break; <==>  ngx.req.set_uri("/foo", false)ngx.req.set_uri("/foo")location /test {rewrite_by_lua_block {local uri = ngx.re.sub(ngx.var.uri, "^/test/(.*)", "/$1", "o")ngx.req.set_uri(uri)}proxy_pass http://my_backend;}<==>location /test {rewrite ^/test/(.*) /$1 break;proxy_pass http://my_backend;}

ngx.req.set_uri_args:设置ngx.req.set_uri的重定向参数

语法格式:ngx.req.set_uri_args(args)环境:context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*# 示例ngx.req.set_uri_args("a=3&b=hello%20world")ngx.req.set_uri_args({ a = 3, b = "hello world" })ngx.req.set_uri_args({ a = 3, b = {5, 6} })rewrite ^ /foo?a=3? last;  <==>ngx.req.set_uri_args("a=3")ngx.req.set_uri("/foo", true) <==>ngx.req.set_uri_args({a = 3})ngx.req.set_uri("/foo", true)

ngx.exec:内部重定向

语法格式:ngx.exec(uri, args?)环境:rewrite_by_lua*, access_by_lua*, content_by_lua*# 示例ngx.exec('/some-location')ngx.exec('/some-location', 'a=3&b=5&c=6')ngx.exec('/some-location?a=3&b=5', 'c=6')ngx.exec("/foo", { a = 3, b = "hello world" })location /foo {content_by_lua_block {ngx.exec("@bar", "a=goodbye")}}location @bar {content_by_lua_block {local args = ngx.req.get_uri_args()for key, val in pairs(args) doif key == "a" thenngx.say(val)endend}}

使用示例

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/local/openresty/nginx/html2;if ( !-e $request_filename ){#rewrite ^/break/(.*)  /default/info  break;rewrite_by_lua_block {ngx.req.set_uri("/default/info", false);}}}#location 2location /break2 {root /usr/local/openresty/nginx/html2;if ( !-e $request_filename ){#rewrite ^/break2/(.*)  /default/info  break;rewrite_by_lua_block {ngx.req.set_uri("/default/info", false);}echo "break";}}#location 3location /last {if ( !-e $request_filename ){#rewrite ^/last/(.*)  /test/$1  last;rewrite_by_lua_block {ngx.req.set_uri("/test/$1", true);}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 redirect2 % pwd
/Users/huli/lua/openresty/redirect2
huli@hudeMacBook-Pro redirect2 % ls html
default
huli@hudeMacBook-Pro redirect2 % ls html/default
info
huli@hudeMacBook-Pro redirect2 % cat html/default/info
break info

创建容器

huli@hudeMacBook-Pro redirect2 % docker run -it -d -p 4005:80 \
-v /Users/huli/lua/openresty/redirect2/html:/usr/local/openresty/nginx/html2 \
-v /Users/huli/lua/openresty/redirect2/default.conf:/etc/nginx/conf.d/default.conf \
--name open9 lihu12344/openresty

使用测试

huli@hudeMacBook-Pro redirect2 % curl localhost:4005/break
break info
huli@hudeMacBook-Pro redirect2 % curl localhost:4005/break/1
break infohuli@hudeMacBook-Pro redirect2 % curl localhost:4005/break2
breakhuli@hudeMacBook-Pro redirect2 % curl localhost:4005/last
test

使用示例 2

default2.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;rewrite_by_lua_block {ngx.redirect("/test/$1", 302);}}}location /permanent {if ( !-e $request_filename ) {#rewrite ^/permanent/(.*)  /test/$1 permanent;rewrite_by_lua_block {ngx.redirect("/test/$1", 301);}}}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 4006:80 \
-v /Users/huli/lua/openresty/redirect2/default2.conf:/etc/nginx/conf.d/default.conf \
--name open10 lihu12344/openresty

使用测试

huli@hudeMacBook-Pro redirect2 % curl -I localhost:4006/redirect
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.21.4.1
Date: Mon, 11 Jul 2022 01:22:30 GMT
Content-Type: text/html
Content-Length: 151
Connection: keep-alive
Location: /test/$1huli@hudeMacBook-Pro redirect2 % curl -I localhost:4006/redirect/1
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.21.4.1
Date: Mon, 11 Jul 2022 01:22:32 GMT
Content-Type: text/html
Content-Length: 151
Connection: keep-alive
Location: /test/$1huli@hudeMacBook-Pro redirect2 % curl -I localhost:4006/permanent
HTTP/1.1 301 Moved Permanently
Server: openresty/1.21.4.1
Date: Mon, 11 Jul 2022 01:22:35 GMT
Content-Type: text/html
Content-Length: 175
Connection: keep-alive
Location: /test/$1huli@hudeMacBook-Pro redirect2 % curl -I localhost:4006/permanent/1
HTTP/1.1 301 Moved Permanently
Server: openresty/1.21.4.1
Date: Mon, 11 Jul 2022 01:22:38 GMT
Content-Type: text/html
Content-Length: 175
Connection: keep-alive
Location: /test/$1

openresty ngx_lua重定向相关推荐

  1. openresty ngx_lua常用指令

    openresty ngx_lua常用指令 请求中断 ngx.exit:请求中断 语法格式:ngx.exit(status) * status >= 200:立刻中断请求,返回状态码 * sta ...

  2. openresty ngx_lua请求响应

    openresty ngx_lua请求响应 请求头操作 ngx.req.set_header:添加.修改请求头 语法格式:ngx.req.set_header(name, value) * name ...

  3. openresty ngx_lua日志操作

    openresty ngx_lua日志操作 日志操作 ngx.log:向日志文件输出日志 语法格式:ngx.log(log_level, ...)环境:init_by_lua*, init_worke ...

  4. openresty ngx_lua获取环境变量

    openresty ngx_lua获取环境变量 环境变量 ngx.config.subsystem:当前请求的nginx环境 语法格式:subsystem = ngx.config.subsystem ...

  5. 什么是OpenResty

    OpenResty是一个成熟的网络平台,它集成了标准的Nginx核心,LuaJIT,许多精心编写的Lua库,许多高质量的第三方Nginx模块以及大多数外部依赖项.它旨在帮助开发人员轻松构建可伸缩的We ...

  6. 用Nginx+Lua(OpenResty)开发高性能Web应用

    在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开 ...

  7. 使用Nginx+Lua(OpenResty)开发高性能Web应用

    在互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Nginx开 ...

  8. 「京东开涛」使用Nginx+Lua(OpenResty)开发高性能Web应用

    几乎所有互联网公司,Nginx可以说是标配组件,但是主要场景还是负载均衡.反向代理.代理缓存.限流等场景:而把Nginx作为一个Web容器使用的还不是那么广泛.Nginx的高性能是大家公认的,而Ngi ...

  9. HHVM安装使用教程

    HHVM即HipHop Virtual Machine,目前是3.0版本,出自Facebook,它先将PHP和Hack程序编译为中间字节码,然后通过just-in-time (JIT)编译器将字节码动 ...

最新文章

  1. ofstream写不进txt文件的奇怪问题
  2. 深入学习SAP UI5框架代码系列之四:SAP UI5控件的元数据实现
  3. redis watch multi exec 关系
  4. sharepoint 弹出框
  5. mllib协同过滤 java实现_协同过滤(ALS)算法介绍及Spark MLlib调用实例(Scala/Java/Python)...
  6. java的标量和聚合量_JVM 角度看代码优化
  7. 清除用友所有单据锁定的SQL语句
  8. 经典神经网络 -- DenseNet : 设计原理与pytorch实现
  9. Redis与LRU实现
  10. 宋宝华:Linux的任督二脉——进程调度和内存管理
  11. 校园二手交易系统平台
  12. 在win10 LTSB版本中使用UWF组件,实现影子保护功能,提供稳定,高速的开发环境...
  13. input隐藏域赋值数组,node获取val的值
  14. Taulia任命Todd Musselman为首席福祉官
  15. Tampermonkey脚本
  16. word2vec(2) 背景知识
  17. ubuntu12.04将tgz文件解压到指定目录
  18. 工商管理专业知识与实务(初级)【6】
  19. FastDFS 单机安装
  20. 16进制 dat matlab_用MATLAB生成DAT文件

热门文章

  1. [ubuntn]常用软件安装方法
  2. ps软件打不开计算机受限制,w10打不开ps怎么解决
  3. PS学习_1-软件下载与破解
  4. CentOS6.5--修改系统语言为中文或英文,文件夹切换语言
  5. 简单计算机java程序_JAVA程序员需要知道的计算机底层基础10-操作系统引导程序的简单...
  6. Java编程笔记9:容器(下)
  7. 关系网络lbs的应用_冒泡网王江:熟人关系将成LBS最重要商业模式
  8. 求解带不确定事件的FJSP的多目标强化学习框架
  9. ORA-29913,ORA-29400,KUP-00554,KUP-01005,KUP-01007 oracle外部表报错解决记录
  10. 一个高中生的编程自学经历