This documentation is part of the "Projects with Books" initiative at zenOSmosis.
The source code for this project is available on GitHub.
Mosquitto Configuration
Relevant source files
This document explains the configuration settings in the mosquitto.conf file, which controls the behavior of the Eclipse Mosquitto MQTT broker. This includes listener configuration, port assignments, protocol settings, and access control options.
For information about setting up the Cloudflare Tunnel to expose the broker, see Cloudflare Tunnel Configuration. For information about environment variable configuration, see Environment Variables. For advanced access control configurations available in other branches, see Topic Access Control (ACL)).
Configuration File Overview
The Mosquitto broker is configured through a single configuration file located at mosquitto.conf in the repository root. This file is mounted into the mosquitto Docker container at runtime via a volume mount defined in docker-compose.yml8
File Location and Mounting
| Property | Value |
|---|---|
| Host Path | ./mosquitto.conf |
| Container Path | /mosquitto/config/mosquitto.conf |
| Mount Type | Volume mount (read-only effective) |
| Defined In | docker-compose.yml7-8 |
The configuration is read by the mosquitto service when the container starts. Changes to the host file require a container restart to take effect.
Sources: docker-compose.yml:7-8 mosquitto.conf:1-6
Listener Configuration
The mosquitto.conf file defines two network listeners, each serving different MQTT client connection types. Each listener operates independently and can be configured with different protocols and security settings.
graph TB
subgraph "mosquitto Container"
Config["mosquitto.conf"]
Broker["Mosquitto Broker Process"]
subgraph "Listener 1883"
L1883["listener 1883"]
TCP["Standard MQTT/TCP Protocol"]
Port1883["Port 1883"]
end
subgraph "Listener 9001"
L9001["listener 9001"]
WS["protocol websockets"]
Port9001["Port 9001"]
end
Anonymous["allow_anonymous true"]
end
subgraph "Cloudflared Routing"
CFProxy["cloudflared proxy"]
PublicHostname["Public Hostname"]
end
Config -->|Configures| L1883
Config -->|Configures| L9001
Config -->|Applies to all listeners| Anonymous
L1883 --> TCP
TCP --> Port1883
L9001 --> WS
WS --> Port9001
PublicHostname -->|Routes to| CFProxy
CFProxy -->|mosquitto:9001| Port9001
Broker -->|Binds| Port1883
Broker -->|Binds| Port9001
Listener Architecture
Sources: mosquitto.conf:1-6 docker-compose.yml:4-9 README.md62
Listener 1: Standard MQTT (Port 1883)
The first listener is configured at mosquitto.conf:1-2 and provides standard MQTT protocol support over TCP.
listener 1883
allow_anonymous true
| Setting | Value | Description |
|---|---|---|
| Port | 1883 | Standard MQTT port (IANA registered) |
| Protocol | TCP (implicit) | Default MQTT protocol when no protocol directive specified |
| IP Binding | All interfaces (implicit) | No explicit bind address, so listens on 0.0.0.0 |
| Authentication | Anonymous allowed | Governed by global allow_anonymous true directive |
This listener is suitable for:
- Standard MQTT client libraries
- IoT devices using native MQTT over TCP
- Local network connections within Docker's internal network
Note: This listener is not directly exposed through the Cloudflare Tunnel by default. Only the WebSocket listener (port 9001) is routed through Cloudflare as configured in README.md62
Sources: mosquitto.conf:1-2
Listener 2: WebSocket MQTT (Port 9001)
The second listener is configured at mosquitto.conf:4-5 and provides MQTT over WebSockets, enabling browser-based and web application clients.
listener 9001
protocol websockets
| Setting | Value | Description |
|---|---|---|
| Port | 9001 | Commonly used WebSocket MQTT port |
| Protocol | websockets | MQTT messages encapsulated in WebSocket frames |
| IP Binding | All interfaces (implicit) | Listens on 0.0.0.0 |
| Authentication | Anonymous allowed | Governed by global allow_anonymous true directive |
| Cloudflare Route | Yes | Proxied via cloudflared to public hostname |
This listener is the primary entry point for external clients. The Cloudflare Tunnel routes public traffic to mosquitto:9001 as specified in the tunnel configuration described in README.md62
WebSocket Protocol Details
The protocol websockets directive at mosquitto.conf5 instructs Mosquitto to:
- Accept WebSocket handshake requests on port 9001
- Upgrade HTTP connections to WebSocket protocol
- Extract MQTT control packets from WebSocket frames
- Encapsulate MQTT responses in WebSocket frames
Sources: mosquitto.conf:4-5 README.md62
Access Control Configuration
Anonymous Access
The configuration file enables anonymous access at mosquitto.conf2:
allow_anonymous true
| Setting | Value | Security Impact |
|---|---|---|
| Directive | allow_anonymous | Controls whether unauthenticated connections are permitted |
| Value | true | All clients can connect without credentials |
| Scope | Global | Applies to all listeners (1883 and 9001) |
| ACL Enforcement | None | No access control list is configured in this file |
Security Implications
Important Security Considerations:
-
No Authentication: Any client that can reach the broker through the Cloudflare Tunnel can connect without providing credentials.
-
No Authorization: Connected clients have unrestricted access to:
- Publish to any topic
- Subscribe to any topic
- Use wildcard subscriptions (
#,+)
-
Network-Level Security Only: Security relies entirely on:
- Cloudflare's edge network protections
- The secrecy of the public hostname
- Optional Cloudflare Zero Trust policies (not configured by default)
-
Trust Boundary: The configuration assumes all traffic reaching the mosquitto container through cloudflared is trusted. There is no defense-in-depth at the MQTT layer.
For enhanced security configurations with user authentication and topic-level ACLs, see Topic Access Control (ACL)) which documents the protected-no-wildcard branch implementation.
Sources: mosquitto.conf2 README.md:5-11
Configuration Directives Reference
The table below provides a complete reference for all directives present in mosquitto.conf:1-6:
| Line | Directive | Value | Scope | Description |
|---|---|---|---|---|
| 1 | listener | 1883 | Listener-specific | Defines a network listener on port 1883 with default TCP protocol |
| 2 | allow_anonymous | true | Global | Permits connections without username/password authentication |
| 3 | (blank) | - | - | Separator for readability |
| 4 | listener | 9001 | Listener-specific | Defines a network listener on port 9001 |
| 5 | protocol | websockets | Listener-specific | Configures the listener at line 4 to use WebSocket protocol |
| 6 | (blank) | - | - | End of file |
Sources: mosquitto.conf:1-6
graph TB
subgraph "Host Filesystem"
HostFile["./mosquitto.conf\nRepository root"]
HostContent["Line 1: listener 1883\nLine 2: allow_anonymous true\nLine 4: listener 9001\nLine 5: protocol websockets"]
end
subgraph "Docker Volume Mount"
Mount["Volume Mount Definition\ndocker-compose.yml:7-8"]
end
subgraph "mosquitto Container"
ContainerFile["/mosquitto/config/mosquitto.conf"]
MosqProcess["mosquitto process\nreads config at startup"]
Listener1["Listener 1883\nTCP"]
Listener2["Listener 9001\nWebSockets"]
end
HostFile -->|Contains| HostContent
HostContent -->|Mounted via| Mount
Mount -->|As| ContainerFile
ContainerFile -->|Read by| MosqProcess
MosqProcess -->|Creates| Listener1
MosqProcess -->|Creates| Listener2
Configuration in Docker Context
Volume Mount Mechanism
The mosquitto configuration file is made available to the container through Docker's volume mounting mechanism:
Mount Details:
| Property | Value |
|---|---|
| Host Source | ./mosquitto.conf (relative to docker-compose.yml) |
| Container Destination | /mosquitto/config/mosquitto.conf |
| Mount Mode | Read-write (default), but container treats as read-only |
| Configuration Source | docker-compose.yml7-8 |
Configuration Loading Process
- Container Start: Docker Compose starts the mosquitto container per docker-compose.yml:4-9
- Volume Mount: Docker mounts
./mosquitto.confinto the container before process execution - Process Initialization: The mosquitto process starts and reads
/mosquitto/config/mosquitto.conf - Listener Creation: Mosquitto binds to ports 1883 and 9001 based on listener directives
- Ready State: Container enters healthy state, ready to accept connections
Runtime Behavior:
- Changes to
mosquitto.confon the host filesystem do not automatically apply to the running container - The mosquitto process must be restarted to reload configuration:
docker compose restart mosquitto - During development, use
docker compose downanddocker compose upto ensure clean configuration reload
Sources: docker-compose.yml:4-9 mosquitto.conf:1-6
Configuration File Location in Project Structure
The mosquitto.conf file is:
- Located in the repository root directory
- Tracked in version control (committed to Git)
- Mounted into the container at runtime
- Safe to modify and commit (contains no secrets)
This is in contrast to:
.envfile: Contains secrets, excluded via .gitignoredata/directory: Contains runtime data, excluded via .gitignore
Sources: docker-compose.yml:7-8 mosquitto.conf:1-6
Minimal Configuration Justification
The current mosquitto.conf implements a minimal viable configuration with only essential directives. This design choice provides:
Advantages
| Aspect | Benefit |
|---|---|
| Simplicity | Easy to understand for first-time users |
| Quick Setup | No complex authentication configuration required |
| Cloudflare Security | Network-level protection offloaded to Cloudflare Tunnel |
| Debugging | Fewer variables when troubleshooting connectivity |
Omitted Configurations
The following common Mosquitto directives are not present in mosquitto.conf:1-6:
password_file: No password-based authenticationacl_file: No topic-level access controlpersistence: No message persistence across restartslog_dest: Uses default logging to stdout (captured by Docker)max_connections: No connection limit imposedmax_queued_messages: Uses Mosquitto defaultsmessage_size_limit: Uses Mosquitto defaults
For implementations requiring these features, see Topic Access Control (ACL)) and Production Considerations.
Sources: mosquitto.conf:1-6