├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md └── data └── opt └── container ├── script ├── renew.sh └── run-nginx.sh └── template ├── common.conf.template ├── nginx.conf.template ├── server.conf.template └── www_redirect.conf.template /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.5 2 | MAINTAINER HandcraftedBits 3 | 4 | ARG VERSION_NGINX=1.10.3 5 | ARG VERSION_NGINX_HEADERS_MORE=0.32 6 | 7 | ENV NGINX_GZIP on 8 | ENV NGINX_HEADERS_REMOVE Server,X-Powered-By 9 | ENV NGINX_KEEPALIVE_TIMEOUT 65 10 | ENV NGINX_PROXY_READ_TIMEOUT 120s 11 | ENV NGINX_RESOLVER 8.8.8.8 8.8.4.4 12 | ENV NGINX_TYPES_HASH_MAX_SIZE 2048 13 | ENV NGINX_UNIT_WAIT 2 14 | ENV NGINX_WORKER_PROCESSES auto 15 | ENV NGINX_WORKER_CONNECTIONS 768 16 | 17 | COPY data / 18 | 19 | RUN apk update && \ 20 | apk add bash build-base certbot gd gd-dev geoip geoip-dev git libxml2 libxslt libxml2-dev libxslt-dev netcat-openbsd \ 21 | openssl openssl-dev pcre pcre-dev perl perl-dev wget zlib zlib-dev && \ 22 | 23 | cd /tmp && \ 24 | wget https://github.com/openresty/headers-more-nginx-module/archive/v${VERSION_NGINX_HEADERS_MORE}.tar.gz && \ 25 | tar -xzvf v${VERSION_NGINX_HEADERS_MORE}.tar.gz && \ 26 | wget http://nginx.org/download/nginx-${VERSION_NGINX}.tar.gz && \ 27 | tar -xzvf nginx-${VERSION_NGINX}.tar.gz && \ 28 | cd nginx-${VERSION_NGINX} && \ 29 | ./configure --conf-path=/etc/nginx/nginx.conf \ 30 | --error-log-path=/var/log/nginx/error.log \ 31 | --http-client-body-temp-path=/var/lib/nginx/body \ 32 | --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ 33 | --http-log-path=/var/log/nginx/access.log \ 34 | --http-proxy-temp-path=/var/lib/nginx/proxy \ 35 | --http-scgi-temp-path=/var/lib/nginx/scgi \ 36 | --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \ 37 | --lock-path=/var/lock/nginx.lock \ 38 | --pid-path=/run/nginx.pid \ 39 | --prefix=/usr/share/nginx \ 40 | --with-http_addition_module \ 41 | --with-http_auth_request_module \ 42 | --with-http_dav_module \ 43 | --with-http_degradation_module \ 44 | --with-http_flv_module \ 45 | --with-http_geoip_module \ 46 | --with-http_gunzip_module \ 47 | --with-http_gzip_static_module \ 48 | --with-http_image_filter_module \ 49 | --with-http_mp4_module \ 50 | --with-http_perl_module \ 51 | --with-http_random_index_module \ 52 | --with-http_realip_module \ 53 | --with-http_secure_link_module \ 54 | --with-http_slice_module \ 55 | --with-http_ssl_module \ 56 | --with-http_stub_status_module \ 57 | --with-http_sub_module \ 58 | --with-http_v2_module \ 59 | --with-http_xslt_module \ 60 | --with-ipv6 \ 61 | --with-mail \ 62 | --with-mail_ssl_module \ 63 | --with-pcre-jit \ 64 | --with-stream \ 65 | --with-stream_ssl_module \ 66 | --with-threads \ 67 | --add-module=/tmp/headers-more-nginx-module-${VERSION_NGINX_HEADERS_MORE} && \ 68 | make install && \ 69 | cd / && \ 70 | rm -rf /tmp/* && \ 71 | 72 | wget -O /etc/ssl/certs/chain.letsencrypt.pem https://letsencrypt.org/certs/lets-encrypt-x1-cross-signed.pem && \ 73 | 74 | ln -sf /dev/stdout /var/log/nginx/access.log && \ 75 | ln -sf /dev/stderr /var/log/nginx/error.log && \ 76 | mkdir -p /var/lib/nginx && \ 77 | mkdir -p /var/log/letsencrypt && \ 78 | chmod +x /opt/container/script/renew.sh && \ 79 | echo "0 0 * * * /opt/container/script/renew.sh" | crontab - && \ 80 | 81 | apk del build-base gd-dev geoip-dev git libxml2-dev libxslt-dev openssl-dev pcre-dev perl-dev wget zlib-dev 82 | 83 | EXPOSE 80 443 84 | 85 | CMD [ "/bin/bash", "/opt/container/script/run-nginx.sh" ] 86 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NGINX Host [![Docker Pulls](https://img.shields.io/docker/pulls/handcraftedbits/nginx-host.svg?maxAge=2592000)](https://hub.docker.com/r/handcraftedbits/nginx-host) 2 | 3 | A [Docker](https://www.docker.com) container used to easily create a secure [NGINX](http://nginx.org) server that is 4 | capable of hosting one or more Docker-based "units" of functionality, such as static content or web applications. 5 | 6 | # Features 7 | 8 | * NGINX 1.10.3 9 | * Designed to make creating an HTTPS server simple -- simply pick the parts you need. 10 | * Default SSL settings score an **A+** grade on [SSL Labs](https://www.ssllabs.com/ssltest/) when including custom 11 | [Diffie-Hellman parameters](https://scotthelme.co.uk/squeezing-a-little-more-out-of-your-qualys-score/). 12 | * Designed to be used with [Let's Encrypt](https://letsencrypt.org) certificates. 13 | * Certificates are automatically renewed. 14 | * Default header settings score a **B** grade on [securityheaders.io](https://securityheaders.io). 15 | * Score can be improved with the addition of 16 | [Content Security Policy](https://www.owasp.org/index.php/Content_Security_Policy) headers and 17 | [HTTP Public Key Pinning](https://developer.mozilla.org/en-US/docs/Web/Security/Public_Key_Pinning). 18 | 19 | # Available Units 20 | 21 | The following units are available -- simply pick and choose which ones you want to sit behind your NGINX server: 22 | 23 | | Unit | Description | 24 | | ------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 25 | | [bamboo](https://github.com/handcraftedbits/docker-nginx-unit-bamboo) | The [Atlassian Bamboo](https://www.atlassian.com/software/bamboo) continuous integration server. | 26 | | [bitbucket](https://github.com/handcraftedbits/docker-nginx-unit-bitbucket) | The [Atlassian Bitbucket Server](https://www.atlassian.com/software/bitbucket/server) collaborative Git server. | 27 | | [confluence](https://github.com/handcraftedbits/docker-nginx-unit-confluence) | The [Atlassian Confluence](https://www.atlassian.com/software/confluence) team collaboration server. | 28 | | [go-import-redirector](https://github.com/handcraftedbits/docker-nginx-unit-go-import-redirector) | A unit based off of [rsc/go-import-redirector](https://github.com/rsc/go-import-redirector), which simplifies the hosting of [Go](https://golang.org) [custom remote import paths](https://golang.org/cmd/go/#hdr-Remote_import_paths). | 29 | | [hugo](https://github.com/handcraftedbits/docker-nginx-unit-hugo) | The [Hugo](https://gohugo.io) static site generator, designed for sites whose source code is hosted on GitHub. Includes the ability to regenerate the site whenever you push a commit. | 30 | | [hugo-extras](https://github.com/handcraftedbits/docker-nginx-unit-hugo-extras) | An enhanced version of the Hugo unit which contains extra tools. | 31 | | [jira](https://github.com/handcraftedbits/docker-nginx-unit-jira) | The [Atlassian JIRA](https://www.atlassian.com/software/jira) software development tool. | 32 | | [static](https://github.com/handcraftedbits/docker-nginx-unit-static) | A unit that hosts simple static content. | 33 | | [webhook](https://github.com/handcraftedbits/docker-nginx-unit-webhook) | A unit based off of [adnanh/webhook](https://github.com/adnanh/webhook), which allows you to execute arbitrary commands whenever a particular URL is accessed. | 34 | 35 | # Usage 36 | 37 | ## Prerequisites 38 | 39 | ### Docker 40 | 41 | * Docker 1.13 or newer 42 | * Docker Compose 1.10.0 or newer 43 | * `docker-compose.yml` must declare version `2.1` or later 44 | 45 | ### SSL Certificates 46 | 47 | You must obtain SSL certificates from Let's Encrypt by following the 48 | [getting started guide](https://letsencrypt.org/getting-started/). Don't worry about writing a renewal script -- this 49 | Docker container handles that for you. 50 | 51 | #### A Note on Certificate Directory Names and Units 52 | 53 | Keep in mind that Let's Encrypt certificates are registered in terms of single hostnames and the directory structure 54 | it creates will reflect that. For example, if you create a certificate for `mysite.com`, Let's Encrypt will create a 55 | directory named `/etc/letsencrypt/live/mysite.com`. As long as the units you use are configured to be served from 56 | that same host (via `NGINX_UNIT_HOSTS` environment variable), there will be no problem. 57 | 58 | However, you can configure units to be served from multiple discrete hosts, via wildcard, etc. Consider a unit that is 59 | served from `*.mysite.com` and `othersite.com` by setting the environment variable 60 | `NGINX_UNIT_HOSTS=*.mysite.com,othersite.com`. NGINX Host will attempt to look for the certificate in the directory 61 | `/etc/letsencrypt/live/*.mysite.com,othersite.com`. Since no such directory exists (after all, you registered your 62 | certificate against `mysite.com`), NGINX Host won't be able to find your certificate. To fix this, you need to create 63 | a symbolic link in your local `/etc/letsencrypt` directory from `*.mysite.com,othersite.com` to `mysite.com`. 64 | 65 | ### Custom Diffie-Hellman parameters 66 | 67 | Though not required, it is strongly recommended that you create custom Diffie-Hellman parameters for added security. 68 | If you're unsure how to do this, please follow 69 | [this guide](https://scotthelme.co.uk/squeezing-a-little-more-out-of-your-qualys-score/). 70 | 71 | ## Configuration 72 | 73 | It is highly recommended that you use Docker orchestration software such as 74 | [Docker Compose](https://www.docker.com/products/docker-compose) as any NGINX Host setup you are likely to use will 75 | involve several Docker containers. This guide will assume that you are using Docker Compose. 76 | 77 | To begin, let's create a `docker-compose.yml` file that contains the bare minimum set of services and volumes required: 78 | 79 | ```yaml 80 | version: "2.1" 81 | 82 | volumes: 83 | data: 84 | 85 | services: 86 | host: 87 | image: handcraftedbits/nginx-host 88 | ports: 89 | - "443:443" 90 | volumes: 91 | - data:/opt/container/shared 92 | - /etc/letsencrypt:/etc/letsencrypt 93 | - /home/me/dhparam.pem:/etc/ssl/dhparam.pem 94 | ``` 95 | 96 | The `host` service creates an instance of NGINX Host, listening on port `443`. If you wish, you can also listen on 97 | port `80` and NGINX Host will automatically redirect HTTP requests to HTTPS. 98 | 99 | Next, we mount the following volumes: 100 | 101 | * `data`: a volume used to share information between NGINX Host and its units. This volume must always be mounted to 102 | `/opt/container/shared`. 103 | * `/etc/letsencrypt`: the location of your Let's Encrypt certificates and renewal information. Typically this will be 104 | located in the `/etc/letsencrypt` directory on your local system. 105 | * `/etc/ssl/dhparam.pem`: the file containing your custom Diffie-Hellman parameters. Note that this volume does not 106 | have to be mounted, but it is highly recommended to do so in the interest of increased security. 107 | 108 | ## Adding Units 109 | 110 | The configuration we created in the previous section will start an NGINX server but is not particularly useful as it 111 | hosts nothing. To fix that, let's add some static content by adding the `static` unit (shown here as the `mysite` 112 | service): 113 | 114 | ```yaml 115 | version: "2.1" 116 | 117 | volumes: 118 | data: 119 | 120 | services: 121 | mysite: 122 | image: handcraftedbits/nginx-unit-static 123 | environment: 124 | - NGINX_UNIT_HOSTS=mysite.com 125 | - NGINX_URL_PREFIX=/ 126 | volumes: 127 | - data:/opt/container/shared 128 | - /home/me/mysite:/opt/container/www-static 129 | 130 | proxy: 131 | image: handcraftedbits/nginx-host 132 | links: 133 | - mysite 134 | ports: 135 | - "443:443" 136 | volumes: 137 | - data:/opt/container/shared 138 | - /etc/letsencrypt:/etc/letsencrypt 139 | - /home/me/dhparam.pem:/etc/ssl/dhparam.pem 140 | ``` 141 | 142 | The `NGINX_UNIT_HOSTS` environment variable specifies that we will be listening for requests to `mysite.com` and the 143 | `NGINX_URL_PREFIX` environment variable specifies that all static content will be available under `/`. Finally, we 144 | mount the local directory `/home/me/mysite` as the root of our static content (for more information on configuring the 145 | `static` unit, refer to the [documentation](https://github.com/handcraftedbits/docker-nginx-unit-static)). 146 | 147 | Note that we must add a link in the `proxy` service to each unit that NGINX Host will host. In this case, we add a link 148 | to the `mysite` service. 149 | 150 | There's more to NGINX Host than just static content though -- there are [several units](#available-units) you can mix 151 | and match to create your ideal server. Consult the appropriate unit documentation for more information. 152 | 153 | ## Additional NGINX Configuration 154 | 155 | Additional configuration at the virtual host level (i.e., within a `server` block) can be added by mounting a file 156 | containing additional NGINX directives via the location `/etc/nginx/extra/${hosts}.extra.conf`. For example, if you 157 | have a unit hosted on `*.mysite.com` and `othersite.com` with additional NGINX directives located in the file 158 | `/home/me/myextra.conf`, you would add the volume 159 | `/home/me/myextra.com:/etc/nginx/extra/*.mysite.com,othersite.com.extra.conf` to the `docker run` command used to run 160 | the NGINX Host container. 161 | 162 | You can also add additional configuration at a higher level (in this case, within the `http` block) by mounting a file 163 | containing additional NGINX directives via the location `/etc/nginx/extra.conf`. For example, if you have additional 164 | NGINX directives located in the file `/home/me/nginxextra.conf`, you would add the volume 165 | `/home/me/nginxextra.conf:/etc/nginx/extra.conf` to the `docker run` command used to run the NGINX host container. 166 | 167 | ## Running NGINX Host 168 | 169 | Assuming you are using Docker Compose, simply run `docker-compose up` in the same directory as your 170 | `docker-compose.yml` file. Otherwise, you will need to start each container with `docker run` or a suitable 171 | alternative, making sure to add the appropriate environment variables and volume references. 172 | 173 | # Reference 174 | 175 | ## Environment Variables 176 | 177 | ### Units 178 | 179 | The following environment variables are required by all units (please consult unit documentation for any additional 180 | environment variables that may be required): 181 | 182 | #### `NGINX_UNIT_HOSTS` 183 | 184 | A comma-delimited list used to specify which virtual server or virtual servers will host the unit. In terms of NGINX 185 | configuration, this environment variable is used for the 186 | [`server_name`](http://nginx.org/en/docs/http/server_names.html) directive and follows the same syntax, with the 187 | exception that the values are comma-delimited. 188 | 189 | **Required** 190 | 191 | #### `NGINX_URL_PREFIX` 192 | 193 | The URL prefix to use. Combined with the `NGINX_UNIT_HOSTS` environment variable, this determines the full URL used to 194 | access the unit. For example, using `NGINX_UNIT_HOSTS=mysite.com` and `NGINX_URL_PREFIX=/site` would cause unit 195 | content to be served via the URL `https://mysite.com/site`. 196 | 197 | **Required** 198 | 199 | ### NGINX 200 | 201 | The following environment variables are used to configure the NGINX server used by NGINX Host: 202 | 203 | #### `NGINX_GZIP` 204 | 205 | Used to set the value of the NGINX [`gzip`](http://nginx.org/en/docs/ngx_http_gzip_module.html#gzip) directive. 206 | 207 | **Default value**: `on` 208 | 209 | #### `NGINX_HEADERS_REMOVE` 210 | 211 | A comma-delimited list used to specify which header or headers will be removed from all responses. This is generally 212 | used for security purposes by removing headers that identify the server. 213 | 214 | **Default value**: `Server,X-Powered-By` 215 | 216 | #### `NGINX_KEEPALIVE_TIMEOUT` 217 | 218 | Used to set the value of the NGINX 219 | [`keepalive_timeout`](http://nginx.org/en/docs/ngx_http_core_module.html#keepalive_timeout) directive. 220 | 221 | **Default value**: `65` 222 | 223 | #### `NGINX_PROXY_READ_TIMEOUT` 224 | 225 | Used to set the value of the NGINX 226 | [`proxy_read_timeout`](http://nginx.org/en/docs/ngx_http_proxy_module.html#proxy_read_timeout) directive. 227 | 228 | **Default value**: `120s` 229 | 230 | #### `NGINX_RESOLVER` 231 | 232 | Used to set the value of the NGINX [`resolver`](http://nginx.org/en/docs/ngx_http_core_module.html#resolver) directive. 233 | 234 | **Default value**: `8.8.8.8 8.8.4.4` 235 | 236 | #### `NGINX_TYPES_HASH_MAX_SIZE` 237 | 238 | Used to set the value of the NGINX 239 | [`types_hash_max_size`](http://nginx.org/en/docs/ngx_http_core_module.html#types_hash_max_size) directive. 240 | 241 | **Default value**: `2048` 242 | 243 | #### `NGINX_UNIT_WAIT` 244 | 245 | Used to set the time, in seconds, that NGINX Host will wait for units to launch. The value only needs to be changed if 246 | a particular unit takes an excessively long time to launch. 247 | 248 | **Default value**: `2` 249 | 250 | #### `NGINX_WORKER_CONNECTIONS` 251 | 252 | Used to set the value of the NGINX 253 | [`worker_connections`](http://nginx.org/en/docs/ngx_core_module.html#worker_connections) directive. 254 | 255 | **Default value**: `768` 256 | 257 | #### `NGINX_WORKER_PROCESSES` 258 | 259 | Used to set the value of the NGINX 260 | [`worker_processes`](http://nginx.org/en/docs/ngx_core_module.html#worker_processes) directive. 261 | 262 | **Default value**: `auto` 263 | 264 | #### `NGINX_WWW_REDIRECT_HOSTS` 265 | 266 | A comma-delimited list used to specify which host(s) will have a `www` to non-`www` redirect added automatically. This 267 | is useful if you want to force the use of "naked" (non-`www`) domains. Note that you cannot use wildcards for this 268 | environment variable. -------------------------------------------------------------------------------- /data/opt/container/script/renew.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Renews Let's Encrypt certificates. 4 | # Adapted from https://letsecure.me/secure-web-deployment-with-lets-encrypt-and-nginx/ 5 | 6 | logFile=/var/log/letsencrypt/renew.log 7 | 8 | date >> ${logFile} 9 | echo -e "---\n" >> ${logFile} 10 | certbot renew --webroot --webroot-path /var/www/letsencrypt-well-known >> ${logFile} 2>&1 11 | echo -e "\n" >> ${logFile} 12 | 13 | /usr/share/nginx/sbin/nginx -s reload 14 | -------------------------------------------------------------------------------- /data/opt/container/script/run-nginx.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | units_dir=/opt/container/shared/etc/nginx/host/units 4 | 5 | function createServerConf () { 6 | local common_config=/etc/nginx/host/servers/${1}.conf.common 7 | local headers_clear="" 8 | local server_config=/etc/nginx/host/servers/${1}.conf 9 | 10 | cp /opt/container/template/common.conf.template ${common_config} 11 | cp /opt/container/template/server.conf.template ${server_config} 12 | fileSubstitute ${server_config} NGINX_PROXY_READ_TIMEOUT ${NGINX_PROXY_READ_TIMEOUT} 13 | fileSubstitute ${common_config} NGINX_RESOLVER ${NGINX_RESOLVER} 14 | fileSubstitute ${common_config} nginx_hosts ${1} 15 | fileSubstitute ${server_config} nginx_hosts ${1} 16 | fileSubstitute ${server_config} nginx_units `echo ${1} | sed "s/,/ /g"` 17 | 18 | if [ -f /etc/ssl/dhparam.pem ] 19 | then 20 | sed -i "s/#ssl_dhparam/ssl_dhparam/g" ${common_config} 21 | fi 22 | 23 | # For NGINX_HEADERS_REMOVE, strip whitespace and split on commas. Then massage the values a bit to fit the format 24 | # of more_clear_headers. 25 | 26 | for header in `echo ${NGINX_HEADERS_REMOVE} | xargs | tr "," "\n"` 27 | do 28 | headers_clear="${headers_clear} '"`echo ${header} | xargs`"'" 29 | done 30 | 31 | if [ ! -z "${headers_clear}" ] 32 | then 33 | sed -i "s/\${headers_clear}/${headers_clear}/g" ${common_config} 34 | sed -i "s/#more_clear_headers/more_clear_headers/g" ${common_config} 35 | fi 36 | 37 | # Include any extra configuration for the virtual host if available. 38 | 39 | if [ -f /etc/nginx/extra/${1}.extra.conf ] 40 | then 41 | cp /etc/nginx/extra/${1}.extra.conf ${server_config}.extra 42 | 43 | sed -i "s/#include/include/g" ${common_config} 44 | fi 45 | } 46 | 47 | function createWWWRedirectConf () { 48 | local www_redirect_config=/etc/nginx/host/servers/${1}.conf.www_redirect 49 | 50 | cp /opt/container/template/www_redirect.conf.template ${www_redirect_config} 51 | fileSubstitute ${www_redirect_config} nginx_hosts ${1} 52 | 53 | sed -i "s/#include/include/g" /etc/nginx/host/servers/${1}.conf 54 | } 55 | 56 | function fileSubstitute () { 57 | sed -i "s/\${"${2}"}/"${3}"/g" ${1} 58 | } 59 | 60 | function nginxConfSubstitute () { 61 | fileSubstitute /etc/nginx/nginx.conf ${1} ${2} 62 | } 63 | 64 | function onProcessStopped () { 65 | kill -TERM ${1} 66 | 67 | # Clean up if the process was terminated by Docker. 68 | 69 | rm -rf /opt/container/shared/* 70 | 71 | exit 0 72 | } 73 | 74 | mkdir -p /etc/nginx/host/servers 75 | mkdir -p /var/www/letsencrypt-well-known 76 | 77 | # Expose /etc/letsencrypt to other units so they can make use of certificates, if necessary. 78 | 79 | mkdir -p /opt/container/shared/etc 80 | rm -rf /opt/container/shared/etc/letsencrypt 81 | cp -R /etc/letsencrypt /opt/container/shared/etc/letsencrypt 82 | 83 | # Fix /etc/nginx/nginx.conf. 84 | 85 | cp /opt/container/template/nginx.conf.template /etc/nginx/nginx.conf 86 | nginxConfSubstitute NGINX_GZIP ${NGINX_GZIP} 87 | nginxConfSubstitute NGINX_KEEPALIVE_TIMEOUT ${NGINX_KEEPALIVE_TIMEOUT} 88 | nginxConfSubstitute NGINX_TYPES_HASH_MAX_SIZE ${NGINX_TYPES_HASH_MAX_SIZE} 89 | nginxConfSubstitute NGINX_WORKER_PROCESSES ${NGINX_WORKER_PROCESSES} 90 | nginxConfSubstitute NGINX_WORKER_CONNECTIONS ${NGINX_WORKER_CONNECTIONS} 91 | 92 | # Insert a brief pause to give us time for all the units to launch. 93 | 94 | echo "[info] waiting ${NGINX_UNIT_WAIT} second(s) for units to launch" 95 | 96 | sleep ${NGINX_UNIT_WAIT} 97 | 98 | echo "[info] found "`ls ${units_dir}/__launched__ | wc -w`" unit(s) to start" 99 | 100 | # Ping each unit to let it proceed with starting its main process. 101 | 102 | for unit in `ls ${units_dir}/__launched__ 2> /dev/null` 103 | do 104 | echo "[info] starting unit ${unit}..." 105 | 106 | until echo "start" | nc ${unit} 1234 107 | do 108 | sleep 0.1 109 | done 110 | done 111 | 112 | echo "[info] started "`ls ${units_dir}/__launched__ | wc -w`" unit(s)" 113 | 114 | rm -rf ${units_dir}/__launched__ 115 | 116 | # Create a server configuration for each host containing units. 117 | 118 | for host in `ls ${units_dir} 2> /dev/null` 119 | do 120 | createServerConf ${host} 121 | 122 | # Turn NGINX_WWW_REDIRECT_HOSTS into an array. If the current host is found, create the appropriate configuration. 123 | 124 | IFS=',' read -r -a www_redirect_hosts <<< ${NGINX_WWW_REDIRECT_HOSTS} 125 | 126 | for www_redirect_host in "${www_redirect_hosts[@]}" 127 | do 128 | if [ "${www_redirect_host}" == "${host}" ] 129 | then 130 | createWWWRedirectConf ${host} 131 | fi 132 | done 133 | done 134 | 135 | # Add any extra global configuration. 136 | 137 | if [ -f /etc/nginx/extra.conf ] 138 | then 139 | sed -i "s/#include/include/g" /etc/nginx/nginx.conf 140 | fi 141 | 142 | # Start cron for automated certificate renewal. 143 | 144 | crond 145 | 146 | /usr/share/nginx/sbin/nginx -g "daemon off;" & 147 | 148 | pid=$! 149 | 150 | trap "onProcessStopped ${pid}" INT KILL TERM 151 | 152 | wait ${pid} 153 | 154 | # Clean up if the process was terminated unexpectedly. 155 | 156 | rm -rf /opt/container/shared/* -------------------------------------------------------------------------------- /data/opt/container/template/common.conf.template: -------------------------------------------------------------------------------- 1 | # 2 | # Security headers 3 | # 4 | 5 | # Content-Type options. 6 | # See https://scotthelme.co.uk/hardening-your-http-response-headers/#x-content-type-options 7 | add_header X-Content-Type-Options 'nosniff' always; 8 | 9 | # HTTP Strict Transport Security. 10 | # See https://raymii.org/s/tutorials/HTTP_Strict_Transport_Security_for_Apaache_NGINX_and_Lighttpd.html 11 | add_header Strict-Transport-Security 'max-age=63072000; includeSubdomains; preload' always; 12 | 13 | # Frame options. 14 | # See https://scotthelme.co.uk/hardening-your-http-response-headers/#x-frame-options 15 | add_header X-Frame-Options 'DENY' always; 16 | 17 | # XSS protection. 18 | # See https://scotthelme.co.uk/hardening-your-http-response-headers/#x-xss-protection 19 | add_header X-XSS-Protection '1; mode=block' always; 20 | 21 | # Remove user-defined headers. 22 | #more_clear_headers ${headers_clear}; 23 | 24 | # 25 | # SSL settings 26 | # 27 | 28 | ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 29 | ssl_prefer_server_ciphers on; 30 | ssl_session_cache shared:SSL:10m; 31 | ssl_certificate /etc/letsencrypt/live/${nginx_hosts}/fullchain.pem; 32 | ssl_certificate_key /etc/letsencrypt/live/${nginx_hosts}/privkey.pem; 33 | 34 | # Recommended cipher suite per https://wiki.mozilla.org/Security/Server_Side_TLS 35 | ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS'; 36 | 37 | # OCSP Stapling settings. 38 | # See https://raymii.org/s/tutorials/OCSP_Stapling_on_nginx.html 39 | ssl_stapling on; 40 | ssl_stapling_verify on; 41 | ssl_trusted_certificate /etc/ssl/certs/chain.letsencrypt.pem; 42 | resolver ${NGINX_RESOLVER} valid=300s; 43 | resolver_timeout 5s; 44 | 45 | # Diffie-Hellman Ephemeral parameter settings 46 | #ssl_dhparam /etc/ssl/dhparam.pem; 47 | 48 | # User-defined extra configuration. 49 | #include /etc/nginx/host/servers/${nginx_hosts}.conf.extra; -------------------------------------------------------------------------------- /data/opt/container/template/nginx.conf.template: -------------------------------------------------------------------------------- 1 | user root; 2 | worker_processes ${NGINX_WORKER_PROCESSES}; 3 | pid /run/nginx.pid; 4 | 5 | events { 6 | worker_connections ${NGINX_WORKER_CONNECTIONS}; 7 | } 8 | 9 | http { 10 | # 11 | # Basic settings 12 | # 13 | 14 | keepalive_timeout ${NGINX_KEEPALIVE_TIMEOUT}; 15 | sendfile on; 16 | tcp_nodelay on; 17 | tcp_nopush on; 18 | types_hash_max_size ${NGINX_TYPES_HASH_MAX_SIZE}; 19 | 20 | include /etc/nginx/mime.types; 21 | default_type application/octet-stream; 22 | 23 | # 24 | # GZip settings 25 | # 26 | 27 | gzip ${NGINX_GZIP}; 28 | 29 | # 30 | # Servers 31 | # 32 | 33 | include /etc/nginx/host/servers/*.conf; 34 | 35 | # 36 | # Extra configuration 37 | # 38 | 39 | #include /etc/nginx/extra.conf; 40 | } 41 | -------------------------------------------------------------------------------- /data/opt/container/template/server.conf.template: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name ${nginx_units}; 4 | 5 | return 301 https://$host$request_uri; 6 | } 7 | 8 | #include /etc/nginx/host/servers/${nginx_hosts}.conf.www_redirect; 9 | 10 | server { 11 | listen 443 ssl http2; 12 | server_name ${nginx_units}; 13 | 14 | # 15 | # Reverse proxy configuration 16 | # 17 | 18 | proxy_pass_request_headers on; 19 | proxy_read_timeout ${NGINX_PROXY_READ_TIMEOUT}; 20 | proxy_redirect off; 21 | proxy_set_header Host $host; 22 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 23 | proxy_set_header X-Forwarded-Host $host; 24 | proxy_set_header X-Forwarded-Server $host; 25 | proxy_set_header X-Forwarded-Proto $scheme; 26 | proxy_set_header X-Real-IP $remote_addr; 27 | 28 | include /etc/nginx/host/servers/${nginx_hosts}.conf.common; 29 | 30 | location / { 31 | # Hidden location used by Let's Encrypt. 32 | location /.well-known/acme-challenge { 33 | root /var/www/letsencrypt-well-known; 34 | } 35 | 36 | # Unit configuration. 37 | include /opt/container/shared/etc/nginx/host/units/${nginx_hosts}/*.conf; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /data/opt/container/template/www_redirect.conf.template: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | listen 443 ssl http2; 4 | server_name www.${nginx_hosts}; 5 | 6 | include /etc/nginx/host/servers/${nginx_hosts}.conf.common; 7 | 8 | return 301 https://${nginx_hosts}$request_uri; 9 | } --------------------------------------------------------------------------------