openresty ngx_lua重定向


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

         

                  

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`ngx_upstream_module` 是 OpenResty 中的一个模块,它提供了对 upstream 的支持。Upstream 是一个概念,用于表示一组后端服务器。在使用 upstream 时,客户端的请求会被反向代理到一个或多个后端服务器上进行处理。 `ngx_upstream_module` 提供了一些方法和指令,可以动态地管理 upstream 中的服务器,包括添加、删除、修改服务器等操作。常用的指令包括: - upstream:定义一个 upstream。 - server:定义一个服务器,包括地址和端口等信息。 - keepalive:设置连接池的大小。 - least_conn:使用最少连接数的负载均衡算法。 - ip_hash:使用 IP 地址进行哈希的负载均衡算法。 常用的方法包括: - ngx.upstream.get_servers(name):获取一个 upstream 中所有的服务器。 - ngx.upstream.add_server(name, server, weight):向一个 upstream 中添加服务器。 - ngx.upstream.remove_server(name, server):从一个 upstream 中删除服务器。 - ngx.upstream.set_peer_down(name, server):将一个服务器标记为不可用。 - ngx.upstream.set_peer_up(name, server):将一个服务器标记为可用。 在使用 `ngx_upstream_module` 时,需要在编译 OpenResty 时包含 `--with-http_upstream_module` 选项。可以通过以下命令检查当前 OpenResty 是否已经安装了 `ngx_upstream_module` 模块: ```shell $ /path/to/openresty/nginx/sbin/nginx -V 2>&1 | grep -o with-http_upstream_module ``` 需要将 `/path/to/openresty` 替换为实际的 OpenResty 安装路径。如果输出 `with-http_upstream_module`,则说明已经安装了 `ngx_upstream_module` 模块。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值