Kong网关代理MQTT的两种方法

我的前一篇文章,是关于Kong网关实现TCP流代理的方法。使用的目前最新版本的Kong网关软件,新版自带 Kong Manager UI界面。但2.*版本没有提供 UI界面,如果实现TCP流代理。有两种方法,一种是使用第三方 konga 的UI界面来操作,配置方法同 Kong Manager UI类似 。还有一种方式就是本文要讲的。

Kong网关技术底层是通过Openresty(基于NGINX与Lua) 反向代理的能力实现。本文的方法是手动配置Nignx来实现TCP协议代理,同时不影响Kong的正常功能。

接下来,我们来以代理MQTT为例,讲解如何操作。

1、开放防火墙端口

以Centos为kong网关操作系统为例。使用网关的1883代理。开放1883端口。

firewall-cmd --zone=public --add-port=1883/tcp --permanent
firewall-cmd --reload

2 新建NGINX配置文件

进入网关容器内,修改配置文件。

docker exec -it <kong容器id> bash

cd /usr/local/kong
cp nginx.conf custom_nginx.template
 
vi custom_nginx.template
 
修改stream内容如下:
stream {
    include 'nginx-kong-stream.conf';
    include 'emqx_tcp_nginx.conf';
}

3 新建 emqx_tcp_nginx.conf

内容如下

upstream mqtt1883 {
    server 10.10.1.2:1883 weight=1;
}
server {
        listen       1883;
        proxy_pass mqtt1883;
        proxy_buffer_size 3M;
        tcp_nodelay on;
}

4 修改容器根目录下的docker-entrypoint.sh文件 ,将原"-c nginx.conf"修改为"custom_nginx.template"。

exec /usr/local/openresty/nginx/sbin/nginx \
      -p "$PREFIX" \
      -c custom_nginx.template

说明:在这里解释一下,为什么要新建一个custom_nginx.template文件 ,而不直接使用nginx.conf。因为kong网关启动或重载后,会自动生成一个新的nginx.conf文件。如果在文件上直接修改,会被系统自动替换掉。使用新建的文件,不要以.conf为后缀。因为新文件即有原nginx.conf的内容,又包含了新配置的流代理配置,所以不影响kong网关的原有功能。

5 重启容器

docker restart kong
相关推荐
fajianchen7 小时前
什么是HTTP/2协议?NGINX如何支持HTTP/2并提升网站性能?
nginx·http
m0_7482487710 小时前
在 Ubuntu 上安装 Nginx 的详细指南
nginx·ubuntu·postgresql
若云止水1 天前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_os_init 函数
运维·nginx
m0_512744641 天前
Nginx(详解以及如何使用)
运维·服务器·nginx
铁锅与大鹅1 天前
http+nginx
网络协议·nginx·http
s_fox_1 天前
Nginx Embedded Variables 嵌入式变量解析(4)
java·网络·nginx
致奋斗的我们1 天前
Nginx反向代理及负载均衡
linux·运维·mysql·nginx·负载均衡·shell·openeluer
招风的黑耳2 天前
使用Nginx本地部署Axure生成的HTML文件,局域网内浏览器通过IP和地址访问
nginx·html·axure·本地部署
s_fox_2 天前
nginx ngx_http_module(7) 指令详解
运维·nginx·http
若云止水2 天前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_process_options
运维·nginx