Skip to content

Automated nginx proxy for Docker containers using docker-gen

License

Notifications You must be signed in to change notification settings

PiTiLeZarD/nginx-proxy

 
 

Repository files navigation

nginx-proxy

This repository is a fork of the very well known jwilder/nginx-proxy

I customised it to my needs. Which are:

  • Provide an override for location /
  • While using fastcgi, nginx serves static files directly instead of passing them along
  • Multi node, Multi container swarm config

How did I solve the swarm situation

  • Every node generate their config as usual, except they do it in a different folder (/etc/nginx/node.conf.d/)
  • the nginx.tmpl is using service_name instead of IP
  • The proxy is deployed globally (one instance per node)
  • A cron will run periodically to check if anything has changed and run a python script
  • That python script combines all configs into one that is /etc/nginx/conf.d/default.conf using (crossplane)[https://github.com/nginxinc/crossplane] and reload nginx

For this to work, all you need is a way to share data between node. It could be a volume driver or anything. I'm using azure, so I have a shared directory on all nodes (which also contains my static files) so I bind /etc/nginx/node.conf.d/ in the shared directory, all nodes add their files, all proxy will regenerate their config including all other nodes. When a new node joins, entr will trigger in each node and the new configuration is generated. If you rebalance your swarm, docker-gen will trigger, that node's config will be updated which in turns triggers entr and so on.

If you want your node.conf.d/ files to have the host name instead of the container ID to ease debugging, you can add this environment variable:

    environment:
        - NODE_HOSTNAME={{.Node.Hostname}}

Entrypoint extentions

If you provide /app/docker-entrypoint-ext.sh it will be loaded before starting forego

Override root location

You can set LOCATION_PATH=xxxon either the proxy for all containers or on the container itself (eg: "~ .php$") and use the vhost.d/default or vhost.d/{VIRTUAL_HOST} to add:

location / {
    try_files $uri /index.php?$query_string;
    limit_rate_after 1000k;
    limit_rate 50k;
}

location {LOCATION_PATH} {
  ...
}

Bind static files

You can bind your files in "/etc/nginx/static_files/{VIRTUAL_HOST}" and nginx will set the root of the server block to that folder as follows:

server {
  ...

  root /etc/nginx/static_files/my.domain.com;

  '''
}

In combination with LOCATION_PATH override you can skip sending queries to the container and serve files directly.

Be aware that if using FastCGI you will also have to explicitly set your VIRTUAL_ROOT.

Server Options

Since we're working in a swarm and the upstreams are networks with possibly many replicas, you migh want to disable max_fails for nginx so even if one container fails, nginx keeps sending requests on that network.

A new environment variable which is either applied on the service or on the proxy as default is available:

        environment:
            - SERVER_OPTIONS=max_fails=0

This will essentially tell nginx, don't bother, keep sending!

About

Automated nginx proxy for Docker containers using docker-gen

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 78.9%
  • Shell 15.8%
  • Dockerfile 4.3%
  • Other 1.0%