0

I have a unix domain socket file and it is working with with nc command. Now I want to access it via nginx but it does not work. Am I missing something?

test with nc => it works

$ echo  '{ "method" : "getinfo", "params" : [], "id" : "1" }' | nc -U /home/zono/.lightning/lightning-rpc
{ "jsonrpc": "2.0", "id" : "1", "result" :
{
  "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

test via nginx => it does not work

// /etc/nginx/sites-enabled/default
upstream nginx-internal-sock {
  server unix:/home/zono/.lightning/lightning-rpc;
}

server {
  listen 80;
  location / {
    proxy_pass http://nginx-internal-sock;
  }
}

$ curl -H "content-type: application/json" -X POST --data '{ "method" : "getinfo", "params" : [], "id" : "1" }' http://127.0.0.1
2019-03-20T04:25:52.551Z lightningd(30143):jcon fd 32: Invalid token in json input: 'POST / HTTP/1.0??Host: nginx-internal-sock??Connection: close??C'

Update 1

There's been a development. However I can't get whole data.

// install nginx-extras
apt-get install nginx-extras

// /etc/nginx/sites-enabled/default
server {
  listen 80;

  location / {
        content_by_lua '
            ngx.req.read_body()
            local body_data = ngx.req.get_body_data()

            local sock = ngx.socket.tcp()
            local ok, err = sock:connect("unix:/home/zono/.lightning/lightning-rpc")

            local bytes = sock:send(body_data)

            local line, err = sock:receive("*a")
            ngx.say(line)

            ok, err = sock:close()
        ';
  }
}

// Response is nil
$ curl -X POST --data '{ "method" : "getinfo", "params" : [], "id" : "1" }' http://127.0.0.1
nil

// /var/log/nginx/error.log
2019/03/20 07:43:39 [error] 4926#4926: *35 lua tcp socket read timed out, client: 127.0.0.1, server: , request: "POST / HTTP/1.1", host: "127.0.0.1"

// When I set "sock:receive("*l")" the response is the part of the data.
$ curl -X POST --data '{ "method" : "getinfo", "params" : [], "id" : "1" }' http://127.0.0.1
{ "jsonrpc": "2.0", "id" : "1", "result" :

I'm checking the reference now. http://w3.impa.br/~diego/software/luasocket/tcp.html

'*a': reads from the socket until the connection is closed. No end-of-line translation is performed;

'*l': reads a line of text from the socket. The line is terminated by a LF character (ASCII 10), optionally preceded by a CR character (ASCII 13). The CR and LF characters are not included in the returned line. In fact, all CR characters are ignored by the pattern. This is the default pattern;

number: causes the method to read a specified number of bytes from the socket.

Community
  • 1
  • 1
zono
  • 8,366
  • 21
  • 75
  • 113

1 Answers1

0

I found out the answer.

// install nginx-extras
apt-get install nginx-extras

// /etc/nginx/sites-enabled/default
server {
  listen 80;

  location / {
        content_by_lua '
            ngx.req.read_body()
            local body_data = ngx.req.get_body_data()

            local sock = ngx.socket.tcp()
            local ok, err = sock:connect("unix:/home/zono/.lightning/lightning-rpc")

            local bytes = sock:send(body_data)

            local readline = sock:receiveuntil("\\n\\n")
            local line, err, part = readline()
            if line then
                ngx.say(line)
            end

            ok, err = sock:close()
        ';
  }
}

// curl
$ curl -X POST --data '{ "method" : "getinfo", "params" : [], "id" : "1" }' http://127.0.0.1
zono
  • 8,366
  • 21
  • 75
  • 113