环境准备
- centos7
- docker
安装
docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8883:8883 -p 8084:8084 -p 18083:18083 emqx/emqx:latest
配置nginx访问
nginx.conf
server {
listen 80;
server_name xxx.xxx.xxx;
rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
}
server {
listen 443 ssl;
server_name xxx.xxx.xxx;
root html;
index index.html index.htm;
ssl_certificate /root/cert/all/xxx.crt;
ssl_certificate_key /root/cert/all/xxx.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# 开启gzip
gzip on;
# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
gzip_comp_level 1;
# 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
# 禁用IE 6 gzip
gzip_disable "MSIE [1-6]\.";
# 设置压缩所需要的缓冲区大小
gzip_buffers 32 4k;
# 设置gzip压缩针对的HTTP协议版本,没做负载的可以不用
# gzip_http_version 1.0;
# wss连接
location /mqtt {
proxy_pass http://xxx:8083/mqtt;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# client_max_body_size 35m;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# dashboard访问
location / {
proxy_pass http://xxx:18083;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_redirect off;
proxy_set_header Host $host;
# 将真实ip存储请求头
proxy_set_header real-ip $remote_addr;
proxy_set_header forwarded-for $proxy_add_x_forwarded_for;
}
}
访问测试
初始用户名和密码是admin-public
记得修改
wss连接测试
记得端口改成443
配置
- 关闭匿名模式
# 进入容器
docker exec -it emqx sh
# 修改配置文件
vi emqx.conf
找到 allow_anonymous
修改值为false
# 在容器内重启emqx
emqx restart
重启完后,再次去连接就会发现需要认证了
- 开启用户名密码认证
参考: https://www.emqx.io/docs/zh/v4.3/advanced/auth-mnesia.html#%E9%A2%84%E8%AE%BE%E8%AE%A4%E8%AF%81%E6%95%B0%E6%8D%AE
开启后添加用户
这有两种添加的方法
- 配置文件里添加
-
进入到容器
-
打开配置文件,直接添加用户
-
重启emqx
emqx restart
- 访问API添加
找到对应的API
用自己熟悉的工具发送请求
这里以PostMan演示
指的注意的是访问需要Basic鉴权
用户名和密码是登录的密码
根据文档去找对应添加的API
文档地址
ACL配置
- 打开ACL
进入容器
修改emqx.conf文件
重启emqx