├── README.md ├── ngx-1.7.10-default_head.patch └── ngx-1.9.3-default_head.patch /README.md: -------------------------------------------------------------------------------- 1 | nginx-tcp-lua-server 2 | ================ 3 | 4 | How to build 5 | ================ 6 | ``` 7 | 1. download the special version from openresty.com 8 | # wget http://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz 9 | 2. download the special version patch and do patch 10 | # cp ngx-1.7.10-default_head.patch ngx_openresty-1.7.10.1/bundle/ 11 | # cd ngx_openresty-1.7.10.1/bundle 12 | # patch -p0 < ngx-1.7.10-default_head.patch 13 | 3. make and install 14 | # cd ngx_openresty-1.7.10.1 15 | # ./configure 16 | # make 17 | # make install 18 | ``` 19 | 20 | How to use it 21 | ================ 22 | 23 | ``` 24 | server { 25 | listen 80; 26 | server_name localhost; 27 | default_head "GET /demo.html HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"; 28 | 29 | location /demo.html { 30 | content_by_lua ' 31 | local tcp_req, err = ngx.req.socket(true) 32 | if not tcp_req then 33 | ngx.log(ngx.ERR, "get req socket err: ", err) 34 | end 35 | 36 | while true do 37 | local req_data 38 | req_data, err = tcp_req:receive(1) 39 | if not req_data then 40 | ngx.log(ngx.ERR, "get req_data err: ", err) 41 | return 42 | end 43 | 44 | ngx.log(ngx.ERR, "req data:", req_data) 45 | tcp_req:send(req_data) 46 | end 47 | '; 48 | } 49 | } 50 | ``` 51 | 52 | Pay attention 53 | ================ 54 | Its not a pure nginx tcp server. But is more power if you use ngx-lua with this way. It have great compatibility, and you can use 100% api of nginx-lua-module with lastest version. 55 | 56 | provide by membphis@gmail.com 57 | 58 | if you have any question, please let me know. 59 | -------------------------------------------------------------------------------- /ngx-1.7.10-default_head.patch: -------------------------------------------------------------------------------- 1 | diff -Naru nginx-1.7.10/src/http/ngx_http_core_module.c nginx-1.7.10_patch/src/http/ngx_http_core_module.c 2 | --- nginx-1.7.10/src/http/ngx_http_core_module.c 2015-03-01 09:58:25.000000000 +0800 3 | +++ nginx-1.7.10_patch/src/http/ngx_http_core_module.c 2015-06-18 23:12:10.000000000 +0800 4 | @@ -297,6 +297,13 @@ 5 | offsetof(ngx_http_core_srv_conf_t, underscores_in_headers), 6 | NULL }, 7 | 8 | + { ngx_string("default_head"), 9 | + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, 10 | + ngx_conf_set_str_slot, 11 | + NGX_HTTP_SRV_CONF_OFFSET, 12 | + offsetof(ngx_http_core_srv_conf_t, default_head), 13 | + NULL }, 14 | + 15 | { ngx_string("location"), 16 | NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12, 17 | ngx_http_core_location, 18 | diff -Naru nginx-1.7.10/src/http/ngx_http_core_module.h nginx-1.7.10_patch/src/http/ngx_http_core_module.h 19 | --- nginx-1.7.10/src/http/ngx_http_core_module.h 2015-02-10 22:33:34.000000000 +0800 20 | +++ nginx-1.7.10_patch/src/http/ngx_http_core_module.h 2015-06-18 23:12:47.000000000 +0800 21 | @@ -209,6 +209,8 @@ 22 | #endif 23 | 24 | ngx_http_core_loc_conf_t **named_locations; 25 | + 26 | + ngx_str_t default_head; 27 | } ngx_http_core_srv_conf_t; 28 | 29 | 30 | diff -Naru nginx-1.7.10/src/http/ngx_http_request.c nginx-1.7.10_patch/src/http/ngx_http_request.c 31 | --- nginx-1.7.10/src/http/ngx_http_request.c 2015-03-01 09:58:25.000000000 +0800 32 | +++ nginx-1.7.10_patch/src/http/ngx_http_request.c 2015-06-18 23:14:40.000000000 +0800 33 | @@ -411,6 +411,12 @@ 34 | return; 35 | } 36 | 37 | + if (cscf->default_head.len > 0) { 38 | + ngx_cpymem(b->last, cscf->default_head.data, cscf->default_head.len); 39 | + b->last += cscf->default_head.len; 40 | + size -= cscf->default_head.len; 41 | + } 42 | + 43 | c->buffer = b; 44 | 45 | } else if (b->start == NULL) { 46 | -------------------------------------------------------------------------------- /ngx-1.9.3-default_head.patch: -------------------------------------------------------------------------------- 1 | diff -Naru nginx-1.9.3/src/http/ngx_http_core_module.c nginx-1.9.3_patch/src/http/ngx_http_core_module.c 2 | --- nginx-1.9.3/src/http/ngx_http_core_module.c 2015-07-29 20:30:49.000000000 +0800 3 | +++ nginx-1.9.3_patch/src/http/ngx_http_core_module.c 2015-08-02 08:46:53.000000000 +0800 4 | @@ -267,6 +267,13 @@ 5 | offsetof(ngx_http_core_srv_conf_t, underscores_in_headers), 6 | NULL }, 7 | 8 | + { ngx_string("default_head"), 9 | + NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1, 10 | + ngx_conf_set_str_slot, 11 | + NGX_HTTP_SRV_CONF_OFFSET, 12 | + offsetof(ngx_http_core_srv_conf_t, default_head), 13 | + NULL }, 14 | + 15 | { ngx_string("location"), 16 | NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12, 17 | ngx_http_core_location, 18 | diff -Naru nginx-1.9.3/src/http/ngx_http_core_module.h nginx-1.9.3_patch/src/http/ngx_http_core_module.h 19 | --- nginx-1.9.3/src/http/ngx_http_core_module.h 2015-07-15 00:46:06.000000000 +0800 20 | +++ nginx-1.9.3_patch/src/http/ngx_http_core_module.h 2015-08-02 08:47:31.000000000 +0800 21 | @@ -216,6 +216,8 @@ 22 | #endif 23 | 24 | ngx_http_core_loc_conf_t **named_locations; 25 | + 26 | + ngx_str_t default_head; 27 | } ngx_http_core_srv_conf_t; 28 | 29 | 30 | diff -Naru nginx-1.9.3/src/http/ngx_http_request.c nginx-1.9.3_patch/src/http/ngx_http_request.c 31 | --- nginx-1.9.3/src/http/ngx_http_request.c 2015-07-29 20:30:49.000000000 +0800 32 | +++ nginx-1.9.3_patch/src/http/ngx_http_request.c 2015-08-02 08:48:43.000000000 +0800 33 | @@ -413,6 +413,12 @@ 34 | 35 | c->buffer = b; 36 | 37 | + if (cscf->default_head.len > 0) { 38 | + ngx_cpymem(b->last, cscf->default_head.data, cscf->default_head.len); 39 | + b->last += cscf->default_head.len; 40 | + size -= cscf->default_head.len; 41 | + } 42 | + 43 | } else if (b->start == NULL) { 44 | 45 | b->start = ngx_palloc(c->pool, size); 46 | --------------------------------------------------------------------------------