config.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. .. _configuration:
  2. =============
  3. Configuration
  4. =============
  5. Configuration files of the broker are under 'etc/' folder, including:
  6. +-------------------+-----------------------------------+
  7. | File | Description |
  8. +-------------------+-----------------------------------+
  9. | etc/vm.args | Erlang VM Arguments |
  10. +-------------------+-----------------------------------+
  11. | etc/emqttd.config | emqttd broker Config |
  12. +-------------------+-----------------------------------+
  13. | etc/acl.config | ACL Config |
  14. +-------------------+-----------------------------------+
  15. | etc/clients.config| ClientId Authentication |
  16. +-------------------+-----------------------------------+
  17. | etc/rewrite.config| Rewrite Rules |
  18. +-------------------+-----------------------------------+
  19. | etc/ssl/* | SSL certificate and key files |
  20. +-------------------+-----------------------------------+
  21. -----------
  22. etc/vm.args
  23. -----------
  24. Configure and Optimize Erlang VM::
  25. ##-------------------------------------------------------------------------
  26. ## Name of the node
  27. ##-------------------------------------------------------------------------
  28. -name emqttd@127.0.0.1
  29. ## Cookie for distributed erlang
  30. -setcookie emqttdsecretcookie
  31. ##-------------------------------------------------------------------------
  32. ## Flags
  33. ##-------------------------------------------------------------------------
  34. ## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive
  35. ## (Disabled by default..use with caution!)
  36. ##-heart
  37. -smp true
  38. ## Enable kernel poll and a few async threads
  39. +K true
  40. ## 12 threads/core.
  41. +A 48
  42. ## max process numbers
  43. +P 8192
  44. ## Sets the maximum number of simultaneously existing ports for this system
  45. +Q 8192
  46. ## max atom number
  47. ## +t
  48. ## Set the distribution buffer busy limit (dist_buf_busy_limit) in kilobytes.
  49. ## Valid range is 1-2097151. Default is 1024.
  50. ## +zdbbl 8192
  51. ## CPU Schedulers
  52. ## +sbt db
  53. ##-------------------------------------------------------------------------
  54. ## Env
  55. ##-------------------------------------------------------------------------
  56. ## Increase number of concurrent ports/sockets, deprecated in R17
  57. -env ERL_MAX_PORTS 8192
  58. -env ERTS_MAX_PORTS 8192
  59. -env ERL_MAX_ETS_TABLES 1024
  60. ## Tweak GC to run more often
  61. -env ERL_FULLSWEEP_AFTER 1000
  62. The two most important parameters in etc/vm.args:
  63. +-------+---------------------------------------------------------------------------+
  64. | +P | Max number of Erlang proccesses. A MQTT client consumes two proccesses. |
  65. | | The value should be larger than max_clients * 2 |
  66. +-------+---------------------------------------------------------------------------+
  67. | +Q | Max number of Erlang Ports. A MQTT client consumes one port. |
  68. | | The value should be larger than max_clients. |
  69. +-------+---------------------------------------------------------------------------+
  70. The name and cookie of Erlang Node should be configured when clustering::
  71. -name emqttd@host_or_ip
  72. ## Cookie for distributed erlang
  73. -setcookie emqttdsecretcookie
  74. -----------------
  75. etc/emqttd.config
  76. -----------------
  77. This is the main emqttd broker configuration file.
  78. File Syntax
  79. -----------
  80. The file users the standard Erlang config syntax, consists of a list of erlang applications and their environments.
  81. .. code-block:: erlang
  82. [{kernel, [
  83. {start_timer, true},
  84. {start_pg2, true}
  85. ]},
  86. {sasl, [
  87. {sasl_error_logger, {file, "log/emqttd_sasl.log"}}
  88. ]},
  89. ...
  90. {emqttd, [
  91. ...
  92. ]}
  93. ].
  94. The file adopts Erlang Term Syntax:
  95. 1. [ ]: List, seperated by comma
  96. 2. { }: Tuple, Usually {Env, Value}
  97. 3. % : comment
  98. Log Level and File
  99. ------------------
  100. Logger of emqttd broker is implemented by 'lager' application:
  101. .. code-block:: erlang
  102. {lager, [
  103. ...
  104. ]},
  105. Configure log handlers:
  106. .. code-block:: erlang
  107. {handlers, [
  108. {lager_console_backend, info},
  109. {lager_file_backend, [
  110. {formatter_config, [time, " ", pid, " [",severity,"] ", message, "\n"]},
  111. {file, "log/emqttd_info.log"},
  112. {level, info},
  113. {size, 104857600},
  114. {date, "$D0"},
  115. {count, 30}
  116. ]},
  117. {lager_file_backend, [
  118. {formatter_config, [time, " ", pid, " [",severity,"] ", message, "\n"]},
  119. {file, "log/emqttd_error.log"},
  120. {level, error},
  121. {size, 104857600},
  122. {date, "$D0"},
  123. {count, 30}
  124. ]}
  125. ]}
  126. emqttd Application
  127. ------------------
  128. The MQTT broker is implemented by erlang 'emqttd' application:
  129. .. code-block:: erlang
  130. {emqttd, [
  131. %% Authentication and Authorization
  132. {access, [
  133. ...
  134. ]},
  135. %% MQTT Protocol Options
  136. {mqtt, [
  137. ...
  138. ]},
  139. %% Broker Options
  140. {broker, [
  141. ...
  142. ]},
  143. %% Modules
  144. {modules, [
  145. ...
  146. ]},
  147. %% Plugins
  148. {plugins, [
  149. ...
  150. ]},
  151. %% Listeners
  152. {listeners, [
  153. ...
  154. ]},
  155. %% Erlang System Monitor
  156. {sysmon, [
  157. ]}
  158. ]}
  159. Pluggable Authentication
  160. ------------------------
  161. The emqttd broker supports pluggable authentication mechanism with a list of modules and plugins.
  162. The broker provides Username, ClientId, LDAP and anonymous authentication modules by default:
  163. .. code-block:: erlang
  164. %% Authetication. Anonymous Default
  165. {auth, [
  166. %% Authentication with username, password
  167. %% Add users: ./bin/emqttd_ctl users add Username Password
  168. %% {username, [{"test", "public"}]},
  169. %% Authentication with clientid
  170. % {clientid, [{password, no}, {file, "etc/clients.config"}]},
  171. %% Authentication with LDAP
  172. % {ldap, [
  173. % {servers, ["localhost"]},
  174. % {port, 389},
  175. % {timeout, 30},
  176. % {user_dn, "uid=$u,ou=People,dc=example,dc=com"},
  177. % {ssl, fasle},
  178. % {sslopts, [
  179. % {"certfile", "ssl.crt"},
  180. % {"keyfile", "ssl.key"}]}
  181. % ]},
  182. %% Allow all
  183. {anonymous, []}
  184. ]},
  185. The modules enabled at the same time compose an authentication chain::
  186. ---------------- ---------------- -------------
  187. Client --> | Username | -ignore-> | ClientID | -ignore-> | Anonymous |
  188. ---------------- ---------------- -------------
  189. | | |
  190. \|/ \|/ \|/
  191. allow | deny allow | deny allow | deny
  192. .. NOTE:: There are also MySQL、PostgreSQL、Redis、MongoDB Authentication Plugins.
  193. Username Authentication
  194. .......................
  195. .. code-block:: erlang
  196. {username, [{client1, "passwd1"}, {client2, "passwd2"}]},
  197. Two ways to configure users:
  198. 1. Configure username and plain password directly::
  199. {username, [{client1, "passwd1"}, {client2, "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. {clientid, [{password, no}, {file, "etc/clients.config"}]},
  206. Configure ClientIDs in etc/clients.config::
  207. testclientid0
  208. testclientid1 127.0.0.1
  209. testclientid2 192.168.0.1/24
  210. LDAP Authentication
  211. ...................
  212. .. code-block:: erlang
  213. {ldap, [
  214. {servers, ["localhost"]},
  215. {port, 389},
  216. {timeout, 30},
  217. {user_dn, "uid=$u,ou=People,dc=example,dc=com"},
  218. {ssl, fasle},
  219. {sslopts, [
  220. {"certfile", "ssl.crt"},
  221. {"keyfile", "ssl.key"}]}
  222. ]},
  223. Anonymous Authentication
  224. ........................
  225. Allow any client to connect to the broker::
  226. {anonymous, []}
  227. ACL
  228. ---
  229. Enable the default ACL module:
  230. .. code-block:: erlang
  231. {acl, [
  232. %% Internal ACL module
  233. {internal, [{file, "etc/acl.config"}, {nomatch, allow}]}
  234. ]}
  235. MQTT Packet and ClientID
  236. ------------------------
  237. .. code-block:: erlang
  238. {packet, [
  239. %% Max ClientId Length Allowed
  240. {max_clientid_len, 1024},
  241. %% Max Packet Size Allowed, 64K default
  242. {max_packet_size, 65536}
  243. ]},
  244. MQTT Client Idle Timeout
  245. ------------------------
  246. .. code-block:: erlang
  247. {client, [
  248. %% Socket is connected, but no 'CONNECT' packet received
  249. {idle_timeout, 10}
  250. ]},
  251. MQTT Session
  252. ------------
  253. .. code-block:: erlang
  254. {session, [
  255. %% Max number of QoS 1 and 2 messages that can be “in flight” at one time.
  256. %% 0 means no limit
  257. {max_inflight, 100},
  258. %% Retry interval for unacked QoS1/2 messages.
  259. {unack_retry_interval, 20},
  260. %% Awaiting PUBREL Timeout
  261. {await_rel_timeout, 20},
  262. %% Max Packets that Awaiting PUBREL, 0 means no limit
  263. {max_awaiting_rel, 0},
  264. %% Interval of Statistics Collection(seconds)
  265. {collect_interval, 20},
  266. %% Expired after 2 days
  267. {expired_after, 48}
  268. ]},
  269. Session parameters:
  270. +----------------------+----------------------------------------------------------+
  271. | max_inflight | Max number of QoS1/2 messages that can be delivered in |
  272. | | the same time |
  273. +----------------------+----------------------------------------------------------+
  274. | unack_retry_interval | Retry interval for unacked QoS1/2 messages. |
  275. +----------------------+----------------------------------------------------------+
  276. | await_rel_timeout | Awaiting PUBREL Timeout |
  277. +----------------------+----------------------------------------------------------+
  278. | max_awaiting_rel | Max number of Packets that Awaiting PUBREL |
  279. +----------------------+----------------------------------------------------------+
  280. | collect_interval | Interval of Statistics Collection |
  281. +----------------------+----------------------------------------------------------+
  282. | expired_after | Expired after |
  283. +----------------------+----------------------------------------------------------+
  284. MQTT Message Queue
  285. ------------------
  286. The message queue of session stores:
  287. 1. Offline messages for persistent session.
  288. 2. Pending messages for inflight window is full
  289. Queue parameters:
  290. .. code-block:: erlang
  291. {queue, [
  292. %% simple | priority
  293. {type, simple},
  294. %% Topic Priority: 0~255, Default is 0
  295. %% {priority, [{"topic/1", 10}, {"topic/2", 8}]},
  296. %% Max queue length. Enqueued messages when persistent client disconnected,
  297. %% or inflight window is full.
  298. {max_length, infinity},
  299. %% Low-water mark of queued messages
  300. {low_watermark, 0.2},
  301. %% High-water mark of queued messages
  302. {high_watermark, 0.6},
  303. %% Queue Qos0 messages?
  304. {queue_qos0, true}
  305. ]}
  306. +----------------------+---------------------------------------------------+
  307. | type | Queue type: simple or priority |
  308. +----------------------+---------------------------------------------------+
  309. | priority | Topic priority |
  310. +----------------------+---------------------------------------------------+
  311. | max_length | Max Queue size, infinity means no limit |
  312. +----------------------+---------------------------------------------------+
  313. | low_watermark | Low watermark |
  314. +----------------------+---------------------------------------------------+
  315. | high_watermark | High watermark |
  316. +----------------------+---------------------------------------------------+
  317. | queue_qos0 | If Qos0 message queued? |
  318. +----------------------+---------------------------------------------------+
  319. Sys Interval of Broker
  320. -----------------------
  321. .. code-block:: erlang
  322. %% System interval of publishing $SYS messages
  323. {sys_interval, 60},
  324. Retained messages
  325. -----------------
  326. .. code-block:: erlang
  327. {retained, [
  328. %% Expired after seconds, never expired if 0
  329. {expired_after, 0},
  330. %% Maximum number of retained messages
  331. {max_message_num, 100000},
  332. %% Max Payload Size of retained message
  333. {max_playload_size, 65536}
  334. ]},
  335. PubSub and Router
  336. -----------------
  337. .. code-block:: erlang
  338. {pubsub, [
  339. %% PubSub Pool
  340. {pool_size, 8},
  341. %% Subscription: true | false
  342. {subscription, true},
  343. %% Route aging time(seconds)
  344. {route_aging, 5}
  345. ]},
  346. Bridge Parameters
  347. -----------------
  348. .. code-block:: erlang
  349. {bridge, [
  350. %% Bridge Queue Size
  351. {max_queue_len, 10000},
  352. %% Ping Interval of bridge node
  353. {ping_down_interval, 1}
  354. ]}
  355. Enable Modules
  356. --------------
  357. 'presence' module will publish presence message to $SYS topic when a client connected or disconnected::
  358. {presence, [{qos, 0}]},
  359. 'subscription' module forces the client to subscribe some topics when connected to the broker:
  360. .. code-block:: erlang
  361. %% Subscribe topics automatically when client connected
  362. {subscription, [
  363. %% Subscription from stored table
  364. stored,
  365. %% $u will be replaced with username
  366. {"$Q/username/$u", 1},
  367. %% $c will be replaced with clientid
  368. {"$Q/client/$c", 1}
  369. ]}
  370. 'rewrite' module supports to rewrite the topic path:
  371. .. code-block:: erlang
  372. %% Rewrite rules
  373. {rewrite, [{file, "etc/rewrite.config"}]}
  374. Plugins Folder
  375. --------------
  376. .. code-block:: erlang
  377. {plugins, [
  378. %% Plugin App Library Dir
  379. {plugins_dir, "./plugins"},
  380. %% File to store loaded plugin names.
  381. {loaded_file, "./data/loaded_plugins"}
  382. ]},
  383. TCP Listeners
  384. -------------
  385. Congfigure the TCP listeners for MQTT, MQTT(SSL) and HTTP Protocols.
  386. The most important parameter is 'max_clients' - max concurrent clients allowed.
  387. The TCP Ports occupied by emqttd broker by default:
  388. +-----------+-----------------------------------+
  389. | 1883 | MQTT Port |
  390. +-----------+-----------------------------------+
  391. | 8883 | MQTT(SSL) Port |
  392. +-----------+-----------------------------------+
  393. | 8083 | MQTT(WebSocket), HTTP API Port |
  394. +-----------+-----------------------------------+
  395. .. code-block:: erlang
  396. {listeners, [
  397. {mqtt, 1883, [
  398. %% Size of acceptor pool
  399. {acceptors, 16},
  400. %% Maximum number of concurrent clients
  401. {max_clients, 8192},
  402. %% Socket Access Control
  403. {access, [{allow, all}]},
  404. %% Connection Options
  405. {connopts, [
  406. %% Rate Limit. Format is 'burst, rate', Unit is KB/Sec
  407. %% {rate_limit, "100,10"} %% 100K burst, 10K rate
  408. ]},
  409. %% Socket Options
  410. {sockopts, [
  411. %Set buffer if hight thoughtput
  412. %{recbuf, 4096},
  413. %{sndbuf, 4096},
  414. %{buffer, 4096},
  415. %{nodelay, true},
  416. {backlog, 1024}
  417. ]}
  418. ]},
  419. {mqtts, 8883, [
  420. %% Size of acceptor pool
  421. {acceptors, 4},
  422. %% Maximum number of concurrent clients
  423. {max_clients, 512},
  424. %% Socket Access Control
  425. {access, [{allow, all}]},
  426. %% SSL certificate and key files
  427. {ssl, [{certfile, "etc/ssl/ssl.crt"},
  428. {keyfile, "etc/ssl/ssl.key"}]},
  429. %% Socket Options
  430. {sockopts, [
  431. {backlog, 1024}
  432. %{buffer, 4096},
  433. ]}
  434. ]},
  435. %% WebSocket over HTTPS Listener
  436. %% {https, 8083, [
  437. %% %% Size of acceptor pool
  438. %% {acceptors, 4},
  439. %% %% Maximum number of concurrent clients
  440. %% {max_clients, 512},
  441. %% %% Socket Access Control
  442. %% {access, [{allow, all}]},
  443. %% %% SSL certificate and key files
  444. %% {ssl, [{certfile, "etc/ssl/ssl.crt"},
  445. %% {keyfile, "etc/ssl/ssl.key"}]},
  446. %% %% Socket Options
  447. %% {sockopts, [
  448. %% %{buffer, 4096},
  449. %% {backlog, 1024}
  450. %% ]}
  451. %%]},
  452. %% HTTP and WebSocket Listener
  453. {http, 8083, [
  454. %% Size of acceptor pool
  455. {acceptors, 4},
  456. %% Maximum number of concurrent clients
  457. {max_clients, 64},
  458. %% Socket Access Control
  459. {access, [{allow, all}]},
  460. %% Socket Options
  461. {sockopts, [
  462. {backlog, 1024}
  463. %{buffer, 4096},
  464. ]}
  465. ]}
  466. ]},
  467. Listener Parameters:
  468. +-------------+----------------------------------------------------------------+
  469. | acceptors | TCP Acceptor Pool |
  470. +-------------+----------------------------------------------------------------+
  471. | max_clients | Maximum number of concurrent TCP connections allowed |
  472. +-------------+----------------------------------------------------------------+
  473. | access | Access Control by IP, for example: [{allow, "192.168.1.0/24"}] |
  474. +-------------+----------------------------------------------------------------+
  475. | connopts | Rate Limit Control, for example: {rate_limit, "100,10"} |
  476. +-------------+----------------------------------------------------------------+
  477. | sockopts | TCP Socket parameters |
  478. +-------------+----------------------------------------------------------------+
  479. .. _config_acl:
  480. --------------
  481. etc/acl.config
  482. --------------
  483. The 'etc/acl.config' is the default ACL config for emqttd broker. The rules by default:
  484. .. code-block:: erlang
  485. %% Allow 'dashboard' to subscribe '$SYS/#'
  486. {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
  487. %% Allow clients from localhost to subscribe any topics
  488. {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
  489. %% Deny clients to subscribe '$SYS#' and '#'
  490. {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
  491. %% Allow all by default
  492. {allow, all}.
  493. An ACL rule is an Erlang tuple. The Access control module of emqttd broker matches the rule one by one from top to bottom::
  494. --------- --------- ---------
  495. Client -> | Rule1 | --nomatch--> | Rule2 | --nomatch--> | Rule3 | --> Default
  496. --------- --------- ---------
  497. | | |
  498. match match match
  499. \|/ \|/ \|/
  500. allow | deny allow | deny allow | deny
  501. .. _config_rewrite:
  502. ------------------
  503. etc/clients.config
  504. ------------------
  505. Enable ClientId Authentication in 'etc/emqttd.config':
  506. .. code-block:: erlang
  507. {auth, [
  508. %% Authentication with clientid
  509. {clientid, [{password, no}, {file, "etc/clients.config"}]}
  510. ]},
  511. Configure all allowed ClientIDs, IP Addresses in etc/clients.config::
  512. testclientid0
  513. testclientid1 127.0.0.1
  514. testclientid2 192.168.0.1/24
  515. ------------------
  516. etc/rewrite.config
  517. ------------------
  518. The Rewrite Rules for emqttd_mod_rewrite:
  519. .. code-block:: erlang
  520. {topic, "x/#", [
  521. {rewrite, "^x/y/(.+)$", "z/y/$1"},
  522. {rewrite, "^x/(.+)$", "y/$1"}
  523. ]}.
  524. {topic, "y/+/z/#", [
  525. {rewrite, "^y/(.+)/z/(.+)$", "y/z/$2"}
  526. ]}.