在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:nginx-stream-lua-module开源软件地址:https://gitee.com/laozi2/nginx-tcp-lua-module开源软件介绍:Namengx_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. Table of Contents
StatusProduction ready.This markdwon is in progress... VersionThis document describes nginx tcp module v0.2. Installation
# 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
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 Config examplenginx tcp core module, for tcp stream server 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 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 DescriptionBased on nginx-1.4.1, refer to nginx-http-lua,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.
Directivesfor ngx-tcp-core-module
for ngx-tcp-log-modulefor ngx-tcp-ssl-module
for ngx-tcp-lua-module
for ngx-tcp-demo-modulelistensyntax: default: listen *:0; context: server example: 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{}. protocolsyntax: default: -; context: server example: 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. read_timeoutsyntax: default: 60s; context: tcp,server example: read_timeout 60s; Sets the timeout for read the whole protocol data. send_timeoutsyntax: default: 60s; context: tcp,server example: 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. keepalive_timeoutsyntax: default: 6000s; context: tcp,server example: 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) connection_pool_sizesyntax: default: 0.5k; context: tcp,server example: 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. session_pool_sizesyntax: default: 1k; context: tcp,server example: 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. client_max_body_sizesyntax: default: 1k; context: tcp,server example: client_max_body_size 1k; Sets the maximum allowed size of the client request body, specified by protocol. error_logsyntax: default: error_log logs/error.log error; context: main,tcp,server example: 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'. nlogsyntax: default: -; context: main,tcp,server example: 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. allowsyntax: default: -; context: tcp,server example: 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. denysyntax: default: -; context: tcp,server example: 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. resolversyntax: default: -; context: tcp,server example: 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. resolver_timeoutsyntax: default: 30s; context: tcp,server example: resolver_timeout 10s; Sets a timeout for name resolution. log_formatsyntax: default: combined '$remote_addr $time_iso8601 $msec $request_time $connection $connection_requests $protocol'; context: tcp,server example: 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
$time_local
$time_iso8601
$msec
$request_time
$connection
$connection_requests
$bytes_sent
$protocol
access_logsyntax: default: logs/access_tcp.log combined; context: tcp,server example: 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. access_nlogsyntax: default: -; context: tcp,server example: 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. sslrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_certificaterefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_certificate_keyrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_dhparamrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_ecdh_curverefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_protocolsrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_ciphersrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_verify_clientrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_verify_depthrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_client_certificaterefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_prefer_server_ciphersrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_session_cacherefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_session_timeoutrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html ssl_crlrefer to http://nginx.org/en/docs/http/ngx_http_ssl_module.html lua_package_cpathsyntax: default: The content of LUA_CPATH environment variable or Lua's compiled-in defaults.; context: tcp example: 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. lua_package_pathsyntax: default: The content of LUA_PATH environ variable or Lua's compiled-in defaults. context: tcp example: 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. lua_code_cachesyntax: default: The content of LUA_PATH environ variable or Lua's compiled-in defaults. context: tcp,server example: 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. init_by_luasyntax: default: - context: tcp example: 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. init_by_lua_filesyntax: default: - context: tcp example: 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. process_by_luasyntax: default: - context: server example: 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). process_by_lua_filesyntax: default: - context: server example: 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. lua_socket_connect_timeoutsyntax: default: 60s; context: tcp,server example: 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 lua_socket_send_lowatsyntax: default: 0; context: tcp,server example: lua_socket_send_lowat 0; Controls the lowat (low water) value for the cosocket send buffer. lua_socket_pool_sizesyntax: default: 30; context: tcp,server example: lua_socket_pool_size 10; Specifies the size limit (in terms of connection count) for every cosocket connection pool associated with every remote server (i.e., identified by either the host-port pair). When the connection pool exceeds the available size limit, the least recently used (idle) connection already in the pool will be closed to make room for the current connection. Note that the cosocket connection pool is per nginx worker process rather than per nginx server instance, so size limit specified here also applies to every single nginx worker process. lua_check_client_abortsyntax: default: off; context: tcp,server example: lua_check_client_abort on; This directive controls whether to check for premature client connection abortion. lua_shared_dictsyntax: default: -; context: tcp example: lua_shared_dict dogs 10m; Declares a shared memory zone, , to serve as storage for the shm based Lua dictionary ngx.shared.. Shared memory zones are always shared by all the nginx worker processes in the current nginx server instance. The argument accepts size units such as k and m. At least 8k; lua_shared_dictsyntax: default: 1024; context: tcp example: lua_shared_dict dogs 10m; Specifies the maximum number of entries allowed in the worker process level compiled regex cache. The regular expressions used in ngx.re.match, ngx.re.gmatch, ngx.re.sub, and ngx.re.gsub will be cached within this cache if the regex option o (i.e., compile-once flag) is specified. lua_regex_match_limitsyntax: default: 0; context: tcp example: lua_shared_dict dogs 10m; Specifies the "match limit" used by the PCRE library when executing the ngx.re API. To quote the PCRE manpage, "the limit ... has the effect of limiting the amount of backtracking that can take place." When the limit is hit, the error string "pcre_exec() failed: -8" will be returned by the ngx.re API functions on the Lua land. When setting the limit to 0, the default "match limit" when compiling the PCRE library is used. And this is the default value of this directive. demo_echosyntax: default: -; context: server example: demo_echo "hello world"; This directive is used for ngx_tcp_demo_module, which define a echo protocol. The request stream is 4 bytes head contains request data length.When demo_echo string set, the server will response 4 bytes head contains response data length, and demo_echo string. when this directive not set, the response body data is request body data. Code Exapmle for ngx_tcp_lua_module local client_sock = ngx.socket.tcp() client_sock:settimeout(5000,1000,3000) local data = "hello world 1234567890" local data_len = string.len(data) local data_len_h = netutil.htonl(data_len) local req_data = netutil.packint32(data_len_h) .. data local upstream_test = function(u) local ret,err = u:connect("127.0.0.1",8000,"127.0.0.1_8000_1"); local reuse = u:getreusedtimes() ngx.log(ngx.INFO,"connect : "..tostring(ret).." "..tostring(err).." "..tostring(reuse)) if not ret then return end ret,err = u:send(req_data) ngx.log(ngx.INFO,"send : "..tostring(ret).." "..tostring(err)) if not ret then return end local data,err = u:receive(4,nil) if use_log then ngx.log(ngx.INFO,"receive : "..tostring(data).." "..tostring(err)) end if not data then return end local totoal_len = netutil.unpackint32(string.sub(data,1,4)) ngx.log(ngx.INFO,"totoal_len : "..tostring(totoal_len)) local data,err = u:receive(totoal_len - 4,nil) ngx.log(ngx.INFO,"receive again: ["..tostring(data).."] "..tostring(err)) if not data then return end if totoal_len - 4 ~= #data then ngx.log(ngx.INFO,"receive len not match") return end u:setkeepalive() end local test_shm = function() local key = "x" local value = "hello world" local dogs = ngx.shared.db_lock local shm_value = dogs:get(key) if not shm_value then local succ, err, forcible = dogs:set(key,value,10000) ngx.log(ngx.INFO,tostring(succ)..","..tostring(err)..","..tostring(forcible)) end local shm_value = dogs:get(key) ngx |
请发表评论