ab-nginx/README.md

229 lines
14 KiB
Markdown
Raw Normal View History

2021-01-05 23:06:52 -07:00
# ab-nginx
2019-10-16 21:48:03 -06:00
2021-01-07 07:29:39 -07:00
Containerized fully-functional implementation of NGINX running on Alpine. The container by default is a 'blank slate' that just serves files out of the box. Changing configuration, server blocks and content is accomplished with simple bind-mounts using a sensible, simple directory structure. The container auto-detects mounted certificates and switches to TLS automatically. [Helper scripts](https://git.asifbacchus.app/ab-docker/ab-nginx/releases) in the git repo make certificate mounting easier, allow for custom docker networks and more. The container by default can be used as a Lets Encrypt endpoint with tools like certbot.
2021-01-05 23:06:52 -07:00
## Contents
- [Alternate repository](#alternate-repository)
- [Documentation and scripts](#documentation-and-scripts)
2021-01-07 07:29:39 -07:00
- [Container layout](#container-layout)
- [Content directory](#content-directory)
- [Configuration directory](#configuration-directory)
2021-01-05 23:06:52 -07:00
- [Quick-start](#quick-start)
2021-01-07 07:29:39 -07:00
- [Mounting content](#mounting-content)
- [Mounting configurations](#mounting-configurations)
- [Mounting server-blocks](#mounting-server-blocks)
- [TLS](#tls)
2021-01-05 23:06:52 -07:00
- [Shell mode](#shell-mode)
2021-01-07 07:29:39 -07:00
- [Drop to shell before NGINX loads](#drop-to-shell-before-nginx-loads)
- [Enter a running container](#enter-a-running-container)
2021-01-05 23:06:52 -07:00
- [Environment variables](#environment-variables)
2021-01-07 07:29:39 -07:00
- [Logs](#logs)
2021-01-05 23:06:52 -07:00
- [Final thoughts](#final-thoughts)
## Alternate repository
2021-01-07 07:29:39 -07:00
Throughout this document, I will reference my repository on DockerHub (`asifbacchus/ab-nginx:tag`). You may also feel free to pull directly from my private registry instead, especially if you need signed containers. Simply use `docker.asifbacchus.app/nginx/ab-nginx:tag`. I usually sign major dot-version releases (1.18, 1.19, etc.) as well as the 'latest' image.
2021-01-05 23:06:52 -07:00
## Documentation and scripts
Check out the [repo wiki](https://git.asifbacchus.app/ab-docker/ab-nginx/wiki) for detailed examples and documentation about the container and the [helper scripts](https://git.asifbacchus.app/ab-docker/ab-nginx/releases) which are located [here](https://git.asifbacchus.app/ab-docker/ab-nginx/releases).
2021-01-07 07:29:39 -07:00
## Container layout
2021-01-07 07:29:39 -07:00
### Content directory
All content is served from the NGINX default `/usr/share/nginx/html` directory within the container. The default set up serves everything found here and in all child directories. To use your own content (this point of this whole thing, right?) bind-mount your content to the containers webroot: `-v /my/webstuff:/usr/share/nginx/html`.
### Configuration directory
All configuration is in the `/etc/nginx` directory and its children. Here is the layout of that directory within the container:
```text
/etc/nginx
├── config
│ └── **add configuration files here or replace the whole directory**
├── sites
│ ├── 05-test_nonsecured.conf
│ ├── 05-test_secured.conf.disabled
2021-01-07 07:29:39 -07:00
│ └── **add additional server-block files or replace whole directory**
├── ssl-config
│ ├── mozIntermediate_ssl.conf.disabled
│ └── mozModern_ssl.conf.disabled
2021-01-07 07:29:39 -07:00
└── (SSL configuration container manages this)
├── errorpages.conf (pre-configured fun error pages, you can override )
├── health.conf (health-check endpoint, best to not touch this)
2021-01-07 07:29:39 -07:00
├── nginx.conf **main NGINX configuration file, replace if really necessary**
├── server_names.conf (list of hostnames, updated via environment variable)
2021-01-07 07:29:39 -07:00
├── ssl_certs.conf (hard-coded for the container, best not to touch)
```
Locations with \**starred descriptions** are designed to be overwritten via bind-mounts to customize the container. For more details on all of these files and what they do, please refer to the [repo wiki](https://git.asifbacchus.app/ab-docker/ab-nginx/wiki).
2021-01-05 23:06:52 -07:00
## Quick-start
2021-01-07 07:29:39 -07:00
At its most basic, all you need to do is mount a directory with content to serve. For more advanced deployments, you can also mount various configurations. In most cases, youll also want to mount certificates so that SSL/TLS is an option. Lets run through some examples:
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
### Mounting content
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
Simply bind-mount whatever you want served to `/usr/share/nginx/html`:
2021-01-05 23:06:52 -07:00
```bash
2021-01-07 07:29:39 -07:00
docker run -d --name ab-nginx --restart unless-stopped \
-p 80:80 \
-v ~/web:/usr/share/nginx/html \
asifbacchus/ab-nginx
```
### Mounting configurations
Any *.conf* files found in `/etc/nginx/config` will be loaded after *nginx.conf* and, thus, take precedence. All config files are read into the HTTP-context. Please note: **only files ending in .conf** will be read by the container by default!
I suggest dividing your configurations into various files organized by type (i.e. headers.conf, buffers.conf, timeouts.conf, etc.) and putting them all into one directory and bind-mounting that to the container:
```bash
docker run -d --name ab-nginx --restart unless-stopped \
-p 80:80 \
-v ~/web:/usr/share/nginx/html \
-v ~/nginx/config:/etc/nginx/config:ro \
asifbacchus/ab-nginx
```
If you need to change configuration settings, make the changes on the host and save the file(s). Then, restart the container to apply the change:
```bash
docker restart ab-nginx
```
If you want the container to ignore a specific set of configuration options, say youre testing something, then just rename that file with those configuration options using any extension other than *.conf*. I usually use *.conf.disabled*. Restart the container and that file will be ignored.
More details and examples are found in the [wiki](https://git.asifbacchus.app/ab-docker/ab-nginx/wiki).
### Mounting server-blocks
If you just want to serve static content from your content/webroot directory, then you can ignore this section entirely. Otherwise, any files found in the `/etc/nginx/sites` directory will be loaded after configuration files. These files are meant to define the *SERVER*-context. The container has both a secure and non-secure default server block that simply serves everything found in the webroot. Depending on your SSL configuration, the container enables the correct block. You can add additional server blocks or you can override these default servers entirely by bind-mounting over the directory:
```bash
# add another server block definition that listens on port 8080
docker run -d --name ab-nginx --restart unless-stopped \
-p 80:80 \
-p 8080:8080 \
-v ~/web:/usr/share/nginx/html \
-v ~/webapp.conf:/etc/nginx/sites/webapp.conf:ro \
asifbacchus/ab-nginx
# override default server-blocks entirely (use your own)
docker run -d --name ab-nginx --restart unless-stopped \
-p 80:80 \
-v ~/web:/usr/share/nginx/html \
-v ~/nginx/servers:/etc/nginx/sites:ro \
asifbacchus/ab-nginx
2021-01-05 23:06:52 -07:00
```
2021-01-07 07:29:39 -07:00
More details and examples are found in the [wiki](https://git.asifbacchus.app/ab-docker/ab-nginx/wiki).
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
## TLS
The container will automatically update its configuration to use provided certificates. The examples below assume you have all required files in one directory, but you can also mount them all separately. The required files and their locations in the container are:
2021-01-05 23:06:52 -07:00
| file type | container-location |
| ------------------------------------------------------------ | -------------------- |
| Full-chain certificate<br />(certificate concatenated with intermediates and/or root CA) | /certs/fullchain.pem |
| Private key | /certs/privkey.pem |
2021-01-07 07:29:39 -07:00
| Certificate chain<br />(intermediates concatenated with root CA) | /certs/chain.pem |
| DH Parameters file<br />(NOT required for TLS 1.3-only mode) | /certs/dhparam.pem |
2021-01-05 23:06:52 -07:00
Once those files are available, you can run the container as follows:
```bash
# TLS 1.2 (requires: fullchain.pem, privkey.pem, chain.pem and dhparam.pem)
docker run -d --name nginx --restart unless-stopped \
2021-01-07 07:29:39 -07:00
-p 80:80 \
-p 443:443 \
-v ~/web:/usr/share/nginx/html \
-v ~/certs:/certs:ro \
2021-01-06 15:51:15 -07:00
-e SERVER_NAMES="domain.tld www.domain.tld" \
2021-01-05 23:06:52 -07:00
asifbacchus/ab-nginx:latest
# TLS 1.3 only mode (requires fullchain.pem, privkey.pem, chain.pem)
docker run -d --name nginx --restart unless-stopped \
2021-01-07 07:29:39 -07:00
-p 80:80 \
-p 443:443 \
-v ~/web:/usr/share/nginx/html \
-v ~/certs:/certs:ro \
2021-01-06 15:51:15 -07:00
-e SERVER_NAMES="domain.tld www.domain.tld" \
2021-01-05 23:06:52 -07:00
-e TLS13_ONLY=TRUE
asifbacchus/ab-nginx:latest
```
The container will load a secure configuration automatically and require SSL connections. If you want to enforce HSTS, simply set the HSTS environment variable to true by adding `-e HSTS=TRUE` before specifying the container name. Careful about doing this while testing though! Also, certificates should always be mounted read-only (`:ro`) for security reasons!
2021-01-07 07:29:39 -07:00
You may have noticed I also specified the `SERVER_NAMES` variable. This is necessary or SSL will not work since the hostname the server responds to must match the certificate being presented. **Make sure you set this environment variable to match your certificates!** N.B. If you using your own server-blocks, then this environment variable is **NOT** required it is only used by the container when auto-configuring the default server-blocks.
2021-01-06 15:51:15 -07:00
If you want to integrate with Let's Encrypt, please refer to the [wiki](https://git.asifbacchus.app/ab-docker/ab-nginx/wiki).
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
## Shell mode
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
Running the container in shell mode as a great way to verify configurations, make sure everything mounted correctly or to see what the defaults are. You have two options: drop to shell before NGINX loads or after.
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
### Drop to shell before NGINX loads
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
This is most useful to verify where things mounted, etc. This is also useful if some configuration is causing NGINX to panic and shutting down the container. You might notice that Im not mounting anything as read-only in this case. This way I could make changes in the container directly if I wanted and then test them right away with `nginx -t`. To make that easier, I have included nano in the container :smile: Note also that Im using the `--rm` flag to auto-remove the container when I exit it.
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
```bash
docker run -it --rm \
-v ~/web:/usr/share/nginx/html \
-v ~/nginx/config:/etc/nginx/config \
-v ~/nginx/servers:/etc/nginx/sites \
-v ~/certs:/certs \
asifbacchus/ab-nginx /bin/sh
2021-01-05 23:06:52 -07:00
```
2021-01-07 07:29:39 -07:00
### Enter a running container
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
If you want to enter a running container and check things out:
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
```
docker exec -it ab-nginx /bin/sh
2021-01-05 23:06:52 -07:00
```
2021-01-07 07:29:39 -07:00
Remember that this container is running Alpine linux, so the shell is ASH. You do *not* have all the bells and whistles of BASH! Also, many commands are run via busybox, so some things may not work exactly like you might be used to in a Debian/Ubuntu environment, for example. As a side note, *ping* is installed and fully functional in this container so that makes troubleshooting a little easier.
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
## Logs
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
The container logs everything to stdout and stderr in other words, the console. To see whats going on with NGINX simply use dockers integrated logging features:
2021-01-05 23:06:52 -07:00
```bash
2021-01-07 07:29:39 -07:00
# default log lookback
docker logs ab-nginx
2021-01-05 23:06:52 -07:00
2021-01-07 07:29:39 -07:00
# last 50 lines
docker logs -n 50 ab-nginx
# show last 10 lines and follow from there in realtime (ctrl-c to stop)
docker logs -n 10 -f ab-nginx
```
2021-01-05 23:06:52 -07:00
## Environment variables
You can set several options simply by passing environment variables. They are pretty self-explanatory but examples and more details are available in the [wiki](https://git.asifbacchus.app/ab-docker/ab-nginx/wiki). Here's a list of them:
| name | description | default |
| ------------ | ------------------------------------------------------------ | --------------------------- |
| TZ | Set the container time zone for proper logging. | Etc/UTC |
| SERVER_NAMES | Space-delimited list of hostnames/FQDNs to which NGINX should respond. This can be overridden via individual server blocks. Must be "enclosed in quotes". | "_" (this means "anything") |
2021-01-05 23:06:52 -07:00
| HTTP_PORT | Port on which HTTP connections should be accepted. If you set this, make sure you set your port mapping properly! For example, if you set this to 8080 then you need to specify `-p 8080:8080` or something like `-p 12.34.567.89:8080:8080`. | 80 |
| HTTPS_PORT | Port on which HTTPS connections should be accepted. If you set this, make sure you set your port mapping properly! For example, if you set this to 8443 then you need to specify `-p 8443:8443` or something like `-p 12.34.567.89:8443:8443`. | 443 |
| ACCESS_LOG | Turn on/off access logging. There is a default format specified in the container's *nginx.conf*, but you can override this via configuration files. | off |
| HSTS | Activate the HSTS header. Please be sure you know what this means and that your SSL configuration is correct before enabling! | FALSE |
| TLS13_ONLY | Activate the container's default TLS 1.3 configuration. This is a strict TLS 1.3 implementation and does *not* fall back to TLS 1.2. If you still need to support TLS 1.2, then leave this turned off. The TLS 1.2 configuration *does* upgrade to TLS 1.3 where possible. | FALSE |
## Final thoughts
I think that's everything to get you going if you are already familiar with docker and with NGINX in general. If you need more help, please [refer to the wiki](https://git.asifbacchus.app/ab-docker/ab-nginx/wiki). I've explained everything there in detail. Also, check out the [helper scripts](https://git.asifbacchus.app/ab-docker/ab-nginx/releases) especially if you are deploying certificates. The scripts take care of all the docker command-lines for you so you have much less typing!
If I've forgotten anything, you find any bugs or you have suggestions, please file an issue either on my private [git server ](https://git.asifbachus.app/ab-docker/ab-nginx) or on [github](https://github.com/asifbacchus/ab-nginx). Also, I am *not* affiliated with NGINX in any way, so please **do not** bother them with any issues you find with this container. Bother me instead, I actually enjoy it!
All the best and have fun!