Skip to content

Instantly share code, notes, and snippets.

@dseg
Last active February 9, 2016 00:26
Show Gist options
  • Save dseg/27e3284706bd7c78321f to your computer and use it in GitHub Desktop.
Save dseg/27e3284706bd7c78321f to your computer and use it in GitHub Desktop.
NGINX 1.9が汎用TCPサーバとして使えるようになっていた件 ref: http://qiita.com/dseg/items/75bf517738a1d8b2d036
-- chargen.lua
-- RFC 864 (https://tools.ietf.org/html/rfc864)
-- Init character table
local chars = {}
for j = 0x21, 0x7E do table.insert(chars, string.char(j)) end
-- Main loop
local size, i, p = #chars, 0, ngx.print
while true do
local ok, err = p(chars[i % size + 1])
-- If client force abort, error will occure
if not ok then
ngx.log(ngx.ERR, "chargen.lua: Socket Write Error. ", err)
ngx.exit(200)
break
end
i = i + 1
-- wrap line
if (i % 72) == 0 then p("\r\n") end
end
local p = ngx.say
local sock = assert(ngx.req.socket(true))
if not sock then
ngx.log(ngx.ERR, "Failed to get the socket. Exiting...")
return
end
-- Main loop
while (true) do
local data, err = sock:receive() -- Read a data from downstream
if not err then
p(data) -- output data
else
ngx.log(ngx.ERR, err)
ngx.exit(200)
break
end
end
cd nginx-1.9.10
./configure \
--with-stream \
--with-stream_ssl_module \
--add-module="../stream-lua-nginx-module-master" \
--add-module="../stream-echo-nginx-module-master" \
...後略...
$ telnet 127.0.0.1 7
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello ←ENTERキー
hello
^]
telnet> q
Connection closed.
$ telnet 127.0.0.1 7
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello ←ENTERキー
hello
^]
telnet> q
Connection closed.
$ telnet 127.0.0.1 7
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello ←ENTERキー
hello
^]
telnet> q
Connection closed.
# Nginx + Lua (先述のecho.lua)
$ echoserver/client -c50 -o2 -h10000 -p7 127.0.0.1
Throughput: 64447.98 [#/sec]
# C++ epoll echoサーバ起動
$ echoserver/server_epoll
$ echoserver/client -c50 -o2 -h10000 -p5000 127.0.0.1
Throughput: 78058.97 [#/sec]
echo ngx.say("hello") > test.lua
$ resty -e 'ngx.say ("hello")'
hello
$ resty test.lua
hello
echo ngx.say("hello") > test.lua
$ resty -e 'ngx.say ("hello")'
hello
$ resty test.lua
hello
cd nginx-1.9.10
./configure \
--with-stream \
--with-stream_ssl_module \
--add-module="../stream-lua-nginx-module-master" \
--add-module="../stream-echo-nginx-module-master" \
...後略...
$ nc 127.0.0.1 13
2016-02-08 19:52:49
$ nc 127.0.0.1 13
2016-02-08 19:52:49
$ nc 127.0.0.1 9
hogefuga ← ENTERキー
(何もecho backされない)
$ nc 127.0.0.1 9
hogefuga ← ENTERキー
(何もecho backされない)
% nc 127.0.0.1 19
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefgh
ijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQR
STUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<
=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&
'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmn
opqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
YZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@AB
CDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,
← CTRL+C
% nc 127.0.0.1 19
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefgh
ijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQR
STUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<
=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&
'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmn
opqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
YZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@AB
CDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,
← CTRL+C
% nc 127.0.0.1 19
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefgh
ijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQR
STUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<
=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&
'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmn
opqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
YZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@AB
CDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,
← CTRL+C
location /something {
lua_code_cache off;
content_by_lua_file /path/to/lua/file.lua;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment