Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/netfloex/nginx

A docker container to simplify the process of creating Nginx configs.
https://github.com/netfloex/nginx

cloudflare cloudflare-dns docker docker-compose docker-container docker-image nginx nginx-configuration nginx-docker nginx-module nginx-proxy proxy typescript yarn

Last synced: 3 months ago
JSON representation

A docker container to simplify the process of creating Nginx configs.

Awesome Lists containing this project

README

        

## Nginx Config Manager

This docker container makes it a lot easier to manage Nginx configs.

Instead of creating an entire new file with a lot of boilerplate you can just easily add a new line to the config.
Nginx Config Manager will create the Nginx config for you, and automatically requests certificates for it using certbot.
It will also create Diffie-Hellman parameters, this could take a while when the container launches for the first time.
If you use cloudflare, it can also automatically restore your visitor ip addresses. (See [Cloudflare](#cloudflare-real-ip))

## Installation

This version uses `nginx:stable-alpine` as a parent container. This allows it to reload nginx by itself.
If you would like to have a standalone version see [#standalone](#standalone)

[docker-compose.example.yml](docker-compose.example.yml):

The only required change is `CERTBOT_EMAIL`.

[Click Here](#environment-options) for a list of Environment Options.

```yaml
version: "3.3"

services:
nginx:
image: netfloex/nginx:v2.4.0
container_name: nginx
environment:
CERTBOT_EMAIL: EMAIL # Required
ports:
- 80:80
- 443:443
volumes:
# Optional

# - ./logs:/var/log/nginx
# - ./nginx_config_files:/etc/nginx/conf.d
- ./data:/app/data # Needed when using custom files or cloudflare, this is used as a cache.

# Required

- ./letsencrypt:/etc/letsencrypt
- ./config:/app/config
```

You can create a config file using json5, js or yaml.
The file should be placed in the config folder as `config.(yml|yaml|json|jsonc|json5|js)`


javascript


json


yaml

Pick your favorite language to write your config in and create a file like "config/config.yml"

Simple Example:

```js
module.exports = {
cloudflare: true, // When using Cloudflare
servers: {
"example.com": {
proxy_pass: "http://mysite:3000",
subdomains: {
www: "http://mysite:3000" // Converts to www.example.com
}
}
}
};
```

The above example could be shorter:

```js
module.exports = {
cloudflare: true, // When using Cloudflare
servers: {
"example.com": "http://mysite:3000",
"www.example.com": "http://mysite:3000"
}
};
```

This example fetches [the latest ips from Cloudflare](#cloudflare-real-ip), enables SSL with Certbot, creates DH-Params, and creates two config files:

```
example.com > http://mysite:3000
www.example.com > http://mysite:3000
```

A more complete example can be found [here](config/config.example.js).

### Good to Know

The entire config is located in one file.

All properties are optional.

The config is validated by [zod](https://github.com/colinhacks/zod), so you know when you make mistakes.

If you only need a proxy pass you can shorten it:

Instead of writing:

```js
/* servers: */ {
"example.com": {
proxy_pass: "http://base_domain:80"
}
}
```

You can also write:

```js
/* servers: */ {
"example.com": "http://base_domain:80"
}
```

## Reloading config

The config can be reloaded by sending a SIGHUP signal to the container.
This updates Nginx's configuration files and renews certificates if needed.

When running inside docker:

```bash
docker kill --signal=HUP nginx_config_manager
```

## Variable Substitution

You can use environment variables in your config by using `%env:VARIABLE%`, where `VARIABLE` is your variable.

> Note: currently this is only possible when using JSON \
> Tip: If your using js, you can also use `process.env`

```conf
# environment variables
USERNAME=John
PASSWORD=Doe
```

```jsonc
// config.json
/* Server/Subdomain/Location: */ {
// Single user
"auth": {
"username": "%env:USERNAME%", // "John"
"password": "%env:PASSWORD%" // "Doe"
}
}
```

[Code](src/utils/parseUserConfig.ts)

## Server Options

- [Proxy Pass](#proxy-pass)
- [Custom CSS](#custom-css)
- [Custom JS](#custom-js)
- [Websocket](#websocket)
- [Headers](#headers)
- [Cors](#cors)

- [Return](#return)
- [HTML](#html)
- [Redirect](#redirect)
- [Rewrite](#rewrite)
- [Static Files](#static-files)

- [Basic Auth](#basic-auth)
- [Location Blocks](#location-blocks)

## Global Options

- [Cloudflare Real IP](#cloudflare-real-ip)
- [Access Log Format](#accesslog-format)

## Options

This is a list of options possible inside a (sub)domain or location.

### Proxy Pass

Proxies the request to another location.

When the container starts a DNS Lookup is performed to test if the hostname is valid.
To disable this see [Environment Options](#environment-options)

```js
/* Server/Subdomain/Location: */ {
proxy_pass: "http://hostname:80";
}
```

[Code](src/lib/createConfig.ts)

### Custom CSS

This adds a custom CSS file to an application.
It should be a url to a CSS file.

This file is downloaded and compressed, its then stored inside `/app/custom/css` [Configurable](#paths).

The compressed CSS is then appended to the end of the `` by using Nginx's `sub_filter`.

```js
/* Server/Subdomain/Location: */ {
custom_css: "http://example.com/style.css"; // The use of an array is possible too
}
```

It will add the following directives:

```conf
# Don't sent this header to the proxy
proxy_set_header Accept-Encoding "";
sub_filter '' '';
sub_filter_once on;
```

> Source [theme-park.dev](https://docs.theme-park.dev/setup/#nginx)

[Code](src/utils/downloadCSS.ts)

### Custom JS

This allows to use a custom JS file.
It should be a url to a JS file.

This file is downloaded, its then stored inside `/app/custom/js` [Configurable](#paths).

This file is appended to the end of the `` by using Nginx's `sub_filter`.

```js
/* Server/Subdomain/Location: */ {
custom_js: "http://example.com/script.js"; // The use of an array is possible too
}
```

It will add the following directives:

```conf
# Don't sent this header to the proxy
proxy_set_header Accept-Encoding "";
sub_filter '' '