# How to config Nginx correctly to use HiveMQ broker

**URL:** <https://community.hivemq.com/t/how-to-config-nginx-correctly-to-use-hivemq-broker/171>\
**Category:** HiveMQ Community Edition\
**Created:** [February 26, 2020, 11:27am UTC](https://community.hivemq.com/t/how-to-config-nginx-correctly-to-use-hivemq-broker/171 "2020-02-26T11:27:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![softiran](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@softiran](https://community.hivemq.com/u/softiran)\
**Post date:** [February 26, 2020, 11:27am UTC](https://community.hivemq.com/t/how-to-config-nginx-correctly-to-use-hivemq-broker/171/1 "2020-02-26T11:27:30Z")

</div>

I ran my own MQTT Broker on a remote server by HiveMQ. I setup Nginx and the dashboards works Ok. But I can’t connect to it via a publisher or subscriber. Before I connected to HiveMQ sandbox somehow like this mqtt://broker.hivemq.com:1883 but I can’t do this on my own server. What should I do?

**HiveMQ Log**

```auto
2020-02-26 07:29:07,819 INFO - Extension "Allow All Extension" version 1.0.0 started successfully.
2020-02-26 07:29:09,952 INFO - 5EQfP: no members discovered after 2001 ms: creating cluster as first member
2020-02-26 07:29:09,989 INFO - No user for HiveMQ Control Center configured. Starting with default user
2020-02-26 07:29:09,990 INFO - Starting HiveMQ Control Center on address 127.0.0.1 and port 8080
2020-02-26 07:29:10,217 INFO - Control Center Audit Logging started.
2020-02-26 07:29:10,217 INFO - Started HiveMQ Control Center in 227ms
2020-02-26 07:29:10,230 INFO - Starting TCP listener on address 0.0.0.0 and port 1883
2020-02-26 07:29:10,269 INFO - Started TCP Listener on address 0.0.0.0 and on port 1883
2020-02-26 07:29:10,269 INFO - Started HiveMQ in 6383ms

```

**Ngnix**

```auto
server {
    listen 80;
    client_max_body_size 20M;
    server_name xxxxx.io www.xxxxx.io;
    error_log /var/log/apache2/domains/xxxxx.io.error.log error;

    location / {
        proxy_pass http://0.0.0.0:8080;
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|odt|ods|odp|odf|tar|wav|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ {
            root /home/admin/web/mqtt/hivemq-4.3.1/;
            access_log /var/log/apache2/domains/xxxxx.log combined;
            access_log /var/log/apache2/domains/xxxxx.bytes bytes;
            expires max;
            try_files $uri @fallback;
        }
    }

    location @fallback {
        proxy_pass http://0.0.0.0:8080;
    }

    location ~ /\.ht {return 404;}
    location ~ /\.svn/ {return 404;}
    location ~ /\.git/ {return 404;}
    location ~ /\.hg/ {return 404;}
    location ~ /\.bzr/ {return 404;}

    include /home/admin/conf/web/nginx.xxxxxx.io.conf*;
}

```

**Subscriber**

```auto
const mqtt = require('mqtt')
options = {
  port: 1883,
  host: 'mqtt://104.251.210.224',
  // clientId: "alireza"
}
const client = mqtt.connect('mqtt://104.251.210.224', options)

/**
* The state of the doorphone, defaults to closed
* Possible states : closed, opening, open, closing
*/
// console.log(client)

var state = 'closed'

client.on('connect', () => {
  console.log(client.connected)
  client.subscribe('doorphone/open')
  client.subscribe('doorphone/close')

  // Inform controllers that doorphone is connected
  client.publish('doorphone/connected', 'true')
  sendStateUpdate()
})

client.on('message', (topic, message) => {
    console.log('received message %s %s', topic, message)
    switch (topic) {
        case 'doorphone/open':
          return handleOpenRequest(message)
        case 'doorphone/close':
          return handleCloseRequest(message)
      }
})

function sendStateUpdate () {
    console.log('sending state %s', state)
    client.publish('doorphone/state', state)
}

function handleOpenRequest (message) {
    if (state !== 'open' && state !== 'opening') {
      console.log('opening doorphone door')
      state = 'opening'
      sendStateUpdate()

      // simulate door open after 5 seconds (would be listening to hardware)
      setTimeout(() => {
        state = 'open'
        sendStateUpdate()
      }, 5000)
    }
}

function handleCloseRequest (message) {
    if (state !== 'closed' && state !== 'closing') {
      state = 'closing'
      sendStateUpdate()

      // simulate door closed after 5 seconds (would be listening to hardware)
      setTimeout(() => {
        state = 'closed'
        sendStateUpdate()
      }, 5000)
    }
}

/**
 * Want to notify controller that doorphone is disconnected before shutting down
 */
function handleAppExit (options, err) {
    if (err) {
      console.log(err.stack)
    }

    if (options.cleanup) {
      client.publish('doorphone/connected', 'false')
    }

    if (options.exit) {
      process.exit()
    }
  }

  /**
   * Handle the different ways an application can shutdown
   */
  process.on('exit', handleAppExit.bind(null, {
    cleanup: true
  }))
  process.on('SIGINT', handleAppExit.bind(null, {
    exit: true
  }))
  process.on('uncaughtException', handleAppExit.bind(null, {
    exit: true
  }))

```

---

<div class="post-metadata">

**Author:** ![michael\_w](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/michael_w/32/200_2.png) [@michael\_w](https://community.hivemq.com/u/michael_w)\
**Post date:** [February 26, 2020, 11:40am UTC](https://community.hivemq.com/t/how-to-config-nginx-correctly-to-use-hivemq-broker/171/2 "2020-02-26T11:40:32Z")

</div>

Hi @softiran,

I just copy the answer from Silvio from [here](https://github.com/hivemq/hivemq-mqtt-client/issues/376#issuecomment-591371683)

> Nevertheless a short answer for your problem: your proxy configuration missed configuration for the MQTT port (1883), you only configured the dashboard port (8080).

Greetings,  
Michael from the HiveMQ team

---

<div class="post-metadata">

**Author:** ![softiran](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@softiran](https://community.hivemq.com/u/softiran)\
**Post date:** [February 26, 2020, 11:44am UTC](https://community.hivemq.com/t/how-to-config-nginx-correctly-to-use-hivemq-broker/171/3 "2020-02-26T11:44:24Z")

</div>

Hi @michael_w thanks for your short answer but the problem is that I don’t know how to do it. I am a newbie so I will be glad to pave the way for me. Thanks.

---

<div class="post-metadata">

**Author:** ![hivemq-support](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/hivemq-support/32/357_2.png) [@hivemq-support](https://community.hivemq.com/u/hivemq-support)\
**Post date:** [February 26, 2020, 3:28pm UTC](https://community.hivemq.com/t/how-to-config-nginx-correctly-to-use-hivemq-broker/171/4 "2020-02-26T15:28:02Z")

</div>

Hi @softiran,

You need a setting that looks something like this:

```
upstream hivemq {

server 127.0.0.1:1883;

}

server {

listen 1883;

proxy_pass hivemq;

}

```

Hope this helps.

Kind regards,  
Florian
