阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

配置Lua转发Nginx请求复制

162次阅读
没有评论

共计 2585 个字符,预计需要花费 7 分钟才能阅读完成。

通过配置 Nginx 来将请求进行复制,转发到其他应用,以下是自己实际搭建的步骤以及自己的理解,方便以后使用

1、环境搭建

实际搭建环境如下:Linux CenterOS 6.5,Nginx1.9.0,headers-more-nginx-module-0.31,LuaJIT-2.1.0-beta2,lua-nginx-module-0.10.2,ngx_devel_kit-0.2.19。

以上是搭建成功的各个对应版本,如果版本不对应可能会导致 nginx 编译失败,github 下载后的插件尽量重命名一下,方便使用。

按照参考链接进行编译 Nginx。

2、Nginx+Lua 文件配置

a、编写一个 copy request 的 lua 脚本 copy_req.lua

local res1, res2, action
action = ngx.var.request_method
if action == “POST” then
        arry = {method = ngx.HTTP_POST, body = ngx.req.read_body()}
else
        arry = {method = ngx.HTTP_GET}
end

if ngx.var.svr == “on” then
        res1, res2 = ngx.location.capture_multi {
                {“/product” .. ngx.var.request_uri , arry},
                {“/test” .. ngx.var.request_uri , arry},
        }
else
        res1, res2 = ngx.location.capture_multi {
                {“/product” .. ngx.var.request_uri , arry},
        }
end

if res1.status == ngx.HTTP_OK then
        local header_list = {“Content-Length”, “Content-Type”, “Content-Encoding”, “Accept-Ranges”}
        for _, i in ipairs(header_list) do
                if res1.header[i] then
                        ngx.header[i] = res1.header[i]
                end
        end
        ngx.say(res1.body) #此处代表只返回生产环境的返回结果
else
        ngx.status = ngx.HTTP_NOT_FOUND
end

此处文件地址引用是可以写觉得地址,相对地址是相对于 nginx 目录的。
b、配置对应的 Nginx 配置文件,此处本文地址是 conf/vhost/fenliu.conf, 在 nginx.conf 下端加入 include vhost/*.conf;

fenliu.conf 文件配置如下:

upstream product {
        server  127.0.0.1:80;
}
upstream test {
        server  192.168.1.1:88;
}
server {
        listen      8000;
        #lua_code_cache off;

        location ~* ^/product {
                log_subrequest on;
                rewrite ^/product(.*)$ $1 break;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://product;
                access_log logs/product-upstream.log;
        }

        location ~* ^/test {
                log_subrequest on;
                rewrite ^/test(.*)$ $1 break;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://test;
                access_log logs/test-upstream.log;
        }

        location ~* ^/(.*)$ {
                client_body_buffer_size 2m;
                set $svr    “on”;              #开启或关闭 copy 功能
                content_by_lua_file conf/vhost/copy_req.lua;
        }
}

此文件很重要,这里备注的是本人自己的理解,^/product,^/test 主要就是对这两个路径访问的 url 进行转发,一个转发到生产,一个到测试,多了一个 rewrite 是为了重写请求地址,下面会讲到,
^/(.*)$ 才是重点,是将所有非 product,test 请求进行请求复制转发。

以上面配置为例,实际使用的流程如下:
1、请求地址:http://ip:8000/hello/req.do
2、nginx 不匹配 product 和 test 会走最后一个,通过 Lua 配置会变成两个请求 /product/hello/req.do 和 /test/hello/req.do
3、这时会被 nginx 的 product 和 test 拦截到,进行转发到生产和测试环境,此时地址是不对的,所以使用 rewrite 进行 url 重写,
rewrite ^/product(.*)$ $1 break; 匹配 /product/hello/req.do 会变成 /product(/hello/req.do),$1 代表 /hello/req.do,重写后的地址就会变成我们想要的地址,转发后就变成 http://product/hello/req.do。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2585字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中