gilyes / docker-nginx-letsencrypt-sample

Dockerized Nginx + Let's Encrypt sample
https://gilyes.com/docker-nginx-letsencrypt
385 stars 185 forks source link

Default/catch-all site #9

Closed gerwinov closed 6 years ago

gerwinov commented 6 years ago

Hi @gilyes,

I have a question regarding my docker-compose file. I would like to setup a default / catch all website that is being displayed when no other virtual host is met. Currently I get a standard 503 page from nginx as default. Below is my docker-compose file. The current setup is working for the service where a specific virtual host is present. It is nog working for the default-site. I am trying to set the 'default-website' as default. Can you maybe spot the error and help me configuring this?

version: "2"

  services:
    nginx:
      restart: always
      image: nginx
      container_name: nginx
      ports:
        - "80:80"
        - "443:443"
      volumes:
        - "/etc/nginx/conf.d"
        - "/etc/nginx/vhost.d"
        - "/usr/share/nginx/html"
        - "./volumes/proxy/certs:/etc/nginx/certs:ro"

    nginx-gen:
      restart: always
      image: jwilder/docker-gen
      container_name: nginx-gen
      volumes:
        - "/var/run/docker.sock:/tmp/docker.sock:ro"
        - "./volumes/proxy/templates/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro"
      volumes_from:
        - nginx
      entrypoint: /bin/sh -c
      command: ["/usr/local/bin/docker-gen -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf"]

    letsencrypt-nginx-proxy-companion:
      restart: always
      image: jrcs/letsencrypt-nginx-proxy-companion
      container_name: letsencrypt-nginx-proxy-companion
      volumes_from:
        - nginx
      volumes:
        - "/var/run/docker.sock:/var/run/docker.sock:ro"
        - "./volumes/proxy/certs:/etc/nginx/certs:rw"
      environment:
        - NGINX_DOCKER_GEN_CONTAINER=nginx-gen

    XXXX:
      image: XXXX/YYYY
      container_name: XXXX
      volumes:
        - './XXXX:/config'
      ports:
        - 'YYYY:YYYY'
      environment:
        - VIRTUAL_HOST=XXXX
        - VIRTUAL_NETWORK=nginx-proxy
        - VIRTUAL_PORT=YYYY
        - LETSENCRYPT_HOST=YYYY
        - LETSENCRYPT_EMAIL=ZZZZ
      restart: always

    default-website:
      restart: always
      image: default-website
      build: ./default-website
      container_name: default-website
      volumes:
        - "./volumes/nginx-default/conf.d/:/etc/nginx/conf.d"
      environment:
        - VIRTUAL_HOST=????
        - VIRTUAL_NETWORK=nginx-proxy
        - VIRTUAL_PORT=80

Thanks in advance!

Best regards, Gerwin

gilyes commented 6 years ago

I think DEFAULT_HOST is the environment variable you are looking for: https://github.com/jwilder/nginx-proxy#default-host

gerwinov commented 6 years ago

Thanks for your quick reply.

I tried it like this:

    nginx-gen:
      restart: always
      image: jwilder/docker-gen
      container_name: nginx-gen
      volumes:
        - "/var/run/docker.sock:/tmp/docker.sock:ro"
        - "./volumes/proxy/templates/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro"
      volumes_from:
        - nginx
      entrypoint: /bin/sh -c
      environment:
        - DEFAULT_HOST=XXXX (same name as one VIRTUAL_HOST further down in the file)
      command: ["/usr/local/bin/docker-gen -notify-sighup nginx -watch /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf"]

This however does not seem to work. When I visit my server by IP address I get a 503 nginx error. Do you have any suggestions?

Thanks in advance!

Best regards, Gerwin

gilyes commented 6 years ago

Haven't tried this but it looks good to me... Did you recreate the containers?

BTW, you can check the generated nginx config with docker exec nginx cat /etc/nginx/conf.d/default.conf

gerwinov commented 6 years ago

Recreating the containers (using docker-compose up --force-recreate) did the trick for me. Rookie mistake I guess.. Thanks a lot!