config.rst 20 KB

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