Nginx+Lua配置提取複寫,nginxlua配置請求

來源:互聯網
上載者:User

Nginx+Lua配置提取複寫,nginxlua配置請求

通過配置nginx來將請求進行複製,轉寄到其他應用,以下是自己實際搭建的步驟以及自己的理解,方便以後使用

1、環境搭建

參考連結:

http://www.crackedzone.com/testing-service-with-nginx-copy-request.html

https://blog.csdn.net/qq_25551295/article/details/51744815

實際搭建環境如下: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編譯失敗,githup下載後的外掛程式盡量重新命名一下,方便使用。

按照參考連結進行編譯Nginx。

2、Nginx+Lua檔案配置

a、編寫一個 copy request 的 lua 指令碼copy_req.lua

local res1, res2, actionaction = ngx.var.request_methodif action == "POST" then        arry = {method = ngx.HTTP_POST, body = ngx.req.read_body()}else        arry = {method = ngx.HTTP_GET}endif 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},        }endif 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_FOUNDend

 

此處檔案地址引用是可以寫覺得地址,相對位址是相對於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。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.