12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nginxAdvent Calendar 2014

Day 8

Nginx で HTTP API 化

Posted at

lua-nginx-module の ngx.socket.tcp() や ngx.socket.udp() を使用すると、
HTTP 以外の TCP や UDP を使用したプロトコルでデータを送受信するようなシステムにも Nginx から直接接続することが可能です

そのため、Nginx の後に別途、HTTP リクエストを受信して、これらのシステムと接続するようなサーバを用意する必要がなくなります

今回は簡単に、POST リクエストで送った path と value を使用して、Graphite (Carbon) に UDP および TCP でデータを送ってみました

設定

ngixn.conf

        location /graphite/udp {
            set $graphite_host '127.0.0.1';
            set $graphite_port 2003;

            content_by_lua '
                local sock = ngx.socket.udp()
                local graphite_host = ngx.var.graphite_host
                local graphite_port = ngx.var.graphite_port
                local ok, err = sock:setpeername(graphite_host, tonumber(graphite_port))
                if not ok then
                    ngx.say(err)
                    return
                end

                ngx.req.read_body()
                local args, err = ngx.req.get_post_args()
                if not args then
                    ngx.say(err)
                    return
                end

                local path = args.path
                local value = args.value

                local timestamp = os.time()
                local data = table.concat({path, value, tostring(timestamp)}, " ")
                local ok, err = sock:send(data)
                if err then
                    ngx.say(err)
                    return
                end

                local ok, err = sock:close()
                if not ok then
                    ngx.say(err)
                    return
                end
            ';
        }

        location /graphite/tcp {
            set $graphite_host '127.0.0.1';
            set $graphite_port 2003;

            content_by_lua '
                local sock = ngx.socket.tcp()
                local graphite_host = ngx.var.graphite_host
                local graphite_port = ngx.var.graphite_port
                local ok, err = sock:connect(graphite_host, tonumber(graphite_port))
                if not ok then
                    ngx.say(err)
                    return
                end

                ngx.req.read_body()
                local args, err = ngx.req.get_post_args()
                if not args then
                    ngx.say(err)
                    return
                end

                local path = args.path
                local value = args.value

                local timestamp = os.time()
                local data = table.concat({path, value, tostring(timestamp)}, " ")
                local ok, err = sock:send(data)
                if err then
                    ngx.say(err)
                    return
                end

                local ok, err = sock:close()
                if not ok then
                    ngx.say(err)
                    return
                end
            ';
        }

送信

curl でデータを送ってみます

tcp
$ curl -v -X POST --data "path=nginx.tcp.data&value=`expr $RANDOM % 100`" http://localhost:8080/graphite/tcp
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /graphite/tcp HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
> Content-Length: 28
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 28 out of 28 bytes
< HTTP/1.1 200 OK
* Server openresty/1.7.4.1 is not blacklisted
< Server: openresty/1.7.4.1
< Date: Sun, 07 Dec 2014 04:57:12 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
udp
$ curl -v -X POST --data "path=nginx.udp.data&value=`expr $RANDOM % 100`" http://localhost:8080/graphite/udp
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /graphite/udp HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
> Content-Length: 28
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 28 out of 28 bytes
< HTTP/1.1 200 OK
* Server openresty/1.7.4.1 is not blacklisted
< Server: openresty/1.7.4.1
< Date: Sun, 07 Dec 2014 04:57:20 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
* Connection #0 to host localhost left intact

Graphite

送られてきたデータをグラフで確認してみます

スクリーンショット 2014-12-07 13.58.34.png

参考 URL

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?