gx_Lua模块中的重定向
2017-04-22 19:59
183 查看
在Nginx中实现重定向可以通过rewrite指令,具体可参考《Nginx学习——http_rewrite_module的rewrite指令》
通过Lua模块也可以实现同样的功能,Lua模块提供了相关的API来实现重定向的功能,主要有:
>ngx.exec
语法:ngx.exec(uri, args?)
主要实现的是内部的重定向,等价于下面的rewrite指令
rewrite regrex replacement last;
例子:
注意:
1. 如果给定的uri是命名的location,那么args就会被自动忽略的,如下所示:
2. args参数可以以string的形式给出,也可以以lua table的形式给出,如下所示:
ngx.exec("/foo","a=3&b=hello%20world")
ngx.exec("/foo",{ a= 3,
b="hello world"})
3. 该方法不会主动返回,因此,强烈建议在调用该方法时,最好显示加上return,如下所示:
return ngx.exec(...)
4. 该方法不像ngx.redirect方法,不会产生额外的网络流量。
>ngx.redirect
语法:ngx.redirect(uri, status?)
该方法会给客户端返回一个301/302重定向,具体是301还是302取决于设定的status值,如果不指定status值,默认是返回302 (
rewrite ^ /foo? permanent;#
nginx config
如果返回301,那么等价于下面的rewrite指令:
rewrite ^
/foo? redirect;# nginx config
要注意与ngx.location.capture*的区别
ngx.location.capture*主要是通过子请求的方式实现location的重新定位的,它与上面的两种方法完全不同的。
Subrequests are completely different from HTTP 301/302 redirection (viangx.redirect)
and internal redirection (viangx.exec).
> ngx.req.set_uri
语法: ngx.req.set_uri(uri, jump?)
通过参数uri重写当前请求的uri;参数jump,表明是否进行locations的重新匹配。当jump为true时,调用ngx.req.set_uri后,nginx将会根据修改后的uri,重新匹配新的locations;如果jump为false,将不会进行locations的重新匹配,而仅仅是修改了当前请求的URI而已。jump的默认值为false。
jump为true,等价于rewrite...last
jump为false,等价于rewrite...break
例如:
ngx.req.set_uri("/foo", true)
等价于
rewrite ^ /foo last;
rewrite ^ /foo break;
等价于
ngx.req.set_uri("/foo", false) 或 ngx.req.set_uri("/foo")
(全文完)
通过Lua模块也可以实现同样的功能,Lua模块提供了相关的API来实现重定向的功能,主要有:
>ngx.exec
语法:ngx.exec(uri, args?)
主要实现的是内部的重定向,等价于下面的rewrite指令
rewrite regrex replacement last;
例子:
ngx.exec('/some-location'); ngx.exec('/some-location', 'a=3&b=5&c=6'); ngx.exec('/some-location?a=3&b=5', 'c=6');
注意:
1. 如果给定的uri是命名的location,那么args就会被自动忽略的,如下所示:
location /foo { content_by_lua ' ngx.exec("@bar"); '; } location @bar { ... }
2. args参数可以以string的形式给出,也可以以lua table的形式给出,如下所示:
ngx.exec("/foo","a=3&b=hello%20world")
ngx.exec("/foo",{ a= 3,
b="hello world"})
3. 该方法不会主动返回,因此,强烈建议在调用该方法时,最好显示加上return,如下所示:
return ngx.exec(...)
4. 该方法不像ngx.redirect方法,不会产生额外的网络流量。
>ngx.redirect
语法:ngx.redirect(uri, status?)
该方法会给客户端返回一个301/302重定向,具体是301还是302取决于设定的status值,如果不指定status值,默认是返回302 (
ngx.HTTP_MOVED_TEMPORARILY),其等价于下面的rewrite指令:
rewrite ^ /foo? permanent;#
nginx config
如果返回301,那么等价于下面的rewrite指令:
rewrite ^
/foo? redirect;# nginx config
要注意与ngx.location.capture*的区别
ngx.location.capture*主要是通过子请求的方式实现location的重新定位的,它与上面的两种方法完全不同的。
Subrequests are completely different from HTTP 301/302 redirection (viangx.redirect)
and internal redirection (viangx.exec).
> ngx.req.set_uri
语法: ngx.req.set_uri(uri, jump?)
通过参数uri重写当前请求的uri;参数jump,表明是否进行locations的重新匹配。当jump为true时,调用ngx.req.set_uri后,nginx将会根据修改后的uri,重新匹配新的locations;如果jump为false,将不会进行locations的重新匹配,而仅仅是修改了当前请求的URI而已。jump的默认值为false。
jump为true,等价于rewrite...last
jump为false,等价于rewrite...break
例如:
ngx.req.set_uri("/foo", true)
等价于
rewrite ^ /foo last;
rewrite ^ /foo break;
等价于
ngx.req.set_uri("/foo", false) 或 ngx.req.set_uri("/foo")
(全文完)
相关文章推荐
- Nginx ngx_Lua模块中的重定向
- ngx_Lua模块中的重定向
- ngx_lua模块中的共享内存字典项API
- CentOS系统下,如何安装 nginx_lua_module 模块 以及 echo-nginx-module 模块
- Lua笔记8-模块和包
- ngx_lua模块,发现不少英文单词错误
- 解析Lua中的全局环境、包、模块组织结构
- ngx_Lua模块中的加密api接口
- 搭建Lua5.2.2并编写C模块
- Lua 调用自定义C模块
- lua模块之cjson模块-json解析
- 在Lua中使用模块的基础教程
- lua模块
- 源码安装nginx配置Lua模块
- 【精选】Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)
- freeswitch会议模块lua脚本
- 以自定义方式加载 lua 模块[云风blog]
- nginx_lua_module模块安装
- Nginx-ngx_lua模块原理和内置函数
- Lua 学习笔记(4) -- 模块与包