How to Configure CE Broker with Domain name via Nginx

  1. We have installed HiveMQ CE Broker in VM.
  2. Made Nginx Configuration to publicly access Broker via IP-Address.
    stream {
    server {
    listen 5679;
    proxy_pass localhost:8884;
    tcp_nodelay on;
    }
    }

but, how to configure Nginx to access CE Broker via Domain-name? Can anyone help for?

Hello @patelaryam

Welcome to HiveMQ Community! As far as I know, it’s not possible. When SERVER block is inside of a STREAM group you cannot set “server_name” directive in Nginx. To access HiveMQ CE broker via domain name you must handle the domain name resolution through your DNS server.

Kind regards,
Diego from HiveMQ Team

Thanks for reply @Diego

   We had also tried without STREAM group i.e. using http-based (as listed below) configuration, but, mqtt-client was never connected with.

created test.conf file with listed content…

upstream hive_mqtt_group {
server localhost:1883;
}

server {
server_name mqtt.tvadartham.com; // this domain already mentioned in DNS Server.

location / {
proxy_http_version 1.1;
proxy_pass http://hive_mqtt_group;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
proxy_set_header Host $host;
}
}

Can you provide a nginx configuration having domain-name mapped with CE Broker?

Hello @patelaryam

You’re welcome. Without STREAM group “proxy_pass” directive will use protocol, “http ” or “https ” then this will not work to connect an MQTT client.

You can find more information on nginx official documentation below.

Module ngx_stream_core_module
Module ngx_http_proxy_module

Kind regards,
Diego from HiveMQ Team

Dear @Diego

I have gone through given modules, but, couldn't find expected solutions from i.e. using http-configuration along with Stream compatibility.

Can you do me a favour? If you have hosted CE Broker with Domain-Name mapping, can you share respective configuration of?

@patelaryam

These nginx configurations that you are getting on the internet with “server_name” directive are to NGINX act as a reverse proxy for a simple WebSocket, this is not related to MQTT (1883) traffic. The only configuration that I have is using STREAM group and as I mentioned above “When SERVER block is inside of a STREAM group you cannot set “server_name” directive in NGINX.

stream {
    upstream hivemq {
        server HIVEMQ_BROKER_IP:1883;
    }

    server {
        listen 1883;
        proxy_pass hivemq;
        proxy_connect_timeout 1s;
        tcp_nodelay on;
    }
}

Kind regards,
Diego from HiveMQ Team

@Diego

As per The Free Public MQTT Broker by HiveMQ - Check out our MQTT Demo, there is a public HiveMQ-Broker accessible with Domain-Name as

Broker: broker.hivemq.com


TCP Port: 1883

How is it configured with Domain-Name?

@patelaryam

By using a DNS entry in your root domain @hivemq.com and pointing it to the correct resource in the backend, nothing related to any NGINX proxy configuration. I think you are misunderstanding the network and DNS concepts here.

Kind regards,
Diego from HiveMQ Team

@Diego

I already added DNS Entry as per attached SS. I really confused to point out to CE-Broker connectivity.

Screenshot from 2023-03-29 16-43-15

From MQTT-Client, if I pass Public-IP, Broker gets connected, but, no idea of TCP-Connection via nginx-mapping using domain-name.

@patelaryam

So the connections to 101.53.153.171 on port 1883 are established?
And the connections to mqtt.tvadartham.com on port 1883 are not established?

Could you please share the results of the following command in your CE-Broker?

netstat -an | grep 1883

Kind regards,
Diego from HiveMQ Team

@Diego

I have attached result of netstat command.

@patelaryam

Now the next 3 commands results

sudo netstat -plnt | grep ':1883'

ifconfig

curl ifconfig.io

Kind regards,
Diego from HiveMQ Team

@Diego

netstat -plnt and ifconfig

Result of ifconfig.io

curl ifconfig.io/ip => 101.53.153.171
curl ifconfig.io/country_code => IN

@patelaryam

Everything looks fine but I cannot see the Linux process that is listening on port 1883, a sample screenshot below

Do you have more than one ethernet interface on this VM?

Kind regards,
Diego from HiveMQ Team

@Diego

It’s the result of netstat -pnlt | grep ':1883'

tcp     0    0 0.0.0.0:1883       0.0.0.0:*     LISTEN     1667/java

@patelaryam

Seems fine! From the computer that you are trying to connect using the domain name, share the results of the command below.

nslookup mqtt.tvadartham.com

Kind regards,
Diego from HiveMQ Team