I'm hoping to make Nginx receive udp packets and then do some operations with the data by lua.
I start with this config (test-1):
https://github.com/openresty/stream-lua-nginx-module/blob/master/t/087-udp-socket.tThe connect and send phase worked, but with receive, it kept getting:
recv() failed (61: Connection refused)
I can't find much information about udp usage in nginx (http server).
So, can someone have a look what I'm doing wrong?
http {
resolver 127.0.0.1 ipv6=off;
include mime.types;
server {
default_type text/html;
listen 8081;
server_tokens off;
location / {
resolver 8.8.8.8;
content_by_lua '
local udp = ngx.socket.udp()
udp:settimeout(1000)
local ok, err = udp:setpeername("127.0.0.1", 8081)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ngx.say("connected")
local req = "\\0\\1\\0\\0\\0\\1\\0\\0flush_all\\r\\n"
local ok, err = udp:send(req)
if not ok then
ngx.say("failed to send: ", err)
return
end
ngx.say("sent")
local data, err = udp:receive()
if not data then
ngx.say("failed to receive data: ", err)
return
end
ngx.print("received ", #data, " bytes: ", data)
';
}
}
}