config.rst 19 KB

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