2017-08-23 108 views
0

我也有類似的需要https://github.com/openresty/lua-nginx-module/issues/220如何在代理通過後發出http請求?使用openresty(LUA + Nginx的)

我的使用情況

  1. 我轉發的文件到遠程服務器,通過使用proxy_pass
  2. 我需要通過$body_bytes_sent遠程URL,代理後傳遞。
  3. 我想過要做一個content_by_lua塊,ngx.capture轉發給proxy_pass塊,ngx.say()返回來自ngx.capture。後面跟着一個$ body_bytes_sent到遠程url的請求。但我需要支持流媒體,這是不行的。文件可能會變得很大,這對ngx.capture()不利。
  4. 我想過要做一個log_by_lua區塊,但是套接字apis被禁用。 https://github.com/openresty/lua-nginx-module#log_by_lua
+0

不認爲這是一個有點矯枉過正,我們做同樣的事情,即跟蹤請求的信息,但是,您可以使用節拍,蒐羅這些信息,並定義NGINX的日誌文件。也許發佈到logstash實例 – Nate

+0

包含'proxy_pass'的塊有一個查詢微服務的'access_by_lua'塊。在我們的設置中,微服務從'log_by_lua'提供數據,從而確定是否可以訪問特定的url。 ----'proxy_pass'內的遠程服務器不受我們的控制。所以我們不能在那裏完成。 ---給了Logstash一些想法。但我認爲這會讓事情變得複雜,因爲我需要將日誌的副本發送給微服務。 – galeaspablo

回答

0

安裝的Lua捲曲或不依賴於cosockets另一個庫。 (https://github.com/Lua-cURL/Lua-cURLv3

如果您使用luarocks(自帶openresty),安裝帶有

apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev --yes

luarocks install Lua-cURL

使用log_by_lua(例如log_by_lua_block,log_by_lua_file),像這樣。然後

# some nginx conf 

location /a_location_with_proxy_pass { 
    proxy_pass https://example.com:443; 
    log_by_lua_file /path/to/luafile.lua; 
} 

你log_by_lua_file應該做的捲曲請求到遠程服務器。

local cURL = require 'curl' 

curlHandle = cURL.easy{ 
    url  = 'https://remote_host.com/endpoint', 
    post  = true, 
    httpheader = { 
     'Content-Type: application/json'; 
    }, 
    postfields = '{"bytes":' .. ngx.var.body_bytes_sent .. '}' 
}; 
curlHandle:perform(); 
+0

這裏的糟糕之處在於,你將會阻止nginx worker直到收到響應。 –

+0

恐怕是這樣。我更喜歡'proxy_pass'到'ngx.capture' - 因爲我正在處理文件。但是這意味着,我只在'log_by_lua'中獲得'$ body_bytes_sent',它不允許使用套接字。 我寧願不阻止nginx工作者,但找不到其他東西。如果你還有什麼,即使只是想法,也會喜歡聽到它! :d – galeaspablo

相關問題