原计划是将nchan 构建为一个动态模块(centos 的)但是发现包含了一些gcc 的问题,所以就直接使用了容器构建(alpine)
镜像
直接复用了openresty官方的,添加了nchan模块,具体参考github,同时也包含了一个fat jar dalongrong/openresty:1.21.4.1-alpine-fat
集成使用
这个比较简单,都是开箱即用的,核心是配置
nginx.conf
user root;master_process off;worker_processes 1;events {worker_connections 1024;}http {upstream my_redis_server {nchan_redis_server redis;}include mime.types;default_type text/html;lua_code_cache off;lua_package_path '/opt/lua/?.lua;;';real_ip_header X-Forwarded-For;resolver 127.0.0.11;server {listen 80;charset utf-8;proxy_set_header X-Forwarded-For $remote_addr;proxy_buffering off;proxy_cache off;proxy_set_header Connection '';proxy_http_version 1.1;chunked_transfer_encoding off;default_type text/html;location ~ /redis_sub/(\w+)$ {nchan_subscriber;nchan_channel_id $1;nchan_redis_pass my_redis_server;}location ~ /redis_pub/(\w+)$ {nchan_redis_pass my_redis_server;nchan_publisher;nchan_channel_id $1;}}}docker-compose 文件
version: '3'services:redis:image: redis:7.0.0-bullseyeports:- "6379:6379"app:image: dalongrong/openresty:1.21.4.1-alpine-fatports:- "80:80"volumes:- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"说明
nchan 源码还是值得学习的,以前是大概的了解过nchan的配置使用,实际上如果企业有实时消息的场景,基于此时一个快速的方案,还是值得研究下的
同时利用好openresty 可以更好的扩展nchan
参考资料
https://github.com/rongfengliang/openresty-nchan
https://github.com/slact/nchan
https://nchan.io/










