# nginx-stream-lua-module **Repository Path**: laozi2/nginx-tcp-lua-module ## Basic Information - **Project Name**: nginx-stream-lua-module - **Description**: nginx tcp stream lua - **Primary Language**: C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 7 - **Forks**: 1 - **Created**: 2015-11-20 - **Last Updated**: 2025-06-11 ## Categories & Tags **Categories**: nginx-modules **Tags**: None ## README Name ==== ngx_tcp_module - A tcp stream module for nginx. ngx_tcp_lua_module - Embed the power of Lua into Nginx Servers. Work under tcp stream mode. This module is not distributed with the Nginx source. See [the installation instructions](#installation). Table of Contents ================= * [Name](#name) * [Status](#status) * [Version](#version) * [Config example](#config-example) * [Description](#description) * [Directives](#directives) * [Code Exapmle for ngx_tcp_lua_module](#code-exapmle-for-ngx_tcp_lua_module) * [Nginx API for Lua](#nginx-api-for-lua) Status ======= Production ready. This markdwon is in progress... Version ======= This document describes nginx tcp module v0.2. Installation ========== 1. Download the nginx-1.4.1 [HERE](http://nginx.org/download/nginx-1.4.1.tar.gz) 2. unzip, cd nginx-1.4.1, copy auto.patch,src_core.patch,src_http.patch into current directory 3. patch -p1 < auto.patch patch -p1 < src_core.patch patch -p1 < src_http.patch #optional, for nginx access_nlog 4. copy tcp/ into current src/ 5. then, ./configure and make and make isntall. here is configure example ```bash # yum -y install -y pcre* openssl* # for pcre, such as ngx.gmatch etc, --with-pcre=PATH/pcre-8.36 --with-pcre-jit # if use openssl, then need --with-http_ssl_module # export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.0 ./configure --prefix=/usr/local/nginx_tcp \ --with-debug \ --with-pcre=/root/ngx_tcp_compile/softwares/pcre-8.36 \ --with-pcre-jit \ --without-http_gzip_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-tcp \ --with-tcp_ssl_module \ --with-openssl=/opt/openssl-1.0.1e \ --with-openssl-opt=-g \ --add-module=src/tcp/ngx_tcp_log_module \ --add-module=src/tcp/ngx_tcp_demo_module \ --add-module=src/tcp/ngx_tcp_lua_module ``` 6. Build the source with ngx_tcp_lua_module: ```bash wget http://luajit.org/download/LuaJIT-2.0.0.tar.gz tar -xvfz LuaJIT-2.0.0.tar.gz cd LuaJIT-2.0.0 make && make install # tell nginx's build system where to find luajit: export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.0 # or tell where to find Lua #export LUA_LIB=/path/to/lua/lib #export LUA_INC=/path/to/lua/include ``` [Back to TOC](#table-of-contents) Config example ========== #### nginx tcp core module, for tcp stream server ```nginx tcp { #connection_pool_size 1k; #main/srv/take one/default 0.5k session_pool_size 1k; #main/srv/take one/default 1k client_max_body_size 1k; #main/srv/take one/default 1k; read_timeout 60s; #main/srv/take one/default 60s #send_timeout 60s; #main/srv/take one/default 60s #keepalive_timeout 60; #main/srv/take one/no set,no keepalive_timeout #error_log logs/error_tcp.log debug_tcp; #main/srv/take one more/default null error_log logs/error_tcp.log info; log_format token '$remote_addr $time_iso8601 $msec $request_time $connection $connection_requests $bytes_sent $protocol'; #default log_format combined '$remote_addr $time_iso8601 $msec $request_time $connection $connection_requests $protocol'; server { listen 6666; protocol demo; #access_log off; access_log logs/access_tcp.log token; #default access_log logs/access_tcp.log; access_nlog 0.0.0.0:5002 0.0.0.0:5151; allow 127.0.0.1; deny all; } #server { # listen 6433; # # protocol demo; # # #access_log off; # access_log logs/access_tcp.log demo; #default access_log logs/access_tcp.log; # access_nlog 0.0.0.0:5002 0.0.0.0:5151; # # ssl on; # ssl_certificate xxx-chain.pem; # ssl_certificate_key xxx-key.pem; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; #} } ``` #### for lua module ```nginx tcp { lua_package_path '/usr/local/nginx_tcp/conf/?.lua;/usr/local/nginx_tcp/conf/lua_module/?.lua;;'; lua_package_cpath '/usr/local/nginx_tcp/conf/lua_module/?.so;;'; lua_shared_dict db_lock 100m; init_by_lua_file 'conf/init_by_lua.lua'; server { listen 6666; protocol tcp_lua; process_by_lua_file 'conf/test.lua'; } ``` and for the test.lua, see example [Code Exapmle for ngx_tcp_lua_module](#code-exapmle-for-ngx_tcp_lua_module) [Back to TOC](#table-of-contents) Description ========= Based on nginx-1.4.1, refer to [nginx-http-lua](https://github.com/openresty/lua-nginx-module), follow the principles of simple, efficient and highly extensible, the nginx-tcp module is designed as a customized stream protocol server, more than http, mail server. And the ngx_tcp_lua module is very useful in fast implement your own service. 1. Patch log.c, ngx_http_log_module.c, add the command `nlog`,`access_nlog` to send error_log,access_log to log server with udp. 2. tcp_module for customized stream protocol on tcp, support ssl. example, tcp/ngx_tcp_demo_module. 3. tcp_lua module: embeds Lua code, 100% non-blocking on network traffic. * just like ngx_tcp_demo_module, ngx_tcp_lua module is a special module on tcp_module. * enriched the functions such as init_by_lua,ngx.sleep,ngx.exit, just like nginx-lua-module * lua lib with cosocket to deal with mysql,http server. simple load banlance and retry also supported * support ngx.nlog to send log to udp log server, more than ngx.log to local file. * support ssl cosocket with upstream 4. About installation, APIs, and examples, see [tcp/doc/](https://github.com/laozi2/nginx-tcp-lua-module/tree/master/tcp/doc) for more details [Back to TOC](#table-of-contents) Directives ========== #### for ngx-tcp-core-module * [server](#server) * [listen](#listen) * [protocol](#protocol) * [read_timeout](#read_timeout) * [send_timeout](#send_timeout) * [keepalive_timeout](#keepalive_timeout) * [connection_pool_size](#connection_pool_size) * [session_pool_size](#session_pool_size) * [client_max_body_size](#client_max_body_size) * [error_log](#error_log) * [nlog](#nlog) * [allow](#allow) * [deny](#deny) * [resolver](#resolver) * [resolver_timeout](#resolver_timeout) ### for ngx-tcp-log-module * [log_format](#log_format) * [access_log](#access_log) * [access_nlog](#access_nlog) ### for ngx-tcp-ssl-module * [ssl](#ssl) * [ssl_certificate](#ssl_certificate) * [ssl_certificate_key](#ssl_certificate_key) * [ssl_dhparam](#ssl_dhparam) * [ssl_ecdh_curve](#ssl_ecdh_curve) * [ssl_protocols](#ssl_protocols) * [ssl_ciphers](#ssl_ciphers) * [ssl_verify_client](#ssl_verify_client) * [ssl_verify_depth](#ssl_verify_depth) * [ssl_client_certificate](#ssl_client_certificate) * [ssl_prefer_server_ciphers](#ssl_prefer_server_ciphers) * [ssl_session_cache](#ssl_session_cache) * [ssl_session_timeout](#ssl_session_timeout) * [ssl_crl](#ssl_crl) #### for ngx-tcp-lua-module * [lua_package_cpath](#lua_package_cpath) * [lua_package_path](#lua_package_path) * [lua_code_cache](#lua_code_cache) * [init_by_lua](#init_by_lua) * [init_by_lua_file](#init_by_lua_file) * [process_by_lua](#process_by_lua) * [process_by_lua_file](#process_by_lua_file) * [lua_socket_connect_timeout](#lua_socket_connect_timeout) * [lua_socket_send_lowat](#lua_socket_send_lowat) * [lua_socket_pool_size](#lua_socket_pool_size) * [lua_check_client_abort](#lua_check_client_abort) * [lua_shared_dict](#lua_shared_dict) * [lua_regex_cache_max_entries](#lua_regex_cache_max_entries) * [lua_regex_match_limit](#lua_regex_match_limit) #### for ngx-tcp-demo-module * [demo_echo](#demo_echo) [Back to TOC](#table-of-contents) listen -------------------- **syntax:** `listen` [*ip*:]*port* [backlog=*number*] [rcvbuf=*size*] [sndbuf=*size*] [deferred] [bind] [ssl] [so_keepalive=on|off|[*keepidle]:[keepintvl]:[keepcnt*]]; **default:** listen *:0; **context:** server **example:** ```nginx listen 127.0.0.1:110; listen *:110; listen 110; # same as *:110 ``` Sets the address and port for IP on which the server will accept requests. Only IPv4 supported now. One server{} can have diffrent ip addresses. But one specified port only can be set in one server{}. [Back to TOC](#directives) protocol -------------------- **syntax:** `protocol` *protocol_name*; **default:** -; **context:** server **example:** ```nginx protocol demo; ``` protocol_name must be defined in an implemented module, such as ngx_tcp_demo_module. One server{} can only have one specified protocol_name. [Back to TOC](#directives) read_timeout -------------------- **syntax:** `read_timeout` *time*; **default:** 60s; **context:** tcp,server **example:** ```nginx read_timeout 60s; ``` Sets the timeout for read the whole protocol data. [Back to TOC](#directives) send_timeout -------------------- **syntax:** `send_timeout` *time*; **default:** 60s; **context:** tcp,server **example:** ```nginx send_timeout 60s; ``` Sets a timeout for transmitting a response to the client. The timeout is set only between two successive write operations, not for the transmission of the whole response. If the client does not receive anything within this time, the connection is closed. [Back to TOC](#directives) keepalive_timeout -------------------- **syntax:** `keepalive_timeout` *time*; **default:** 6000s; **context:** tcp,server **example:** ```nginx send_timeout 6000s; ``` Sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections. (TODO: enable never close client connection) [Back to TOC](#directives) connection_pool_size -------------------- **syntax:** `connection_pool_size` *size*; **default:** 0.5k; **context:** tcp,server **example:** ```nginx connection_pool_size 1k; ``` Allows accurate tuning of per-connection memory allocations. This directive has minimal impact on performance and should not generally be used. [Back to TOC](#directives) session_pool_size -------------------- **syntax:** `session_pool_size` *size*; **default:** 1k; **context:** tcp,server **example:** ```nginx session_pool_size 1k; ``` Allows accurate tuning of per-session memory allocations. This directive has minimal impact on performance and should not generally be used. The optimal value is little larger than average receiving data length, so it can take full use of memory and minimum times of allocations. [Back to TOC](#directives) client_max_body_size -------------------- **syntax:** `client_max_body_size` *size*; **default:** 1k; **context:** tcp,server **example:** ```nginx client_max_body_size 1k; ``` Sets the maximum allowed size of the client request body, specified by protocol. [Back to TOC](#directives) error_log -------------------- **syntax:** `error_log` *file* | stderr | [debug | info | notice | warn | error | crit | alert | emerg]; **default:** error_log logs/error.log error; **context:** main,tcp,server **example:** ```nginx error_log logs/error.log error; #or in tcp{},server{} below: error_log logs/error_tcp.log debug_tcp; ``` Configures logging. 'debug_tcp' can be used like 'debug_http'. [Back to TOC](#directives) nlog -------------------- **syntax:** `nlog` *local_ip*:*local_port* *remote_ip*:*remote_prot*; **default:** -; **context:** main,tcp,server **example:** ```nginx error_log logs/error.log error; nlog 0.0.0.0:5001 0.0.0.0:5151; #nlog must set after error_log. ``` Configures ip address to support logging to UDP log server. [Back to TOC](#directives) allow -------------------- **syntax:** `allow` *ip* | all; **default:** -; **context:** tcp,server **example:** ```nginx allow 127.0.0.1; allow 127.0.0.0/24; allow all; ``` Allows access for the specified network or address. The rules are checked in sequence until the first match is found. [Back to TOC](#directives) deny -------------------- **syntax:** `deny` *ip* | all; **default:** -; **context:** tcp,server **example:** ```nginx deny 127.0.0.1; deny 127.0.0.0/24; deny all; ``` Denies access for the specified network or address. The rules are checked in sequence until the first match is found. [Back to TOC](#directives) resolver -------------------- **syntax:** `resolver` *address* [valid=*time*]; **default:** -; **context:** tcp,server **example:** ```nginx resolver 127.0.0.1 8.8.8.8 valid=30s;; ``` Configures name servers used to resolve names of upstream servers into addresses. By default, nginx caches answers using the TTL value of a response. An optional valid parameter allows overriding it. [Back to TOC](#directives) resolver_timeout -------------------- **syntax:** `resolver` *time*; **default:** 30s; **context:** tcp,server **example:** ```nginx resolver_timeout 10s; ``` Sets a timeout for name resolution. [Back to TOC](#directives) log_format -------------------- **syntax:** `log_format` *name* *string* ...; **default:** combined '$remote_addr $time_iso8601 $msec $request_time $connection $connection_requests $protocol'; **context:** tcp,server **example:** ```nginx log_format log1 '$remote_addr $time_iso8601 $msec $request_time $connection $connection_requests $bytes_sent $protocol'; ``` Specifies log format. The log format can contain common variables, and variables that exist only at the time of a log write: $remote_addr > client ip address. $time_local > local time in the Common Log Format, "28/Sep/1970:12:00:00 +0600". $time_iso8601 > local time in the ISO 8601 standard format, "1970-09-28T12:00:00+06:00". $msec > time in seconds with a milliseconds resolution at the time of the log write. $request_time > request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client. $connection > connection serial number. $connection_requests > the current number of requests made through a connection. $bytes_sent > the number of bytes sent to a client. $protocol > the current protocol name. [Back to TOC](#directives) access_log -------------------- **syntax:** `access_log` *path* | off [*format*]; **default:** logs/access_tcp.log combined; **context:** tcp,server **example:** ```nginx access_log logs/access_tcp.log log1; ``` Sets the path, format for a buffered log write. Several logs can be specified on the same level. [Back to TOC](#directives) access_nlog -------------------- **syntax:** `access_nlog` *local_ip*:*local_port* *remote_ip*:*remote_prot*; **default:** -; **context:** tcp,server **example:** ```nginx access_log logs/access_tcp.log log1; access_nlog 127.0.0.1:5002 127.0.0.1:5151;#access_nlog must set after access_log. ``` Configures ip address to support logging to UDP log server. [Back to TOC](#directives) ssl -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_certificate -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_certificate_key -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_dhparam -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_ecdh_curve -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_protocols -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_ciphers -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_verify_client -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_verify_depth -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_client_certificate -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_prefer_server_ciphers -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_session_cache -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_session_timeout -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) ssl_crl -------------------- refer to [http://nginx.org/en/docs/http/ngx_http_ssl_module.html](http://nginx.org/en/docs/http/ngx_http_ssl_module.html) [Back to TOC](#directives) lua_package_cpath -------------------- **syntax:** `lua_package_cpath` *lua-style-cpath-str*; **default:** *The content of LUA_CPATH environment variable or Lua's compiled-in defaults.*; **context:** tcp **example:** ```nginx lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;'; ``` Sets the Lua C-module search path used by scripts specified by init_by_lua[_file], protocol_by_lua[_file]. The cpath string is in standard Lua cpath form, and ;; can be used to stand for the original cpath. [Back to TOC](#directives) lua_package_path -------------------- **syntax:** `lua_package_path` *lua-style-path-str*; **default:** *The content of LUA_PATH environ variable or Lua's compiled-in defaults.* **context:** tcp **example:** ```nginx lua_package_path '/foo/bar/?.lua;/blah/?.lua;;' ``` Sets the Lua module search path used by scripts specified by init_by_lua[_file], protocol_by_lua[_file]. The path string is in standard Lua path form, and ;; can be used to stand for the original search paths. [Back to TOC](#directives) lua_code_cache -------------------- **syntax:** `lua_code_cache` on | off; **default:** *The content of LUA_PATH environ variable or Lua's compiled-in defaults.* **context:** tcp,server **example:** ```nginx lua_code_cache on; ``` Enables or disables the Lua code cache for Lua code in *_by_lua_file directives and Lua modules. When turning off, every request served by ngx_lua will run in a separate Lua VM instance. Disabling the Lua code cache is strongly discouraged for production use and should only be used during development as it has a significant negative impact on overall performance. [Back to TOC](#directives) init_by_lua -------------------- **syntax:** `init_by_lua` *lua-script-str*; **default:** - **context:** tcp **example:** ```nginx init_by_lua 'a = require("a")'; ``` Runs the Lua code specified by the argument on the global Lua VM level when the Nginx master process (if any) is loading the Nginx config file. [Back to TOC](#directives) init_by_lua_file -------------------- **syntax:** `init_by_lua_file` *path-to-lua-script-file*; **default:** - **context:** tcp **example:** ```nginx init_by_lua_file 'conf/init_by_lua.lua'; ``` Equivalent to init_by_lua, except that the file specified by contains the Lua code or Lua/LuaJIT bytecode to be executed. [Back to TOC](#directives) process_by_lua -------------------- **syntax:** `process_by_lua` *lua-script-str*; **default:** - **context:** server **example:** ```nginx process_by_lua 'ngx.exit()'; ``` Executes Lua code string specified in for requests of every connection. The Lua code may make API calls and is executed as a new spawned coroutine in an independent global environment (i.e. a sandbox). [Back to TOC](#directives) process_by_lua_file -------------------- **syntax:** `process_by_lua_file` *path-to-lua-script-file*; **default:** - **context:** server **example:** ```nginx process_by_lua_file 'conf/test.lua'; ``` Equivalent to process_by_lua, except that the file specified by contains the Lua code or Lua/LuaJIT bytecode to be executed. [Back to TOC](#directives) lua_socket_connect_timeout -------------------- **syntax:** `lua_socket_connect_timeout` *time*; **default:** 60s; **context:** tcp,server **example:** ```nginx lua_socket_connect_timeout 5; ``` This directive controls the default timeout value used in tcp socket object's connect method and can be overridden by the settimeout method. The