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) do
if key == "a" then
ngx.say(val)
end
end
}
}
使用示例
default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
#location 1
location /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 2
location /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 3
location /last {
if ( !-e $request_filename ){
#rewrite ^/last/(.*) /test/$1 last;
rewrite_by_lua_block {
ngx.req.set_uri("/test/$1", true);
}
echo "last";
}
}
#location 4
location /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 info
huli@hudeMacBook-Pro redirect2 % curl localhost:4005/break2
break
huli@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/$1
huli@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/$1
huli@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/$1
huli@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