NGINX Proxy Automation

repository·main·Indexed 25 days ago

https://github.com/evertramos/nginx-proxy-automation

A tool for setting up and managing an NGINX reverse proxy environment using Docker. It automates hosting multiple sites, managing Docker networks, and handling automatic Let's Encrypt SSL certificate renewals. Key features include an interactive setup via fresh-start.sh, support for basic authentication, custom port forwarding via VIRTUAL_PORT, and environment customization through a .env file for NGINX, docker-gen, and acme-companion services.

Tokens
5.5K
Snippets
7
Records
32
Agent score
84%

What's inside nginx-proxy-automation

  1. Configure Cloudflare DNS-01 in NGINX Proxy

    main

    After generating your Cloudflare API Token, follow these steps to activate DNS-01 validation:

    1. Open your .env file.
    2. Add your token to the CLOUDFLARE_DNS_TOKEN environment variable.
    3. Open your docker-compose.yml file and uncomment the Cloudflare-related environment variable lines.
    4. Restart your service by running docker compose up -d from your root folder.
  2. Run the fresh-start.sh setup script

    main

    The fresh-start.sh script performs the initial configuration process via an interactive walkthrough.

    Interactive Mode

    Navigate to the bin directory and run the script:

    cd proxy/bin
    ./fresh-start.sh

    Non-interactive Mode (Quick Start)

    If you want to skip the interactive prompts, use the following flags:

    • --yes: Automatically answer yes to prompts.
    • -e <email>: Provide your email for Let's Encrypt.
    • --skip-docker-image-check: Skip the Docker image verification step.

    To see all available options, run ./fresh-start.sh --help.

  3. Configure Basic Authentication for a Virtual Host

    main

    To secure a virtual host with basic authentication, create an htpasswd file at ${NGINX_FILES_PATH}/htpasswd/${VIRTUAL_HOST}.

    Replace ${NGINX_FILES_PATH} with your actual NGINX files path, [username] with your desired username, and ${VIRTUAL_HOST} with your domain name.

    sudo sh -c "echo -n '[username]:' >> ${NGINX_FILES_PATH}/htpasswd/${VIRTUAL_HOST}"
    sudo sh -c "openssl passwd -apr1 >> ${NGINX_FILES_PATH}/htpasswd/${VIRTUAL_HOST}"
  4. Configure DDNS for port forwarding

    main
    When using port forwarding to expose your home server to the internet, it is highly recommended to use Dynamic DNS (DDNS). Because ISPs frequently change your router's public IP address, DDNS ensures your registered domain always points to the current public IP of your router, preventing connection failures when the IP changes.
  5. Generate a Cloudflare API Token for ACME DNS-01

    main

    To allow the NGINX proxy to validate your domain using the DNS-01 challenge, you must create a restricted Cloudflare API Token.

    1. In your Cloudflare profile, navigate to the API Token option.
    2. Create a token using the 'Edit zone DNS' template.
    3. Crucial Security Step: Restrict the token's access to only the specific domains you intend to use and, if possible, specify your source IP address.
    4. Copy the generated token.
  6. Set up configuration directories and permissions for Synology

    main

    To ensure the proxy has the necessary directories and correct permissions for persistent files, run the following commands. This assumes your data catalog is located in the default ./data directory.

    mkdir -p data/certs
    mkdir data/htpasswd
    mkdir data/conf.d
    mkdir data/vhost.d
    mkdir data/html
    chgrp -R 101 data
    chmod -R g+rwx data
  7. Configure port mapping for Synology NAS

    main

    Synology NAS installs a default web server on port 80, which blocks certificate generation. To resolve this, configure your .env file to use alternative internal ports for HTTP and HTTPS.

    Important: You must configure your internet router to forward external traffic from the official ports to these new internal ports. For example, if you set DOCKER_HTTP=81, your router must forward external port 80 to internal port 81.

    DOCKER_HTTP=81
    DOCKER_HTTPS=444
  8. Test the NGINX Proxy setup

    main

    You can verify your setup using the provided test scripts or by running a dummy container.

    Using test scripts

    Run test.sh with your domain (ensure DNS is already pointing to your server):

    ./test.sh your.domain.com

    To test Let's Encrypt specifically:

    ./ssl_test.sh your.domain.com

    Manual test container

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

    To clean up the test:

    ./stop.sh
    # OR
    docker stop test-web && docker rm test-web
  9. Install NGINX Proxy Automation

    main

    To set up the automation environment, clone the repository using the --recurse-submodules flag to ensure external dependencies like basescript are included.

    Prerequisites

    • A Linux server.
    • Docker and Docker-compose installed.
    • Ports 80 and 443 available (ensure no other web services like Apache or NGINX are running on these ports).
    • A public IP address accessible to the server.
    git clone --recurse-submodules https://github.com/evertramos/nginx-proxy-automation.git proxy
  10. Upgrade from v0.4 to v0.5

    main

    Upgrading from v0.4 to v0.5 involves significant changes to automation and network handling. Follow these steps to migrate your production environment while minimizing downtime.

    1. Backup

    Backup all files on the server and to an external location before proceeding.

    2. Update the Repository

    First, preserve your current configuration by copying the compose files and environment variables:

    $ cp docker-compose.yml docker-compose-old.yml
    $ cp .env .env-old

    (Note: Use docker-compose-multiple-networks.yml instead of docker-compose.yml if you are using that specific configuration.)

    Then, reset the repository to the latest version:

    $ git reset --hard
    $ git pull origin master
    $ git checkout master
    $ git submodule init
    $ git submodule update

    3. Initialize New Settings

    Run the fresh-start.sh script to generate the new configuration settings:

    $ cd bin
    $ ./fresh-start.sh

    Note on expected errors: You may see a port binding error (e.g., port 80 is already in use). This is expected because the current nginx-proxy container is still running. If you use the same service and network names as before, you might not see this error.

    4. Network Migration

    If you changed your network name during the upgrade, you must connect all currently running containers to the new network created by fresh-start.sh:

    $ docker network connect [YOUR_NEW_NETWORK_NAME] [CONTAINER_NAME]

    Important: Update the network name in the docker-compose.yml file for all your sites to ensure they persist on the new network after a restart.

    5. Switch to the New Proxy

    To minimize downtime, stop the old services and start the new ones in a single command:

    $ docker-compose --file docker-compose-old.yml down && docker-compose up -d

    If sites are not working, check the letsencrypt container logs. It may take several minutes for Let's Encrypt to issue new certificates.

    6. Revert if Necessary

    If the upgrade fails, you can quickly restore the previous environment using the backup files:

    $ docker-compose down && docker-compose --file docker-compose-old.yml --env-file .env-old up -d

    7. Cleanup

    Once the upgrade is verified as successful, remove the backup files:

    $ rm docker-compose-old.yml .env-old
    # Backup current config
    $ cp docker-compose.yml docker-compose-old.yml
    $ cp .env .env-old
    
    # Update repo
    $ git reset --hard
    $ git pull origin master
    $ git checkout master
    $ git submodule init
    $ git submodule update
    
    # Run fresh start
    $ cd bin
    $ ./fresh-start.sh
    
    # Connect containers to new network (if name changed)
    $ docker network connect [YOUR_NEW_NETWORK_NAME] [CONTAINER_NAME]
    
    # Switch proxy
    $ docker-compose --file docker-compose-old.yml down && docker-compose up -d
    
    # Revert if failed
    $ docker-compose down && docker-compose --file docker-compose-old.yml --env-file .env-old up -d