PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/content/2013-04-12-cherokee-to-nginx-technical.rst

https://github.com/davidjb/davidjb.com
ReStructuredText | 221 lines | 183 code | 38 blank | 0 comment | 0 complexity | 73aa32ebe1ff2fd66a2deb1476ea72da MD5 | raw file
  1. Switching to Nginx from Cherokee: Techincal Guide
  2. #################################################
  3. :category: Web
  4. :tags: Nginx, Cherokee, web, web servers
  5. This is a follow up to a previous post on
  6. `Switching to Nginx from Cherokee <|filename|2013-04-11-cherokee-to-nginx.rst>`_. Read that in case you're here and haven't already.
  7. All information here on a server level is related to RHEL 6. You will
  8. need to change some instructions for Debian based systems. CentOS
  9. should be fine and needs only a minor URL change in the repo configuration.
  10. #. Edit ``/etc/yum.repos.d/nginx.repo`` and add this::
  11. [nginx]
  12. name=nginx repo
  13. baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
  14. gpgcheck=0
  15. enabled=1
  16. #. Run ``yum install nginx`` to install Nginx.
  17. #. Start rebuilding your configuration files in Ngnix format. I achieved
  18. this by starting ``cherokee-admin`` (the Cherokee GUI interface)
  19. and stepping through the configuration one server, and one behaviour
  20. at a time until I was finished. Refer to `Configuration changeover`_
  21. below for helpful hints and configuration matching.
  22. This will take you some time. It took me 2 days for about 15 servers,
  23. which isn't bad considering! I think it's a testament to how similar
  24. the configuration between the two happens to be.
  25. #. Stop Cherokee and start Ngnix once your configuration is ready:
  26. .. code:: bash
  27. service cherokee stop
  28. service nginx start
  29. Configuration changeover
  30. ~~~~~~~~~~~~~~~~~~~~~~~~
  31. Note that the following is a rough mapping of what you can use to convert
  32. Cherokee configuration to Ngnix. In the interests of not reproducing
  33. information that's already on Ngnix's site, I've just linked to their
  34. wiki. Keep in mind this list isn't complete as it's just what I used for
  35. converting my configuration. Your requirements may be more complex.
  36. The good news is that most things have a counter-part in both applications.
  37. It's worth remembering that a lot of these aspects, whilst locking in place
  38. within Cherokee, don't have to be configured at the same *level* within
  39. Ngnix. For instance, whils Cherokee requires you to specify ports to
  40. listen to at the server-configuration level (top), Nginx applies this
  41. at a server_ level.
  42. Also keep in mind that everything in Ngnix is inherited based upon the
  43. nesting of the various http_, server_, location_ blocks within the
  44. configuration. This is particularly nice and helps reduce duplication
  45. that you might have had originally with Cherokee. So, if you find yourself
  46. doing something like ``gzip on`` within Ngnix everywhere, maybe just
  47. enable it server_-wide or http_-wide at that level, and just use
  48. ``gzip off`` wherever you need it turned off. Easy.
  49. Some things like
  50. listen_ might seem a little more verbose to start, since you may find
  51. yourself repeating it a few times, but once you realise how it all works,
  52. it's a lot more flexible across multiple host names or IP addresses.
  53. If you've got more to add to this, feel free to fork my blog on GitHub
  54. and submit a pull request.
  55. Server-level
  56. ~~~~~~~~~~~~
  57. =============== ===================================================
  58. Cherokee option Nginx option
  59. =============== ===================================================
  60. Ports to listen listen_
  61. Timeout client_body_timeout_, client_header_timeout_,
  62. other \*_connect_timeout or \*_read_timeout options
  63. Server Tokens server_tokens_
  64. User user_
  65. Mime Types Refer to ``/etc/ngnix/mime.types``
  66. =============== ===================================================
  67. vServer-level
  68. ~~~~~~~~~~~~~
  69. ======================= =========================================
  70. Virtual server option Nginx option
  71. ======================= =========================================
  72. Virtual Server server_ block
  73. Virtual Server nickname server_name_
  74. Document Root root_
  75. Directory Indexes index_
  76. Match nickname server_name_
  77. Virtual Server matching server_name_ (Use ``\~`` to specify regex)
  78. Error logging error_log_
  79. Access Logging access_log_
  80. SSL Certificate ssl_certificate_
  81. SSL Certificate key ssl_certificate_key_
  82. ======================= =========================================
  83. Behaviour-level
  84. ~~~~~~~~~~~~~~~
  85. ================ ===================================================
  86. Behaviour option Nginx option
  87. ================ ===================================================
  88. Behaviour location_ block, if_ condition
  89. Matching rule Depends. location_ is a good start for matching URI
  90. strings, regex, directory names and paths. Watch
  91. out for if_ statements within location_.
  92. * Directory: location_
  93. * Extensions: location_
  94. * Regular Expression: location_
  95. * Header: if_ (in server_ or location_)
  96. * Exists: try_files_ or if_
  97. * HTTP Method: if_ against $request_method_
  98. * Incoming IP/Port: None. Use correct server_
  99. block
  100. * SSL/TLS: Use specific server_ block (fastest),
  101. or if_ against $server_port_
  102. * Full Path: location_
  103. * Connected from: if_ against $remote_addr_
  104. * URL Argument: location_ or if_ against $args_
  105. * GeoIP: See geo_ module
  106. Try and simplify rules down. Odds are your rules
  107. aren't complicated and don't need to be. Avoid
  108. unnecessary regular expressions and use strings
  109. only if possible. Note that location_ blocks in
  110. Ngnix will match on initial substrings by default.
  111. Many things that you might have had to use
  112. conditional *if*, *and*, *not* and *or* in Cherokee
  113. can be done either by using regex instead or using
  114. built-in logic in Ngnix. For instance, use a
  115. dedicated server_ blocks to handle HTTP and HTTPS
  116. differently and avoid needing tonnes of if_
  117. statements.
  118. Handler Configuration (typically) within location_.
  119. May move to server_ block if appropriate.
  120. * None: ``{}`` (empty brackets)
  121. * List & Send: alias_ or root_
  122. * Static Content: alias_ or root_
  123. * Only Listing: Unknown.
  124. * Redirection: return_ or rewrite_
  125. * FastCGI: fastcgi_pass_ and fastcgi options
  126. * SCGI: scgi_pass_ and scgi options
  127. * uWSGI: uwsgi_pass_ and uwsgi options
  128. * HTTP Reverse Proxy: proxy_pass and proxy options
  129. * Upload Reporting: Unknown
  130. * CGI: Not directly supported. Use uWSGI to wrap.
  131. * Server Side Includes: ssi_
  132. * Hidden Downloads: See 3rd party modules.
  133. * Server Info: Unknown.
  134. * MySQL Bridge: Not applicable.
  135. * HTTP Error: return_
  136. * Remote Administration: Not applicable.
  137. * 1x1 Transparent GIF: empty_gif_
  138. * Drop Connection: ``return 444`` (special code)
  139. Transforms gzip_, gzip_comp_level_ (more options here!)
  140. Caching expires_, various ``_cache`` directives
  141. Security allow_, deny_
  142. Restrictions client_body_timeout_, client_header_timeout_,
  143. other \*_connect_timeout or \*_read_timeout options
  144. ================ ===================================================
  145. That's about it. It's mostly a manual process to step through the
  146. configuration and sort each rule out. It's not quick so bring a cut lunch.
  147. The good news though is that most things map almost exactly. Even regex.
  148. .. _$request_method: http://wiki.nginx.org/HttpCoreModule#Variables
  149. .. _$args: http://wiki.nginx.org/HttpCoreModule#Variables
  150. .. _$remote_addr: http://wiki.nginx.org/HttpCoreModule#Variables
  151. .. _$server_port: http://wiki.nginx.org/HttpCoreModule#Variables
  152. .. _allow: http://wiki.nginx.org/HttpAccessModule#allow
  153. .. _deny: http://wiki.nginx.org/HttpAccessModule#deny
  154. .. _expires: http://wiki.nginx.org/HttpHeadersModule#expires
  155. .. _gzip: http://wiki.nginx.org/HttpGzipModule#gzip
  156. .. _gzip_comp_level: http://wiki.nginx.org/HttpGzipModule#gzip_comp_level
  157. .. _ssi: http://wiki.nginx.org/HttpSsiModule#ssi
  158. .. _empty_gif: http://wiki.nginx.org/HttpEmptyGifModule#empty_gif
  159. .. _geo: http://wiki.nginx.org/HttpGeoModule
  160. .. _alias: http://wiki.nginx.org/HttpCoreModule#alias
  161. .. _return: http://wiki.nginx.org/HttpRewriteModule#return
  162. .. _rewrite: http://wiki.nginx.org/HttpRewriteModule#rewrite
  163. .. _fastcgi_pass: http://wiki.nginx.org/HttpFcgiModule#fastcgi_pass
  164. .. _scgi_pass: http://wiki.nginx.org/HttpScgiModule#scgi_pass
  165. .. _uwsgi_pass: http://wiki.nginx.org/HttpUwsgiModule#uwsgi_pass
  166. .. _try_files: http://wiki.nginx.org/HttpCoreModule#try_files
  167. .. _http: http://wiki.nginx.org/NginxHttpCoreModule#http
  168. .. _server: http://wiki.nginx.org/HttpCoreModule#server
  169. .. _if: http://wiki.nginx.org/HttpRewriteModule#if
  170. .. _location: http://wiki.nginx.org/HttpCoreModule#location
  171. .. _ssi: http://wiki.nginx.org/HttpSsiModule#ssi
  172. .. _ssl_certificate: http://wiki.nginx.org/HttpSslModule#ssl_certificate
  173. .. _ssl_certificate_key: http://wiki.nginx.org/HttpSslModule#ssl_certificate_key
  174. .. _user: http://wiki.nginx.org/CoreModule#user
  175. .. _listen: http://wiki.nginx.org/HttpCoreModule#listen
  176. .. _access_log: http://wiki.nginx.org/HttpLogModule#access_log
  177. .. _error_log: http://wiki.nginx.org/NginxCoreModule#error_log
  178. .. _server_name: http://wiki.nginx.org/HttpCoreModule#server_name
  179. .. _root: http://wiki.nginx.org/HttpCoreModule#root
  180. .. _index: http://wiki.nginx.org/HttpCoreModule#index
  181. .. _server_tokens: http://wiki.nginx.org/HttpCoreModule#server_tokens
  182. .. _client_body_timeout: http://wiki.nginx.org/HttpCoreModule#client_body_timeout
  183. .. _client_header_timeout: http://wiki.nginx.org/HttpCoreModule#client_header_timeout