tengine独立编译运行方法-不依赖k8s+ingress环境

 

本文为内容提要,详细的分析拆解过程详见: k8s tengine代码分析之编译运行 

概述

依赖openresty

openresty-luajit
lua-resty-core
ngx_http_lua_module
stream-lua-nginx-module
ngx_http_upstream_dynamic_module ??
ngx_http_upstream_dyups_module ??
ngx_devel_kit ??

依赖tengine

tongsou
ngx_ingress_module
mod_common
mod_strategy
mod_proc
tengine-ingress/rootfs/etc/nginx/lua/*

1 安装依赖

sudo apt install libluajit2-5.1-dev
wget https://github.com/Tongsuo-Project/Tongsuo/archive/refs/heads/8.4-stable.zip
wget https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.27.zip
sudo apt install lua-resty-lrucache
wget https://github.com/openresty/stream-lua-nginx-module/archive/refs/tags/v0.0.16.zip
sudo apt install lua-cjson
wget https://github.com/openresty/lua-resty-dns/archive/refs/tags/v0.23.zip
wget https://github.com/openresty/lua-resty-balancer/archive/refs/tags/v0.05.zip
wget https://github.com/cloudflare/lua-resty-cookie/archive/refs/tags/v0.1.0.zip
wget https://github.com/openresty/lua-resty-lock/archive/refs/tags/v0.09.zip

 

2 lua包安装方法

unzip xxx
sudo make install LUA_LIB_DIR=/usr/local/share/lua/5.1

 

3 设置共享内存

dd if=/dev/zero of=/dev/shm/ShmServiceCfg bs=1M count=256
touch /etc/nginx/shm_service_cfg.lock

 

4 配置方法

sudo cp -a /home/tong/Source/tengine-ingress-Tengine-Ingress-v1.1.0/rootfs/etc/nginx/lua /etc/nginx/lua

 

5 修改ingress配置文件

  1 # Configuration checksum: 18225032199610257382
  2 
  3 # setup custom paths that do not require root access
  4 pid /tmp/nginx.pid;
  5 
  6 user root;
  7 
  8 error_log /var/log/nginx/error.log debug;
  9 #load_module /etc/nginx/modules/ngx_http_geoip_module.so;
 10 
 11 daemon off;
 12 
 13 worker_processes 2;
 14 
 15 worker_rlimit_nofile 523264;
 16 
 17 worker_shutdown_timeout 240s ;
 18 
 19 events {
 20     multi_accept on;
 21     worker_connections 16384;
 22     accept_mutex on;
 23     use epoll;
 24 }
 25 
 26 #include /etc/nginx/apps/proc.conf;
 27 processes {
 28     process strategy {}
 29 }
 30 
 31 http {
 32     lua_package_path "/etc/nginx/lua/?.lua;;";
 33     lua_package_cpath "/usr/local/share/lua/5.1/?.so;;";
 34 
 35     lua_shared_dict csp_data 10m;
 36     lua_shared_dict deny_lock 1m;
 37     lua_shared_dict deny_data 1000m;
 38 
 39     lua_use_default_type off;
 40 
 41     lua_shared_dict balancer_ewma 10M;
 42     lua_shared_dict balancer_ewma_last_touched_at 10M;
 43     lua_shared_dict balancer_ewma_locks 1M;
 44     lua_shared_dict certificate_data 20M;
 45     lua_shared_dict certificate_servers 5M;
 46     lua_shared_dict configuration_data 20M;
 47 
 48     init_by_lua_block {
 49         collectgarbage("collect")
 50 
 51         -- init modules
 52         local ok, res
 53 
 54         ok, res = pcall(require, "lua_ingress")
 55         if not ok then
 56         error("require failed: " .. tostring(res))
 57         else
 58         lua_ingress = res
 59         lua_ingress.set_config( {
 60             use_forwarded_headers = false,
 61             use_proxy_protocol = false,
 62             is_ssl_passthrough_enabled = false,
 63             http_redirect_code = 308,
 64             listen_ports = {
 65                 ssl_proxy = "442", https = "443"
 66             }
 67             ,
 68 
 69             hsts = true,
 70             hsts_max_age = 15724800,
 71             hsts_include_subdomains = true,
 72             hsts_preload = false,
 73         }
 74         )
 75         end
 76 
 77         ok, res = pcall(require, "configuration")
 78         if not ok then
 79         error("require failed: " .. tostring(res))
 80         else
 81         configuration = res
 82         end
 83 
 84         ok, res = pcall(require, "balancer")
 85         if not ok then
 86         error("require failed: " .. tostring(res))
 87         else
 88         balancer = res
 89         end
 90 
 91         ok, res = pcall(require, "monitor")
 92         if not ok then
 93         error("require failed: " .. tostring(res))
 94         else
 95         monitor = res
 96         end
 97 
 98         ok, res = pcall(require, "certificate")
 99         if not ok then
100         error("require failed: " .. tostring(res))
101         else
102         certificate = res
103         end
104 
105         ok, res = pcall(require, "plugins")
106         if not ok then
107         error("require failed: " .. tostring(res))
108         else
109         plugins = res
110         end
111         -- load all plugins that'll be used here
112         plugins.init( {
113         }
114         )
115     }
116 
117     init_worker_by_lua_block {
118         lua_ingress.init_worker()
119         balancer.init_worker()
120 
121         monitor.init_worker()
122 
123         plugins.run()
124     }
125 
126     geo $dontlog {
127         default 0;
128         127.0.0.1/32 1;
129         192.168.0.0/16 1;
130         121.0.29.226/32 1;
131     }
132     geo $nolog {
133         default 0;
134     }
135     geo $no_reqstatus {
136         default 0;
137     }
138     geo $x_connection {
139         default "";
140     }
141 
142     # start hot reload config
143     ingress_gateway_hash_num shm_service_cfg 500000;
144     ingress_gateway_pool_size shm_service_cfg 256m;
145     ingress_gateway_update_interval shm_service_cfg 10s;
146     ingress_gateway_shm_config shm_service_cfg ShmServiceCfg 256m /etc/nginx/shm_service_cfg.lock;
147 
148     #geoip_country /etc/nginx/geoip/GeoIP.dat;
149     #geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
150     #geoip_org /etc/nginx/geoip/GeoIPASNum.dat;
151     #geoip_proxy_recursive on;
152 
153     #aio threads;
154     #aio_write on;
155 
156     tcp_nopush on;
157     tcp_nodelay on;
158 
159     log_subrequest on;
160 
161     reset_timedout_connection on;
162 
163     keepalive_timeout 75s;
164     keepalive_requests 100;
165 
166     client_body_temp_path /tmp/client-body;
167     fastcgi_temp_path /tmp/fastcgi-temp;
168     proxy_temp_path /tmp/proxy-temp;
169 
170     client_header_buffer_size 1k;
171     client_header_timeout 60s;
172     large_client_header_buffers 4 8k;
173     client_body_buffer_size 8k;
174     client_body_timeout 60s;
175 
176     http2_max_concurrent_streams 128;
177 
178     types_hash_max_size 2048;
179     server_names_hash_max_size 1024;
180     server_names_hash_bucket_size 32;
181     map_hash_bucket_size 130;
182 
183     proxy_headers_hash_max_size 512;
184     proxy_headers_hash_bucket_size 64;
185 
186     variables_hash_bucket_size 256;
187     variables_hash_max_size 2048;
188 
189     underscores_in_headers off;
190     ignore_invalid_headers on;
191 
192     limit_req_status 503;
193     limit_conn_status 503;
194 
195     #include /etc/nginx/mime.types;
196     default_type text/html;
197 
198     gzip on;
199     gzip_comp_level 5;
200     gzip_http_version 1.1;
201     gzip_min_length 256;
202     gzip_types application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/javascript text/plain text/x-component;
203     gzip_proxied any;
204     gzip_vary on;
205 
206     # Custom headers for response
207 
208     server_tokens on;
209 
210     # disable warnings
211     uninitialized_variable_warn off;
212 
213     # Additional available variables:
214     # $namespace
215     # $ingress_name
216     # $service_name
217     # $service_port
218     log_format upstreaminfo '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id';
219 
220     map $request_uri $loggable {
221 
222         default 1;
223     }
224 
225     access_log /var/log/nginx/access.log upstreaminfo if=$loggable;
226 
227     #error_log /var/log/nginx/error.log notice;
228 
229     resolver 10.96.0.10 valid=30s;
230 
231     # See https://www.nginx.com/blog/websocket-nginx
232     map $http_upgrade $connection_upgrade {
233         default upgrade;
234 
235         # See http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
236         '' '';
237 
238     }
239 
240     # Reverse proxies can detect if a client provides a X-Request-ID header, and pass it on to the backend server.
241     # If no such header is provided, it can provide a random value.
242     map $http_x_request_id $req_id {
243         default $http_x_request_id;
244 
245         ""
246         $request_id;
247 
248     }
249 
250     # Create a variable that contains the literal $ character.
251     # This works because the geo module will not resolve variables.
252     geo $literal_dollar {
253         default "$";
254     }
255 
256     server_name_in_redirect off;
257     port_in_redirect off;
258 
259     #ssl_protocols TLSv1.2;
260     #ssl_early_data off;
261 
262     # turn on session caching to drastically improve performance
263 
264     #ssl_session_cache builtin:1000 shared:
265     #SSL:10m;
266     #ssl_session_timeout 10m;
267 
268     # allow configuring ssl session tickets
269     #ssl_session_tickets on;
270 
271     # slightly reduce the time-to-first-byte
272     #ssl_buffer_size 4k;
273 
274     # allow configuring custom ssl ciphers
275     #ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
276     #ssl_prefer_server_ciphers on;
277 
278     #ssl_ecdh_curve auto;
279 
280     # PEM sha: 24abab98ff48f9134bf7ede71a6f651b32494be6
281     #ssl_certificate /etc/ingress-controller/ssl/default-fake-certificate.pem;
282     #ssl_certificate_key /etc/ingress-controller/ssl/default-fake-certificate.pem;
283 
284     #xquic_ssl_certificate /etc/ingress-controller/ssl/default-fake-certificate.pem;
285     #xquic_ssl_certificate_key /etc/ingress-controller/ssl/default-fake-certificate.pem;
286 
287     #proxy_ssl_session_reuse on;
288 
289     upstream upstream_balancer {
290         ### Attention!!!
291         #
292         # We no longer create "upstream" section for every backend.
293         # Backends are handled dynamically using Lua. If you would like to debug
294         # and see what backends ingress-nginx has in its memory you can
295         # install our kubectl plugin https://kubernetes.github.io/ingress-nginx/kubectl-plugin.
296         # Once you have the plugin you can use "kubectl ingress-nginx backends" command to
297         # inspect current backends.
298         #
299         ###
300 
301         server 0.0.0.1;
302         # placeholder
303 
304         balancer_by_lua_block {
305             balancer.balance()
306         }
307 
308         keepalive 32;
309 
310         keepalive_timeout 60s;
311         keepalive_requests 100;
312 
313     }
314 
315     # Cache for internal auth checks
316     proxy_cache_path /tmp/nginx-cache-auth levels=1:2 keys_zone=auth_cache:10m max_size=128m inactive=30m use_temp_path=off;
317 
318     # Global filters
319 
320     ## start server _
321     server {
322         server_name _ ;
323 
324         listen 80 default_server reuseport backlog=4096 ;
325         listen [::]:80 default_server reuseport backlog=4096 ;
326     #listen 443 default_server reuseport backlog=4096 ssl http2 ;
327     #listen [::]:443 default_server reuseport backlog=4096 ssl http2 ;
328     #listen 443 default_server reuseport backlog=4096 xquic ;
329     #listen [::]:443 default_server reuseport backlog=4096 xquic ;
330 
331         ingress_gateway shm_service_cfg;
332         ingress_gateway_metadata "ssl-protocols" $metadata_ssl_protocols;
333 
334         # set log host
335         set $log_host $host;
336 
337         set $proxy_upstream_name "-";
338 
339     #ssl_certificate_by_lua_block {
340     #    certificate.call()
341     #}
342 
343         location = /status.tengine {
344             if ($host !~* "^\d{1,3}(\.\d{1,3}){3}|^status\.tengine\.com$") {
345                 return 404;
346                 break;
347             }
348         #sysguard off;
349             access_log off;
350             root /etc/nginx/htdocs;
351         }
352 
353         location / {
354 
355             ingress_gateway_metadata "namespace" $metadata_namespace;
356             ingress_gateway_metadata "ingress_name" $metadata_ingress_name;
357             ingress_gateway_metadata "service_name" $metadata_service_name;
358             ingress_gateway_metadata "service_port" $metadata_service_port;
359             ingress_gateway_metadata "location_path" $metadata_location_path;
360             set $namespace $metadata_namespace;
361             set $ingress_name $metadata_ingress_name;
362             set $service_name $metadata_service_name;
363             set $service_port $metadata_service_port;
364             set $location_path $metadata_location_path;
365 
366 
367             set $upstream_read_time $ingress_read_timeout;
368             set $https_host_mode $ingress_force_https;
369         #include /etc/nginx/apps/ssl.conf;
370             set $proxy_upstream_name $ingress_route_target;
371             add_header Ups-Target-Key $ingress_route_target always;
372             add_header X-protocol $server_protocol always;
373 
374             add_header Alt-Svc 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000' always;
375 
376             if ($ingress_route_target = "") {
377                 return 404;
378                 break;
379             }
380 
381             # be careful with `access_by_lua_block` and `satisfy any` directives as satisfy any
382             # will always succeed when there's `access_by_lua_block` that does not have any lua code doing `ngx.exit(ngx.DECLINED)`
383             # other authentication method such as basic auth or external auth useless - all requests will be allowed.
384             #access_by_lua_block {
385             #}
386 
387             header_filter_by_lua_block {
388                 lua_ingress.header()
389                 plugins.run()
390             }
391 
392             body_filter_by_lua_block {
393             }
394 
395             log_by_lua_block {
396                 balancer.log()
397 
398                 monitor.call()
399 
400                 plugins.run()
401             }
402 
403             #sysguard off;
404 
405             port_in_redirect off;
406 
407             set $balancer_ewma_score -1;
408 
409             set $proxy_host $proxy_upstream_name;
410             set $pass_access_scheme $scheme;
411 
412             set $pass_server_port $server_port;
413 
414             set $best_http_host $http_host;
415             set $pass_port $pass_server_port;
416 
417             set $proxy_alternative_upstream_name "";
418 
419             # CORS
420 
421             ingress_gateway_metadata "enable-cors" $metadata_enable_cors;
422             ingress_gateway_metadata "cors-allow-origin" $metadata_cors_allow_origin;
423             ingress_gateway_metadata "cors-max-age" $metadata_cors_max_age;
424             ingress_gateway_metadata "cors-allow-credentials" $metadata_cors_allow_credentials;
425             ingress_gateway_metadata "cors-allow-methods" $metadata_cors_allow_methods;
426             ingress_gateway_metadata "cors-allow-headers" $metadata_cors_allow_headers;
427 
428             set $enable_cors_options_credentials "${metadata_enable_cors}_${request_method}_${metadata_cors_allow_credentials}";
429             set $metadata_enable_cors_credentials "${metadata_enable_cors}_${metadata_cors_allow_credentials}";
430 
431             client_max_body_size 1m;
432 
433             proxy_set_header Host $best_http_host;
434 
435             # Pass the extracted client certificate to the backend
436 
437             # Allow websocket connections
438             proxy_set_header Upgrade $http_upgrade;
439 
440             proxy_set_header Connection $connection_upgrade;
441 
442             proxy_set_header X-Request-ID $req_id;
443             proxy_set_header X-Real-IP $remote_addr;
444 
445             proxy_set_header X-Forwarded-For $remote_addr;
446 
447             proxy_set_header X-Forwarded-Host $best_http_host;
448             proxy_set_header X-Forwarded-Port $pass_port;
449             proxy_set_header X-Forwarded-Proto $pass_access_scheme;
450 
451             proxy_set_header X-Scheme $pass_access_scheme;
452 
453             # Pass the original X-Forwarded-For
454             proxy_set_header X-Original-Forwarded-For $http_x_forwarded_for;
455 
456             # tengine headers
457             proxy_set_header X-Real-Scheme $scheme;
458             proxy_set_header Web-Server-Type tengine-ingress;
459             proxy_set_header X-Request-From tengine-ingress;
460             proxy_set_header WL-Proxy-Client-IP $remote_addr;
461             proxy_set_header Proxy-X-Forwarded-For $proxy_add_x_forwarded_for;
462             proxy_set_header X-Client-Http2 $http2;
463             proxy_set_header X-Connection $x_connection;
464 
465             # mitigate HTTPoxy Vulnerability
466             # https://www.nginx.com/blog/mitigating-the-httpoxy-vulnerability-with-nginx/
467             proxy_set_header Proxy "";
468 
469             # Custom headers to proxied server
470 
471             if ($enable_cors_options_credentials = "true_OPTIONS_true") {
472                 # Cors Preflight methods needs additional options and different Return Code
473                 ##more_set_headers 'Access-Control-Allow-Origin: $metadata_cors_allow_origin';
474                 #more_set_headers 'Access-Control-Allow-Credentials: $metadata_cors_allow_credentials';
475                 #more_set_headers 'Access-Control-Allow-Methods: $metadata_cors_allow_methods';
476                 #more_set_headers 'Access-Control-Allow-Headers: $metadata_cors_allow_headers';
477                 #more_set_headers 'Access-Control-Max-Age: $metadata_cors_max_age';
478                 #more_set_headers 'Content-Type: text/plain charset=UTF-8';
479                 #more_set_headers 'Content-Length: 0';
480                 return 204;
481             }
482 
483             if ($enable_cors_options_credentials = "true_OPTIONS_false") {
484                 # Cors Preflight methods needs additional options and different Return Code
485                 #more_set_headers 'Access-Control-Allow-Origin: $metadata_cors_allow_origin';
486                 #more_set_headers 'Access-Control-Allow-Methods: $metadata_cors_allow_methods';
487                 #more_set_headers 'Access-Control-Allow-Headers: $metadata_cors_allow_headers';
488                 #more_set_headers 'Access-Control-Max-Age: $metadata_cors_max_age';
489                 #more_set_headers 'Content-Type: text/plain charset=UTF-8';
490                 #more_set_headers 'Content-Length: 0';
491                 return 204;
492             }
493 
494             if ($metadata_enable_cors_credentials = "true_true") {
495                 #more_set_headers 'Access-Control-Allow-Origin: $metadata_cors_allow_origin';
496                 #more_set_headers 'Access-Control-Allow-Credentials: $metadata_cors_allow_credentials';
497                 #more_set_headers 'Access-Control-Allow-Methods: $metadata_cors_allow_methods';
498                 #more_set_headers 'Access-Control-Allow-Headers: $metadata_cors_allow_headers';
499             }
500 
501             if ($metadata_enable_cors_credentials = "true_false") {
502                 #more_set_headers 'Access-Control-Allow-Origin: $metadata_cors_allow_origin';
503                 #more_set_headers 'Access-Control-Allow-Methods: $metadata_cors_allow_methods';
504                 #more_set_headers 'Access-Control-Allow-Headers: $metadata_cors_allow_headers';
505             }
506 
507             proxy_connect_timeout 5s;
508             proxy_send_timeout 60s;
509             proxy_read_timeout 60s;
510 
511             proxy_buffering off;
512             proxy_buffer_size 4k;
513             proxy_buffers 4 4k;
514 
515             proxy_max_temp_file_size 1024m;
516 
517             proxy_request_buffering on;
518             proxy_http_version 1.1;
519 
520             proxy_cookie_domain off;
521             proxy_cookie_path off;
522 
523             # In case of errors try the next upstream server before returning an error
524             proxy_next_upstream error timeout;
525             proxy_next_upstream_timeout 0;
526             proxy_next_upstream_tries 3;
527 
528             proxy_pass http://upstream_balancer;
529 
530             proxy_redirect off;
531 
532         }
533 
534         location /robots.txt {
535             header_filter_by_lua_block {
536                 lua_ingress.header()
537                 plugins.run()
538             }
539 
540             root /etc/nginx/htdocs;
541 
542         #sysguard off;
543             set $log_host "robots.tengine.com";
544             proxy_set_header Host $http_host;
545             proxy_set_header X-Request-From tengine-ingress;
546 
547             set $upstream_read_time $ingress_read_timeout;
548             set $https_host_mode $ingress_force_https;
549         #include /etc/nginx/apps/ssl.conf;
550             set $proxy_upstream_name $ingress_route_target;
551             add_header Ups-Target-Key $ingress_route_target always;
552             add_header X-protocol $server_protocol always;
553 
554             add_header Alt-Svc 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000' always;
555 
556             set $https_use_timing "*";
557             if ($ingress_route_target = "") {
558                 return 404;
559                 break;
560             }
561             ingress_gateway_metadata "disable-robots" $metadata_disable_robots;
562             if ($metadata_disable_robots = "false") {
563                 proxy_pass http://upstream_balancer;
564             }
565 
566         }
567 
568         # health checks in cloud providers require the use of port 80
569         location /healthz {
570 
571             access_log off;
572             return 200;
573         }
574 
575         # this is required to avoid error if nginx is being monitored
576         # with an external software (like sysdig)
577         location /nginx_status {
578 
579             allow 127.0.0.1;
580 
581             allow ::1;
582 
583             deny all;
584 
585         #sysguard off;
586             access_log off;
587             stub_status on;
588         }
589 
590         location /traffic_status {
591 
592             allow 127.0.0.1;
593 
594             allow ::1;
595 
596             deny all;
597 
598         #sysguard off;
599             access_log off;
600             #req_status_show;
601         }
602 
603     }
604     ## end server _
605 
606     # backend for when default-backend-service is not configured or it does not have endpoints
607     server {
608         listen 8181 default_server reuseport backlog=4096;
609         listen [::]:8181 default_server reuseport backlog=4096;
610         set $proxy_upstream_name "internal";
611 
612         access_log off;
613 
614         location / {
615             return 404;
616         }
617     }
618 
619     # default server, used for NGINX healthcheck and access to nginx stats
620     server {
621         listen 127.0.0.1:10246;
622         server_name status.tengine.com;
623         set $proxy_upstream_name "internal";
624 
625     #sysguard off;
626 
627         keepalive_timeout 0;
628         gzip off;
629 
630         access_log off;
631 
632         if ($host !~* "^\d{1,3}(\.\d{1,3}){3}|^status\.tengine\.com$") {
633             set $log_host "notfound";
634             return 404;
635             break;
636         }
637 
638         location /healthz {
639             return 200;
640         }
641 
642         location /is-dynamic-lb-initialized {
643             content_by_lua_block {
644                 local configuration = require("configuration")
645                 local backend_data = configuration.get_backends_data()
646                 if not backend_data then
647                 ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
648                 return
649                 end
650 
651                 ngx.say("OK")
652                 ngx.exit(ngx.HTTP_OK)
653             }
654         }
655 
656         location /nginx_status {
657             stub_status on;
658         }
659 
660         location /configuration {
661             client_max_body_size 21m;
662             client_body_buffer_size 21m;
663             proxy_buffering off;
664 
665             content_by_lua_block {
666                 configuration.call()
667             }
668         }
669 
670         location / {
671             content_by_lua_block {
672                 ngx.exit(ngx.HTTP_NOT_FOUND)
673             }
674         }
675 
676         # for health check on alibaba
677         location = /status.tengine {
678             root /etc/nginx/htdocs;
679         }
680 
681         location = /traffic_status {
682             allow 10.0.0.0/8;
683             allow 11.0.0.0/8;
684             allow 172.16.0.0/12;
685             allow 127.0.0.1/32;
686             allow 192.168.0.0/16;
687             deny all;
688             #req_status_show;
689         }
690     }
691 }
692 
693 stream {
694     lua_package_path "/etc/nginx/lua/?.lua;/etc/nginx/lua/vendor/?.lua;;";
695     lua_package_cpath "/usr/local/share/lua/5.1/?.so;;";
696     
697     lua_shared_dict tcp_udp_configuration_data 5M;
698 
699     init_by_lua_block {
700         collectgarbage("collect")
701 
702         -- init modules
703         local ok, res
704 
705         ok, res = pcall(require, "configuration")
706         if not ok then
707         error("require failed: " .. tostring(res))
708         else
709         configuration = res
710         end
711 
712         ok, res = pcall(require, "tcp_udp_configuration")
713         if not ok then
714         error("require failed: " .. tostring(res))
715         else
716         tcp_udp_configuration = res
717         end
718 
719         ok, res = pcall(require, "tcp_udp_balancer")
720         if not ok then
721         error("require failed: " .. tostring(res))
722         else
723         tcp_udp_balancer = res
724         end
725     }
726 
727     init_worker_by_lua_block {
728         tcp_udp_balancer.init_worker()
729     }
730 
731     lua_add_variable $proxy_upstream_name;
732 
733     log_format log_stream '[$remote_addr] [$time_local] $protocol $status $bytes_sent $bytes_received $session_time';
734 
735     access_log /var/log/nginx/access.log log_stream ;
736 
737     #error_log /var/log/nginx/error.log;
738 
739     upstream upstream_balancer {
740         server 0.0.0.1:1234;
741         # placeholder
742 
743         balancer_by_lua_block {
744             tcp_udp_balancer.balance()
745         }
746     }
747 
748     server {
749         listen 127.0.0.1:10247;
750 
751         access_log off;
752 
753         content_by_lua_block {
754             tcp_udp_configuration.call()
755         }
756     }
757 
758     # TCP services
759 
760     # UDP services
761 
762 }
View Code

 

6 编译命令

sudo ./configure --with-http_lua_module --with-luajit-inc=/usr/include/luajit-2.1/ --with-luajit-lib=/usr/lib/x86_64-linux-gnu/ --add-module=modules/mod_common --add-module=modules/mod_strategy --add-module=modules/ngx_ingress_module/ --with-openssl=/home/tong/Source/Tongsuo-8.4-stable/ --with-cc-opt="-Wno-error=cast-function-type -g -O0" --with-http_v2_module --with-stream --with-stream_ssl_module --add-module=/home/tong/Source/stream-lua-nginx-module-0.0.13/

 

7 运行命令

sudo ./objs/nginx -c /etc/nginx/tengine-ingress-nginx.conf

 

posted on 2025-03-14 11:02  toong  阅读(31)  评论(0)    收藏  举报