openresty + njs 提升系统nginx 的扩展性

使用了docker 构建,具体的参考github,已经包含了一个现成的docker镜像dalongrong/openresty-tengine:debug-njs

参考构建

./configure --add-dynamic-module=path-to-njs/nginx

参考使用

  • app.js
function hello(r) {
    r.return(200, "Hello world!");
}
 
export default {hello};
  • 配置
worker_processes  auto;
user root;  
master_process on;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    root html;
    # 加载配置
    js_import /usr/local/openresty/nginx/html/app.js;
    keepalive_timeout  65;
    gzip  on;
    upstream cluster1 {
        # simple round-robin
        server app2:80;
        server app3:80;
        check interval=1000 rise=2 fall=5 timeout=1000 type=http;
        check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    }  
     upstream cluster2 {
        # simple round-robin
        server app2:80;
        server app3:80;
        check interval=1000 rise=2 fall=5 timeout=1000 type=http;
        check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    } 
    server {
        listen       81;
        server_name  localhost;
        charset utf-8;
        default_type text/html;
        location / {
            index index.html;
        }
        location = /dalongdemo {
            content_by_lua_block {
                ngx.say('Hello,world!')
            }
        }
        location /status {
            healthcheck_status;
        }
        location /app.html {
            sub_filter '<a href="http://dalong:9090/'  '<a href="http://$host/';
            sub_filter_once off;
            sub_filter_types *;
            root html;
        }
        location /css/ {
            trim on;
            trim_js on;
            trim_css on;
            concat on;
            concat_max_files 20;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }    
    }
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        default_type text/html;
        location / {
            proxy_set_header Host $http_host;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
            client_body_buffer_size 10M;
            client_max_body_size 10G;
            proxy_buffers 1024 4k;
            proxy_read_timeout 300;
            proxy_connect_timeout 80;
            proxy_pass http://cluster1;
            footer "<!-- dalong demo-->";  
        }
        location /status {
            healthcheck_status;
        }
        location /dalong {
           # 使用
            js_content app.hello;
        }
        location /app.html {
            sub_filter 'href="http://dalong:9090'  'href="http://$host/';
            sub_filter_once off;
            sub_filter_types *;
            root html;
        }
        location = /dalongdemo {
            content_by_lua_block {
                ngx.say('Hello,world!')
            }
        }
        location /css/ {
            trim on;
            trim_js on;
            trim_css on;
            concat on;
            concat_max_files 20;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }    
    }    
}
  • 效果

 

 


参考调用火焰图

 

 

说明

很多时候大家使用js 写东西可能比较顺手,但是对于lua 不是很顺手,基于openresty+njs 集合起来,可以提升nginx 的灵活性,让前端同学也可以进行模块能力的开发

参考资料

https://github.com/rongfengliang/openresty-tengine-docker
https://nginx.org/en/docs/njs/reference.html
https://nginx.org/en/docs/njs/
https://nginx.org/en/docs/njs/install.html

posted on   荣锋亮  阅读(537)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-04-09 cube.js mssql driver 问题
2019-04-09 使用fpm 方便快速生成postgresql extension分发包
2014-04-09 web性能优化

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示