1. 在服务器上部署mqtt服务
2.在宝塔上配置域名+证书
3.nginx配置websocket
server
{listen 80;listen 443 ssl http2;server_name ws-xx.example.com;index index.php index.html index.htm default.php default.htm default.html;root /www/wwwroot/ws-xx.example.com;location / { proxy_pass http://ip地址:8084;proxy_http_version 1.1;proxy_set_header Upgrade websocket;proxy_set_header Connection "upgrade";} #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则#error_page 404/404.html;ssl_certificate /www/server/panel/vhost/cert/ws-xx.example.com/fullchain.pem;ssl_certificate_key /www/server/panel/vhost/cert/ws-xx.example.com/privkey.pem;ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;ssl_prefer_server_ciphers on;ssl_session_cache shared:SSL:10m;ssl_session_timeout 10m;add_header Strict-Transport-Security "max-age=31536000";error_page 497 https://$host$request_uri;#SSL-END#ERROR-PAGE-START 错误页配置,可以注释、删除或修改#error_page 404 /404.html;#error_page 502 /502.html;#ERROR-PAGE-END#PHP-INFO-START PHP引用配置,可以注释或修改include enable-php-00.conf;#PHP-INFO-END#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效include /www/server/panel/vhost/rewrite/ws-xx.example.com.conf;#REWRITE-END#禁止访问的文件或目录location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md){return 404;}#一键申请SSL证书验证目录相关设置location ~ \.well-known{allow all;}#禁止在证书验证目录放入敏感文件if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {return 403;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;error_log /dev/null;access_log /dev/null;}location ~ .*\.(js|css)?${expires 12h;error_log /dev/null;access_log /dev/null;}access_log /www/wwwlogs/ws-xx.example.com.log;error_log /www/wwwlogs/ws-xx.example.com.error.log;
}
4.登录mqtt控制台,配置监听器
只要配置证书和key即可,最最关键一点就是 验证服务器证书 这一处要关闭掉,否则导致wss连接失败
5.打开网页客户端并配置连接:http://www.emqx.io/online-mqtt-client
配置完毕点击 连接 即可。
---------------------------------------------------------------------------------------------------------------------------------
k8s部署mqtt,使用ingress配置wss访问
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:name: mqttnamespace: mqttannotations:kubesphere.io/alias-name: wsskubesphere.io/creator: admin
spec:ingressClassName: nginxtls:- hosts:- ws-xx.example.comsecretName: example.comrules:- host: ws-xx.example.comhttp:paths:- path: /pathType: ImplementationSpecificbackend:service:name: mqtt-serviceport:number: 8084