performance.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. ==================
  2. Performance Tuning
  3. ==================
  4. [[ Note: latest version is found at https://wiki.lighttpd.net/Docs_Performance ]]
  5. [[ Note: see version with links at https://wiki.lighttpd.net/Docs_Performance ]]
  6. important performance tuning rules
  7. * Prefer lighttpd defaults unless you have a reason to change a setting,
  8. and unless you test that changing the setting is beneficial to you.
  9. * Proper functionality is more important than marginal increases in performance;
  10. a web server that does not function as intended is not useful.
  11. Do not sacrifice security or desired operational functioning for marginal
  12. performance improvements.
  13. * Performance tuning is not magic. The recommended approach is that one change
  14. be made at a time, that the change be tested and benchmarked, and that if the
  15. change does not have a measurable and positive impact in real-world scenarios,
  16. that the change be reverted.
  17. lighttpd is generally pretty snappy.
  18. Most of the following are micro-optimizations.
  19. No changes are required unless you have a specific performance issue that you
  20. must address.
  21. lighttpd configuration performance tuning (technical guidelines)
  22. -----------------------------------------
  23. * less is more (and is often simpler, too)
  24. - rely on defaults where possible to reduce unnecessary (duplicative) config
  25. processing (at runtime) to process configuration directives which were
  26. already set to the default values
  27. - set config options in the global scope rather than repeating in sub-scopes.
  28. lighttpd optimizes configuration settings in the global scope and makes
  29. those settings the defaults
  30. - TLS configuration can be set in the global scope and inherited by multiple
  31. $SERVER["socket"]
  32. ssl.pemfile = "..."
  33. ssl.privkey = "..."
  34. $SERVER["socket"] == ":443" { ssl.engine = "enable" }
  35. $SERVER["socket"] == "[::]:443" { ssl.engine = "enable" }
  36. - list only the modules actually used and enabled in server.modules;
  37. comment out the others
  38. - each loaded module registers itself into lighttpd hooks and gets a chance
  39. to handle each request, which is is unnecessary if a module is loaded but
  40. not otherwise configured to be used
  41. - server.compat-module-load = "disable" skips loading the default modules
  42. (mod_indexfile, mod_dirlisting, mod_staticfile), and you can then
  43. explicitly add one or more to server.modules to use them
  44. - tweaks to remove optional functionality
  45. - server.tag = "" skips sending "Server: lighttpd/1.4.xx" in responses;
  46. alternatively, use: server.tag = "lighttpd" to hide the lighttpd version
  47. - server.range-requests = "disable" can be used if all server responses are
  48. small files, but otherwise it is recommended to be left enabled
  49. - review the default lighttpd config provided by your distro
  50. - configs provided by distros aim to be newbie friendly but can introduce
  51. complexity of yet another config framework
  52. - configs provided by distros are often out-dated and then kept for historic
  53. compatibility, rather than current best practices
  54. - example: ~20 years ago some widely used versions of Adobe Acrobat reader
  55. plugin PDF clients misbehaved with range requests. Unfortunately, the
  56. config setting to disable range requests for PDFs has been cargo-culted
  57. into configs since then. Prefer to comment out or remove:
  58. $HTTP["url"] =~ "\.pdf$" { server.range-requests = "disable" }
  59. - server.max-connections limits the maximum number of simultaneous connections
  60. to handle and also affects memory usage for the connection cache
  61. - default is (about) 1365 which is oversized for all but the largest
  62. systems. Embedded systems might set server.max-connections = 16 or lower
  63. - server.max-worker = 0 should generally be left unset (or "0"), as
  64. CPU bottlenecks are usually elsewhere
  65. - server.follow-symlink = "enable" (default) should be left enabled. If such
  66. restrictions are required, prefer to run a separate lighttp instance under a
  67. separate user account, and enforce more restrictive file access permissions.
  68. - ssl.read-ahead = "disable" (default) is strongly recommended for slower,
  69. embedded systems which process TLS packets more slowly than network
  70. wire-speed. For faster systems, test if ssl.read-ahead = "enable" improves
  71. performance (or not)
  72. - prefer to configure mod_extforward extforward.hap-PROXY for lighttpd
  73. instances behind HAProxy or load balancers supporting the HAProxy PROXY
  74. protocol
  75. * minimize conditional processing (but not at the cost of proper functionality)
  76. - more conditions means more config processing at runtime
  77. - more conditions means more memory used by config per request
  78. - avoid repeating conditions and its opposite by joining them into if/else
  79. <condition> { ... } else { ... }
  80. <condition> { ... } else <condition> { ... } else { ... }
  81. - sometimes it may take fewer config lines to set a config option once in the
  82. global scope and then, where necessary, to unset the option in a small
  83. number of conditions rather than leaving the default in the global scope
  84. and enabling the config option in many more conditions
  85. - having no config conditions will be among the fastest configs to be
  86. processed, but config processing at runtime is fast and is not typically
  87. a bottleneck
  88. * dynamic backends (mod_proxy, mod_fastcgi, mod_scgi, mod_ajp13, ...)
  89. - prefer to use unix domain sockets (instead of TCP sockets) for connections
  90. from lighttpd to backends running on the same host
  91. - lighttpd can listen on a unix domain socket
  92. (server.bind = "/path/to/lighttpd.sock")
  93. and lighttpd mod_proxy can act as a reverse-proxy to a backend lighttpd
  94. server. Use with mod_extforward to preserve client remote address for the
  95. backend.
  96. * mod_fastcgi
  97. - Recommended: use PHP-FPM (FastCGI Process Manager),
  98. which is available as a package in many OS distros
  99. - If not using PHP-FPM, then see Docs_PerformanceFastCGI
  100. - lighttpd provides mechanisms for lighttpd to start up PHP backends, and
  101. that works well, but PHP-FPM is the modern and recommended mechanism to
  102. manage PHP backends
  103. * mod_rewrite and mod_redirect: short-circuiting
  104. (when using a sequence of regexes)
  105. - consider putting static file matches (passed through unmodified) first,
  106. and using a blank target to indicate no modification
  107. - consider using a blank match as a catch-all, rather than "^(.*)",
  108. which will still match all, but without the regex
  109. url.rewrite-once = (
  110. "^/static/|\.(?:css|jpg)$" => "",
  111. "" => "/index.php${url.path}${qsa}"
  112. )
  113. * mod_indexfile: reduce the number of entries in index-file.names,
  114. if mod_indexfile is enabled
  115. - index-file.names = ("index.html") as a list of one or two entries rather
  116. than a list of, say, 10 differenent file extensions
  117. * cache tuning
  118. - stat_cache: default server.stat_cache-engine = "simple" works well for
  119. typical usage and caches stat() results for 1-2 seconds. Test with
  120. server.stat-cache-engine = "inotify" or server.stat-cache-engine = "kqueue"
  121. for stat() results to be cached longer (16 seconds)
  122. - mod_auth: set auth.cache = ("max-age" => "600") to cache passwords (default
  123. disabled), but acknowledge changes to your security posture if enabling the
  124. cache. (since lighttpd 1.4.56)
  125. - mod_deflate: set deflate.cache-dir to cache (and reuse) compressed static
  126. assets based on ETag (since lighttpd 1.4.56)
  127. - mod_dirlisting: set dir-listing.cache = ( ... ) to configure caching of
  128. generated directory listings (since lighttpd 1.4.60)
  129. * do not sacrifice security to save a few CPU cycles
  130. - server.http-parseopts* option defaults are recommended, and are very fast
  131. - disabling server.http-parseopts* might save a few CPU cycles, but is an
  132. anti-pattern for secure configurations
  133. - server.http-parseopts* options should be modified only when the
  134. functionality needs to be tuned for proper site operation
  135. - ETag response headers are used in HTTP/1.1 conditional caching.
  136. ETag response headers are also required for mod_deflate and strongly
  137. recommended with mod_webdav. While lighttpd ETag generation for
  138. static content can be disabled for micro-benchmarking purposes,
  139. ETag generation (default enabled) is recommended for production use
  140. (etag.use-inode, etag.use-mtime, etag.use-size)
  141. * compile lighttpd with mmap support (./configure --enable-mmap) to improve
  142. mod_deflate performance
  143. lighttpd configuration for use of operating system (OS) features
  144. ----------------------------------------------------------------
  145. lighttpd generally chooses optimal defaults for the OS on which it is running.
  146. Prefer lighttpd defaults unless something is not functioning correctly.
  147. (Please report bugs and include your platform information if the lighttpd OS
  148. defaults are not working correctly.)
  149. * server.event-handler (e.g. epoll, kqueue, event ports, devpoll, poll, ...)
  150. * server.network-backend (e.g. sendfile, writev, write)
  151. lighttpd configuration tuning for high-traffic sites with a large number of connections
  152. ---------------------------------------------------------------------------------------
  153. * test with server.max-fds = 16384 (or higher) and OS system and/or per-user
  154. ulimit -Hn might need to be adjusted to allow this or higher values.
  155. For each 4k increase in server.max-fds, lighttpd uses an additional ~100 kb
  156. of memory for internal structures, not including memory used by each active
  157. connection. (In other words, there is a marginal cost for using very high
  158. values when there are not nearly so many simultaneous open connections).
  159. server.max-connections is calculated to be 1/3 of server.max-fds if
  160. server.max-connections is not configured.
  161. lighttpd configuration tuning for low-memory systems
  162. ----------------------------------------------------
  163. * test with server.max-fds = 128 (or lower)
  164. * test with server.max-connections = 16 (or lower)
  165. * test with server.listen-backlog = 16 (or lower)
  166. * (default) server.stat_cache-engine = "simple"
  167. * (default) ssl.read-ahead = "disable"
  168. * support for the HTTP/2 protocol (enabled by default in lighttpd 1.4.59) uses
  169. more memory than HTTP/1.1; low-memory systems might choose to disable HTTP/2
  170. protocol support: server.feature-flags += ("server.h2proto" => "disable")
  171. lighttpd configuration tuning for traffic shapping (download rate-limiting)
  172. --------------------------------------------------
  173. connection.kbytes-per-second
  174. server.kbytes-per-second
  175. lighttpd configuration tuning for timeouts
  176. ------------------------------------------
  177. To free up connections more quickly, tune down the idle timeouts for how long
  178. lighttpd waits to read or write to the client (when lighttpd is trying to read
  179. or write), or how long lighttpd waits for the next keep-alive request, and for
  180. how many keep-alive requests, before lighttpd closes the connection. A value
  181. of 0 disables an idle timeout and is not recommended.
  182. * server.max-read-idle = 60
  183. * server.max-write-idle = 360
  184. * server.max-keep-alive-idle = 5
  185. * server.max-keep-alive-requests = 100
  186. Generally, server.max-keep-alive-requests should not be set to 0 since setting
  187. up a new TCP connection takes more resources than keeping an open idle fd,
  188. especially if the connection is over TLS.
  189. Platform-Specific Notes
  190. =======================
  191. Note: The following is old and possibly out-dated.
  192. Please consider only as a starting point for further testing.
  193. Linux
  194. -----
  195. For Linux 2.4.x you should think about compiling lighttpd with the option
  196. ``--disable-lfs`` to disable the support for files larger than 2GB. lighttpd will
  197. fall back to the ``writev() + mmap()`` network calls which is ok, but not as
  198. fast as possible but support files larger than 2GB.
  199. Disabling the TCP options reduces the overhead of each TCP packet and might
  200. help to get the last few percent of performance out of the server. Be aware that
  201. disabling these options most likely decreases performance for high-latency and lossy
  202. links.
  203. - net.ipv4.tcp_sack = 0
  204. - net.ipv4.tcp_timestamps = 0
  205. Increasing the TCP send and receive buffers will increase the performance a
  206. lot if (and only if) you have a lot of large files to send.
  207. - net.ipv4.tcp_wmem = 4096 65536 524288
  208. - net.core.wmem_max = 1048576
  209. If you have a lot of large file uploads, increasing the receive buffers will help.
  210. - net.ipv4.tcp_rmem = 4096 87380 524288
  211. - net.core.rmem_max = 1048576
  212. Keep in mind that every TCP connection uses the configured amount of memory for socket
  213. buffers. If you've got many connections this can quickly drain the available memory.
  214. See http://www.acc.umu.se/~maswan/linux-netperf.txt for more information on these parameters.
  215. FreeBSD
  216. -------
  217. On FreeBSD you might gain some performance by enabling accept filters. Just
  218. compile your kernel with: ::
  219. options ACCEPT_FILTER_HTTP
  220. For more ideas about tuning FreeBSD read: tuning(7)
  221. Reducing the recvspace should always be ok if the server only handles HTTP
  222. requests without large uploads. Increasing the sendspace would reduce the
  223. system load if you have a lot of large files to be sent, but keep in mind that
  224. you have to provide the memory in the kernel for each connection. 1024 * 64KB
  225. would mean 64MB of kernel RAM. Keep this in mind.
  226. - net.inet.tcp.recvspace = 4096