HungOm / nginx-proxy-automation

Automated docker nginx proxy integrated with letsencrypt.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NGINX Proxy Automation

This project automates the nginx-proxy. We strongly recommend you (:pray: please do!) to read all documentation.

To access the previous version of this project please access version 0.4.

What this project does

This script will set up your server with the nginx-proxy, which will enable you to host multiple sites, auto renewing Let´s Encrypt certificates.

Something like:

Web Proxy environment

Prerequisites

  1. Linux! 🐧 (just in case...)

Please check all requirements at requirements.

In order to use this compose file (docker-compose.yml) you must have:

  1. Docker installed (https://docs.docker.com/engine/installation/)

  2. Docker-compose installed (https://docs.docker.com/compose/install/)

I have an easy-server for myself which I use to install docker and docker-compose in new servers and some aliases and other stuff. Feel free to use it, but it is not related to this repo and maintainance it's for my own use only. Check './install/docker' folder.

Also, you will need to make sure you have:

  1. Port 80 and 443 available for binding - which means apache/nginx or other web services should not be running in your server

  2. Server must be accessible by a public IP address

How to use it

  1. Clone this repository:
$ git clone --recurse-submodules https://github.com/evertramos/nginx-proxy-automation.git proxy 

Make sure you use the option '--recurse-submodules' once we use an external module in this project, please check basescript

Please note we use 'proxy' as folder at the end. But you can change it to whatever fits you better

  1. Run the script 'fresh_start.sh'
$ cd proxy/bin
$ ./fresh-start.sh

This script will walk you through all config process.

When it finishes you are good to go! 🏁

You can check all available options to run the script $ ./fresh-start.sh --help

  1. Fire your new site with the following options:
  VIRTUAL_HOST=your.domain.com
  LETSENCRYPT_HOST=your.domain.com
  LETSENCRYPT_EMAIL=your.email@your.domain.com
  NETWORK=proxy

The fresh start script asked you for the proxy network name if you changed set a name differente from the default please update the option 'NETWORK' in the examples below before running it.

  • Simple site without Let's Encrypt certificate
$ docker run -d -e VIRTUAL_HOST=your.domain.com \
              --network=proxy \
              --name my_app \
              httpd:alpine
  • To have SSL in your web/app you must add the option -e LETSENCRYPT_HOST=your.domain.com, as follow:
$ docker run -d -e VIRTUAL_HOST=your.domain.com \
              -e LETSENCRYPT_HOST=your.domain.com \
              -e LETSENCRYPT_EMAIL=your.email@your.domain.com \
              --network=proxy \
              --name my_app \
              httpd:alpine

You don´t need to open port 443 in your container, the certificate validation is managed by the web proxy

Please note that when running a new container to generate certificates with Let's Encrypt (-e LETSENCRYPT_HOST=your.domain.com), it may take a few minutes

Further Options

  1. Basic Authentication Support

In order to be able to secure your virtual host with basic authentication, you must create a htpasswd file within ${NGINX_FILES_PATH}/htpasswd/${VIRTUAL_HOST} via:

$ sudo sh -c "echo -n '[username]:' >> ${NGINX_FILES_PATH}/htpasswd/${VIRTUAL_HOST}"
$ sudo sh -c "openssl passwd -apr1 >> ${NGINX_FILES_PATH}/htpasswd/${VIRTUAL_HOST}"

Please replace the ${NGINX_FILES_PATH} with real path to information, replace [username] with your username and ${VIRTUAL_HOST} with your host's domain. You will be prompted for a password.

  1. Using different networks

If you want to use more than one network to better organize your environment you could set the option SERVICE_NETWORK in our .env.sample or you can just create your own network and attach all your containers as of:

docker network create myownnetwork
docker network connect myownnetwork nginx-web
docker network connect myownnetwork nginx-gen
docker network connect myownnetwork nginx-letsencrypt
  1. Using different ports to be proxied

If your service container runs on port 8545 you probably will need to add the VIRTUAL_PORT environment variable to your container, in the docker-compose.yml, as of:

parity
    image: parity/parity:v1.8.9
    [...]
    environment:
      [...]
      VIRTUAL_PORT: 8545

Or as of below:

docker run [...] -e VIRTUAL_PORT=8545 [...]
  1. Restarting proxy container

In some cases you will need to restart the proxy in order to read, as an example, the Basic Auth, if you set it after your service container is already up and running. So, the way I use to restart the proxy (NGINX) is as following, which has no downtime:

docker exec -it ${NGINX_WEB} nginx -s reload

Where ${NGINX_WEB} is your proxy container name, which in the original .env file is set as nginx-web.

Testing your proxy with scripts preconfigured

  1. Run the script test.sh informing your domain already configured in your DNS to point out to your server as follow:
./test_start_ssl.sh your.domain.com

or simply run:

docker run -dit -e VIRTUAL_HOST=your.domain.com --network=webproxy --name test-web httpd:alpine

Access your browser with your domain!

To stop and remove your test container run our stop_test.sh script:

./test_stop.sh

Or simply run:

docker stop test-web && docker rm test-web

Running this Proxy on a Synology NAS

Please checkout this howto.

Production Environment using Web Proxy

Following are links to docker containers using this web proxy:

  1. docker-wordpress-letsencrypt
  2. docker-portainer-letsencrypt
  3. docker-nextcloud-letsencrypt
  4. docker-registry-letsencrypt
  5. gitlab-docker-letsencrypt
  6. docker-webtrees-letsencrypt

Credits

Without the repositories below this webproxy wouldn´t be possible.

Credits goes to:

Special thanks to:

Support this project at Patreon

https://www.patreon.com/evertramos

About

Automated docker nginx proxy integrated with letsencrypt.

License:MIT License


Languages

Language:Shell 100.0%