Using Nginx-Proxy in Your Homelab: A Comprehensive Guide

  ·   3 min read

In the world of self-hosting and homelabs, managing multiple web services can become a complex task. This is where a reverse proxy like Nginx comes into play. Nginx-proxy, an automated reverse proxy for Docker containers, simplifies the process of managing and routing traffic to your various services. In this article, we’ll explore how to set up and use nginx-proxy in your homelab environment.

What is Nginx-Proxy?

Nginx-proxy is a Docker container that automatically configures itself to act as a reverse proxy for other Docker containers. It listens for Docker events and updates its configuration to route traffic to the appropriate container based on the domain name. This is particularly useful in a homelab setup where you might be running multiple web services on a single machine.

Why Use Nginx-Proxy?

  1. Simplified Configuration: Automatically generates Nginx configuration files based on Docker container labels.
  2. Domain-Based Routing: Routes traffic based on domain names, allowing you to host multiple services on the same IP address.
  3. SSL Support: Easily integrates with Let’s Encrypt to provide SSL certificates for your services.
  4. Scalability: As you add more services, nginx-proxy automatically updates its configuration without manual intervention.

Setting Up Nginx-Proxy

Prerequisites

  • A machine with Docker installed.
  • Basic knowledge of Docker and networking concepts.
  • Domain names pointing to your homelab’s public IP address.

Step-by-Step Guide

  1. Pull the Nginx-Proxy Image

    Start by pulling the nginx-proxy Docker image from Docker Hub:

    docker pull jwilder/nginx-proxy
    
  2. Run the Nginx-Proxy Container

    Launch the nginx-proxy container with the following command:

    docker run -d -p 80:80 -p 443:443 \
      --name nginx-proxy \
      -v /var/run/docker.sock:/tmp/docker.sock:ro \
      jwilder/nginx-proxy
    

    This command maps the Docker socket into the container, allowing it to listen for Docker events and update its configuration.

  3. Configure Your Web Services

    For each web service you want to proxy, add the VIRTUAL_HOST environment variable to the Docker run command. For example:

    docker run -d \
      --name my-web-app \
      -e VIRTUAL_HOST=example.com \
      my-web-app-image
    

    Replace example.com with your actual domain name.

  4. Enable SSL with Let’s Encrypt (Optional)

    To enable SSL, you can use the companion container jrcs/letsencrypt-nginx-proxy-companion. First, pull the image:

    docker pull jrcs/letsencrypt-nginx-proxy-companion
    

    Then, run the container:

    docker run -d \
      --name nginx-proxy-letsencrypt \
      --volumes-from nginx-proxy \
      -v /var/run/docker.sock:/var/run/docker.sock:ro \
      -v /etc/acme.sh:/etc/acme.sh \
      jrcs/letsencrypt-nginx-proxy-companion
    

    For each service, add the LETSENCRYPT_HOST and LETSENCRYPT_EMAIL environment variables:

    docker run -d \
      --name my-web-app \
      -e VIRTUAL_HOST=example.com \
      -e LETSENCRYPT_HOST=example.com \
      -e LETSENCRYPT_EMAIL[email protected] \
      my-web-app-image
    

Conclusion

Using nginx-proxy in your homelab can greatly simplify the management of multiple web services. With its automated configuration and SSL support, you can focus more on developing and deploying your applications rather than managing complex network configurations. As your homelab grows, nginx-proxy will scale with you, providing a robust solution for routing traffic efficiently.

References

By leveraging open-source tools like nginx-proxy, you can enhance the capabilities of your homelab while maintaining a high level of control and customization.