config.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. .. _configuration:
  2. =============
  3. Configuration
  4. =============
  5. The two main configuration files of the broker are under 'etc/' folder:
  6. +----------------------+-----------------------------------+
  7. | File | Description |
  8. +----------------------+-----------------------------------+
  9. | releases/2.0/vm.args | Erlang VM Arguments |
  10. +----------------------+-----------------------------------+
  11. | etc/emqttd.conf | emqttd broker Config |
  12. +----------------------+-----------------------------------+
  13. ----------------------------
  14. Plugins' Configuration Files
  15. ----------------------------
  16. +----------------------------------------+-----------------------------------+
  17. | File | Description |
  18. +----------------------------------------+-----------------------------------+
  19. | etc/plugins/emqttd_auth_http.conf | HTTP Auth/ACL Plugin Config |
  20. +----------------------------------------+-----------------------------------+
  21. | etc/plugins/emqttd_auth_mongo.conf | MongoDB Auth/ACL Plugin Config |
  22. +----------------------------------------+-----------------------------------+
  23. | etc/plugins/emqttd_auth_mysql.conf | MySQL Auth/ACL Plugin Config |
  24. +----------------------------------------+-----------------------------------+
  25. | etc/plugins/emqttd_auth_pgsql.conf | Postgre Auth/ACL Plugin Config |
  26. +----------------------------------------+-----------------------------------+
  27. | etc/plugins/emqttd_auth_redis.conf | Redis Auth/ACL Plugin Config |
  28. +----------------------------------------+-----------------------------------+
  29. | etc/plugins/emqttd_coap.conf | CoAP Protocol Plugin Config |
  30. +----------------------------------------+-----------------------------------+
  31. | etc/plugins/emqttd_dashboard.conf | Dashboard Plugin Config |
  32. +----------------------------------------+-----------------------------------+
  33. | etc/plugins/emqttd_plugin_template.conf| Template Plugin Config |
  34. +----------------------------------------+-----------------------------------+
  35. | etc/plugins/emqttd_recon.conf | Recon Plugin Config |
  36. +----------------------------------------+-----------------------------------+
  37. | etc/plugins/emqttd_reloader.conf | Reloader Plugin Config |
  38. +----------------------------------------+-----------------------------------+
  39. | etc/plugins/emqttd_sn.conf | MQTT-SN Protocal Plugin Config |
  40. +----------------------------------------+-----------------------------------+
  41. | etc/plugins/emqttd_stomp.conf | Stomp Protocl Plugin Config |
  42. +----------------------------------------+-----------------------------------+
  43. ----------------------------
  44. Modules' Configuration Files
  45. ----------------------------
  46. The modules' configuration files are in etc/modules/ folder, and referrenced by etc/emqttd.conf:
  47. +----------------------------+-----------------------------------+
  48. | File | Description |
  49. +----------------------------+-----------------------------------+
  50. | etc/modules/acl.config | Internal ACL Rules |
  51. +----------------------------+-----------------------------------+
  52. | etc/modules/client.config | Config for ClientId Auth Module |
  53. +----------------------------+-----------------------------------+
  54. | etc/modules/rewrite.config | Config for Rewrite Module |
  55. +----------------------------+-----------------------------------+
  56. | etc/ssl/* | SSL Certfile and Keyfile |
  57. +-----------------------------+----------------------------------+
  58. --------------------
  59. releases/2.0/vm.args
  60. --------------------
  61. Configure and Optimize Erlang VM::
  62. ##-------------------------------------------------------------------------
  63. ## Name of the node: Name@Host
  64. ##-------------------------------------------------------------------------
  65. -name emqttd@127.0.0.1
  66. # or
  67. #-name emqttd@localhost.
  68. ## Cookie for distributed erlang
  69. -setcookie emqttdsecretcookie
  70. ##-------------------------------------------------------------------------
  71. ## Flags
  72. ##-------------------------------------------------------------------------
  73. ## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive
  74. ## (Disabled by default..use with caution!)
  75. ##-heart
  76. -smp true
  77. ## Enable kernel poll and a few async threads
  78. +K true
  79. ## 12 threads/core.
  80. +A 48
  81. ## max process numbers
  82. +P 8192
  83. ## Sets the maximum number of simultaneously existing ports for this system
  84. +Q 8192
  85. ## max atom number
  86. ## +t
  87. ## Set the distribution buffer busy limit (dist_buf_busy_limit) in kilobytes.
  88. ## Valid range is 1-2097151. Default is 1024.
  89. ## +zdbbl 8192
  90. ## CPU Schedulers
  91. ## +sbt db
  92. ##-------------------------------------------------------------------------
  93. ## Env
  94. ##-------------------------------------------------------------------------
  95. ## Increase number of concurrent ports/sockets, deprecated in R17
  96. -env ERL_MAX_PORTS 8192
  97. -env ERTS_MAX_PORTS 8192
  98. -env ERL_MAX_ETS_TABLES 1024
  99. ## Tweak GC to run more often
  100. -env ERL_FULLSWEEP_AFTER 1000
  101. The two most important parameters in releases/2.0/vm.args:
  102. +-------+---------------------------------------------------------------------------+
  103. | +P | Max number of Erlang proccesses. A MQTT client consumes two proccesses. |
  104. | | The value should be larger than max_clients * 2 |
  105. +-------+---------------------------------------------------------------------------+
  106. | +Q | Max number of Erlang Ports. A MQTT client consumes one port. |
  107. | | The value should be larger than max_clients. |
  108. +-------+---------------------------------------------------------------------------+
  109. The name and cookie of Erlang Node should be configured when clustering::
  110. -name emqttd@host_or_ip
  111. ## Cookie for distributed erlang
  112. -setcookie emqttdsecretcookie
  113. ------------------
  114. Log Level and File
  115. ------------------
  116. Logger of emqttd broker is implemented by 'lager' application, which is configured in releases/2.0/sys.config:
  117. .. code-block:: erlang
  118. {lager, [
  119. ...
  120. ]},
  121. Configure log handlers:
  122. .. code-block:: erlang
  123. {handlers, [
  124. {lager_console_backend, info},
  125. {lager_file_backend, [
  126. {formatter_config, [time, " ", pid, " [",severity,"] ", message, "\n"]},
  127. {file, "log/emqttd_info.log"},
  128. {level, info},
  129. {size, 104857600},
  130. {date, "$D0"},
  131. {count, 30}
  132. ]},
  133. {lager_file_backend, [
  134. {formatter_config, [time, " ", pid, " [",severity,"] ", message, "\n"]},
  135. {file, "log/emqttd_error.log"},
  136. {level, error},
  137. {size, 104857600},
  138. {date, "$D0"},
  139. {count, 30}
  140. ]}
  141. ]}
  142. ---------------
  143. etc/emqttd.conf
  144. ---------------
  145. This is the main configuration file for emqttd broker.
  146. File Syntax
  147. -----------
  148. The file uses the Erlang term syntax which is like rebar.config or relx.config:
  149. 1. [ ]: List, seperated by comma
  150. 2. { }: Tuple, Usually {Env, Value}
  151. 3. % : comment
  152. MQTT Protocol Parameters
  153. ------------------------
  154. Maximum ClientId Length
  155. .......................
  156. .. code-block:: erlang
  157. %% Max ClientId Length Allowed.
  158. {mqtt_max_clientid_len, 512}.
  159. Maximum Packet Size
  160. ...................
  161. .. code-block:: erlang
  162. %% Max Packet Size Allowed, 64K by default.
  163. {mqtt_max_packet_size, 65536}.
  164. MQTT Client Idle Timeout
  165. ........................
  166. .. code-block:: erlang
  167. %% Client Idle Timeout.
  168. {mqtt_client_idle_timeout, 30}. % Second
  169. Pluggable Authentication
  170. ------------------------
  171. The emqttd broker supports pluggable authentication mechanism with a list of modules and plugins.
  172. The broker provides Username, ClientId, LDAP and anonymous authentication modules by default:
  173. .. code-block:: erlang
  174. %%--------------------------------------------------------------------
  175. %% Authentication
  176. %%--------------------------------------------------------------------
  177. %% Anonymous: Allow all
  178. {auth, anonymous, []}.
  179. %% Authentication with username, password
  180. {auth, username, [{passwd, "etc/modules/passwd.conf"}]}.
  181. %% Authentication with clientId
  182. {auth, clientid, [{config, "etc/modules/client.conf"}, {password, no}]}.
  183. The modules enabled at the same time compose an authentication chain::
  184. ---------------- ---------------- --------------
  185. Client --> | Anonymous | -ignore-> | Username | -ignore-> | ClientID |
  186. ---------------- ---------------- --------------
  187. | | |
  188. \|/ \|/ \|/
  189. allow | deny allow | deny allow | deny
  190. .. NOTE:: There are also MySQL, Postgre, Redis, MongoDB and HTTP Authentication Plugins.
  191. Username Authentication
  192. .......................
  193. .. code-block:: erlang
  194. %% Authentication with username, password
  195. {auth, username, [{passwd, "etc/modules/passwd.conf"}]}.
  196. Two ways to configure users:
  197. 1. Configure username and plain password in etc/modules/passwd.conf::
  198. {"user1", "passwd1"}.
  199. {"user2", "passwd2"}.
  200. 2. Add user by './bin/emqttd_ctl users' command::
  201. $ ./bin/emqttd_ctl users add <Username> <Password>
  202. ClientID Authentication
  203. .......................
  204. .. code-block:: erlang
  205. %% Authentication with clientId
  206. {auth, clientid, [{config, "etc/modules/client.conf"}, {password, no}]}.
  207. Configure ClientIDs in etc/clients.config::
  208. "testclientid0".
  209. {"testclientid1", "127.0.0.1"}.
  210. {"testclientid2", "192.168.0.1/24"}.
  211. Anonymous Authentication
  212. ........................
  213. Allow any client to connect to the broker::
  214. %% Anonymous: Allow all
  215. {auth, anonymous, []}.
  216. ACL(Authorization)
  217. ------------------
  218. Enable the default ACL module:
  219. .. code-block:: erlang
  220. %% Internal ACL config
  221. {acl, internal, [{config, "etc/modules/acl.conf"}, {nomatch, allow}]}.
  222. Define ACL rules in etc/modules/acl.conf. The rules by default:
  223. .. code-block:: erlang
  224. %% Allow 'dashboard' to subscribe '$SYS/#'
  225. {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
  226. %% Allow clients from localhost to subscribe any topics
  227. {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
  228. %% Deny clients to subscribe '$SYS#' and '#'
  229. {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
  230. %% Allow all by default
  231. {allow, all}.
  232. An ACL rule is an Erlang tuple. The Access control module of emqttd broker matches the rule one by one from top to bottom::
  233. --------- --------- ---------
  234. Client -> | Rule1 | --nomatch--> | Rule2 | --nomatch--> | Rule3 | --> Default
  235. --------- --------- ---------
  236. | | |
  237. match match match
  238. \|/ \|/ \|/
  239. allow | deny allow | deny allow | deny
  240. Sys Interval of Broker
  241. ----------------------
  242. .. code-block:: erlang
  243. %% System interval of publishing $SYS messages
  244. {broker_sys_interval, 60}.
  245. Retained Message Configuration
  246. ------------------------------
  247. Expiration of Retained Message
  248. ...............................
  249. .. code:: erlang
  250. %% Expired after seconds, never expired if 0
  251. {retained_expired_after, 0}.
  252. Maximum Number of Retained Message
  253. ...................................
  254. .. code:: erlang
  255. %% Max number of retained messages
  256. {retained_max_message_num, 100000}.
  257. Maximum Size of Retained Message
  258. ................................
  259. .. code:: erlang
  260. %% Max Payload Size of retained message
  261. {retained_max_playload_size, 65536}.
  262. MQTT Session
  263. ------------
  264. .. code-block:: erlang
  265. %% Max number of QoS 1 and 2 messages that can be “inflight” at one time.
  266. %% 0 means no limit
  267. {session_max_inflight, 100}.
  268. %% Retry interval for redelivering QoS1/2 messages.
  269. {session_unack_retry_interval, 60}.
  270. %% Awaiting PUBREL Timeout
  271. {session_await_rel_timeout, 20}.
  272. %% Max Packets that Awaiting PUBREL, 0 means no limit
  273. {session_max_awaiting_rel, 0}.
  274. %% Statistics Collection Interval(seconds)
  275. {session_collect_interval, 0}.
  276. %% Expired after 2 day (unit: minute)
  277. {session_expired_after, 2880}.
  278. Session parameters:
  279. +------------------------------+----------------------------------------------------------+
  280. | session_max_inflight | Max number of QoS1/2 messages that can be delivered in |
  281. | | the same time |
  282. +------------------------------+----------------------------------------------------------+
  283. | session_unack_retry_interval | Retry interval for unacked QoS1/2 messages. |
  284. +------------------------------+----------------------------------------------------------+
  285. | session_await_rel_timeout | Awaiting PUBREL Timeout |
  286. +------------------------------+----------------------------------------------------------+
  287. | session_max_awaiting_rel | Max number of Packets that Awaiting PUBREL |
  288. +------------------------------+----------------------------------------------------------+
  289. | session_collect_interval | Interval of Statistics Collection |
  290. +------------------------------+----------------------------------------------------------+
  291. | session_expired_after | Expired after (unit: minute) |
  292. +------------------------------+----------------------------------------------------------+
  293. MQTT Message Queue
  294. ------------------
  295. The message queue of session stores:
  296. 1. Offline messages for persistent session.
  297. 2. Pending messages for inflight window is full
  298. Queue parameters:
  299. .. code-block:: erlang
  300. %% Type: simple | priority
  301. {queue_type, simple}.
  302. %% Topic Priority: 0~255, Default is 0
  303. %% {queue_priority, [{"topic/1", 10}, {"topic/2", 8}]}.
  304. %% Max queue length. Enqueued messages when persistent client disconnected,
  305. %% or inflight window is full.
  306. {queue_max_length, infinity}.
  307. %% Low-water mark of queued messages
  308. {queue_low_watermark, 0.2}.
  309. %% High-water mark of queued messages
  310. {queue_high_watermark, 0.6}.
  311. %% Queue Qos0 messages?
  312. {queue_qos0, true}.
  313. +----------------------+---------------------------------------------------+
  314. | queue_type | Queue type: simple or priority |
  315. +----------------------+---------------------------------------------------+
  316. | queue_priority | Topic priority |
  317. +----------------------+---------------------------------------------------+
  318. | queue_max_length | Max Queue size, infinity means no limit |
  319. +----------------------+---------------------------------------------------+
  320. | queue_low_watermark | Low watermark |
  321. +----------------------+---------------------------------------------------+
  322. | queue_high_watermark | High watermark |
  323. +----------------------+---------------------------------------------------+
  324. | queue_qos0 | If Qos0 message queued? |
  325. +----------------------+---------------------------------------------------+
  326. PubSub and Router
  327. -----------------
  328. PubSub Pool Size
  329. ................
  330. .. code-block:: erlang
  331. %% PubSub Pool Size. Default should be scheduler numbers.
  332. {pubsub_pool_size, 8}.
  333. MQTT Bridge Parameters
  334. ----------------------
  335. Max MQueue Size of Bridge
  336. .........................
  337. .. code:: erlang
  338. %% TODO: Bridge Queue Size
  339. {bridge_max_queue_len, 10000}.
  340. Ping Interval of Bridge
  341. .......................
  342. .. code:: erlang
  343. %% Ping Interval of bridge node
  344. {bridge_ping_down_interval, 1}. % second
  345. Extended Modules
  346. ----------------
  347. Presence Module
  348. ...............
  349. 'presence' module will publish presence message to $SYS topic when a client connected or disconnected:
  350. .. code:: erlang
  351. %% Client presence management module. Publish presence messages when
  352. %% client connected or disconnected.
  353. {module, presence, [{qos, 0}]}.
  354. Subscription Module
  355. ...................
  356. 'subscription' module forces the client to subscribe some topics when connected to the broker:
  357. .. code:: erlang
  358. %% Subscribe topics automatically when client connected
  359. {module, subscription, [{"$client/$c", 1}]}.
  360. Rewrite Module
  361. ..............
  362. 'rewrite' module supports to rewrite the topic path:
  363. .. code:: erlang
  364. %% [Rewrite](https://github.com/emqtt/emqttd/wiki/Rewrite)
  365. {module, rewrite, [{config, "etc/modules/rewrite.conf"}]}.
  366. Configure rewrite rules in etc/modules/rewrite.conf::
  367. {topic, "x/#", [
  368. {rewrite, "^x/y/(.+)$", "z/y/$1"},
  369. {rewrite, "^x/(.+)$", "y/$1"}
  370. ]}.
  371. {topic, "y/+/z/#", [
  372. {rewrite, "^y/(.+)/z/(.+)$", "y/z/$2"}
  373. ]}.
  374. Plugins Folder
  375. --------------
  376. .. code:: erlang
  377. %% Dir of plugins' config
  378. {plugins_etc_dir, "etc/plugins/"}.
  379. %% File to store loaded plugin names.
  380. {plugins_loaded_file, "data/loaded_plugins"}.
  381. TCP Listeners
  382. -------------
  383. Configure the TCP listeners for MQTT, MQTT(SSL) and HTTP Protocols.
  384. The most important parameter is 'max_clients' - max concurrent clients allowed.
  385. The TCP Ports occupied by emqttd broker by default:
  386. +-----------+-----------------------------------+
  387. | 1883 | MQTT Port |
  388. +-----------+-----------------------------------+
  389. | 8883 | MQTT(SSL) Port |
  390. +-----------+-----------------------------------+
  391. | 8083 | MQTT(WebSocket), HTTP API Port |
  392. +-----------+-----------------------------------+
  393. .. code-block:: erlang
  394. Listener Parameters:
  395. +-------------+----------------------------------------------------------------+
  396. | acceptors | TCP Acceptor Pool |
  397. +-------------+----------------------------------------------------------------+
  398. | max_clients | Maximum number of concurrent TCP connections allowed |
  399. +-------------+----------------------------------------------------------------+
  400. | access | Access Control by IP, for example: [{allow, "192.168.1.0/24"}] |
  401. +-------------+----------------------------------------------------------------+
  402. | connopts | Rate Limit Control, for example: {rate_limit, "100,10"} |
  403. +-------------+----------------------------------------------------------------+
  404. | sockopts | TCP Socket parameters |
  405. +-------------+----------------------------------------------------------------+
  406. 1883 - Plain MQTT
  407. .................
  408. .. code-block:: erlang
  409. %% Plain MQTT
  410. {listener, mqtt, 1883, [
  411. %% Size of acceptor pool
  412. {acceptors, 16},
  413. %% Maximum number of concurrent clients
  414. {max_clients, 512},
  415. %% Mount point prefix
  416. %% {mount_point, "prefix/"},
  417. %% Socket Access Control
  418. {access, [{allow, all}]},
  419. %% Connection Options
  420. {connopts, [
  421. %% Rate Limit. Format is 'burst, rate', Unit is KB/Sec
  422. %% {rate_limit, "100,10"} %% 100K burst, 10K rate
  423. ]},
  424. %% Socket Options
  425. {sockopts, [
  426. %Set buffer if hight thoughtput
  427. %{recbuf, 4096},
  428. %{sndbuf, 4096},
  429. %{buffer, 4096},
  430. %{nodelay, true},
  431. {backlog, 1024}
  432. ]}
  433. ]}.
  434. 8883 - MQTT(SSL)
  435. ................
  436. .. code-block:: erlang
  437. %% MQTT/SSL
  438. {listener, mqtts, 8883, [
  439. %% Size of acceptor pool
  440. {acceptors, 4},
  441. %% Maximum number of concurrent clients
  442. {max_clients, 512},
  443. %% Mount point prefix
  444. %% {mount_point, "secure/"},
  445. %% Socket Access Control
  446. {access, [{allow, all}]},
  447. %% SSL certificate and key files
  448. {ssl, [{certfile, "etc/ssl/ssl.crt"},
  449. {keyfile, "etc/ssl/ssl.key"}]},
  450. %% Socket Options
  451. {sockopts, [
  452. {backlog, 1024}
  453. %{buffer, 4096},
  454. ]}
  455. ]}.
  456. 8083 - MQTT(WebSocket)
  457. ......................
  458. .. code-block:: erlang
  459. %% HTTP and WebSocket Listener
  460. {listener, http, 8083, [
  461. %% Size of acceptor pool
  462. {acceptors, 4},
  463. %% Maximum number of concurrent clients
  464. {max_clients, 64},
  465. %% Socket Access Control
  466. {access, [{allow, all}]},
  467. %% Socket Options
  468. {sockopts, [
  469. {backlog, 1024}
  470. %{buffer, 4096},
  471. ]}
  472. ]}.
  473. Erlang VM Monitor
  474. -----------------
  475. .. code:: erlang
  476. %% Long GC, don't monitor in production mode for:
  477. %% https://github.com/erlang/otp/blob/feb45017da36be78d4c5784d758ede619fa7bfd3/erts/emulator/beam/erl_gc.c#L421
  478. {sysmon_long_gc, false}.
  479. %% Long Schedule(ms)
  480. {sysmon_long_schedule, 240}.
  481. %% 8M words. 32MB on 32-bit VM, 64MB on 64-bit VM.
  482. %% 8 * 1024 * 1024
  483. {sysmon_large_heap, 8388608}.
  484. %% Busy Port
  485. {sysmon_busy_port, false}.
  486. %% Busy Dist Port
  487. {sysmon_busy_dist_port, true}.