nanoninja/docker-nginx-php-mysql

I want to create multiple virtual hosts for nginx. Please help me.

furqank786 opened this issue · 3 comments

@nanoninja @ZengLeiPro @ghostal @makros @letvinz

I want to create multiple virtual hosts for the single application available in the web directory.
I want to create new conf file for the virtual host and want to move same as the default.conf.
Tried with the same commands and steps in the yml file, also created new host variable in .env but did not work.
Please guide me to achieve multiple virtual hosts with multiple config files.

Thanks in advance.

@nanoninja @ZengLeiPro @ghostal @makros

Please help me if anyone have idea about how to create multiple environment using virtual host for a single source code e.g. multi-tenancy application.
Thanks

Hi @furqank786,

This is not possible with this default configuration. Basically, the goal is to launch an instance for each project. This goal has not been fully met, but it will be. It is possible to integrate what you ask for, but you have to develop more to achieve this result.

I already managed to do it but it would be premature to add it knowing that it could break the existing.

This is an extract that works on code that I tested:

.env

NGINX_HOST=hostA.com hostB.net hostC.org

docker-compose.yaml

services:
    web:
        image: nginx:alpine
        volumes:
            - "./etc/nginx:/etc/nginx/conf.d"
            - "./etc/ssl:/etc/ssl"
            - "./web:/var/www/html"
            - "./scripts:/scripts"
        ports:
            - "8000:80"
            - "3000:443"
        environment:
            - NGINX_HOST=${NGINX_HOST}
        command: /scripts/nginx.sh
        restart: always
        depends_on:
            - php
            - mysqldb

nginx.sh

chmod +x /scripts/nginx.sh
for SERVER_NAME in $NGINX_HOST; do
  sed -e "s/\${NGINX_HOST}/$SERVER_NAME /" /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/$SERVER_NAME.conf
  mkdir -p /var/www/html/$SERVER_NAME/public
done

nginx -g 'daemon off;'

For everything to work you have to manage all the commands that go together like certificate generation, composer management, etc...

@nanoninja

Thank you for your detailed and valuable answer, I accomplished the multiple virtual host for a single source code. To achieve I just add some line of code in the yml file, the addition given below for the help of others.

 web:       
    image: nginx
    volumes:

        - "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
        - "./etc/ssl:/etc/ssl"
        - "./web:/var/www/html"
        - "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template"
    ports:
        - "8000:80"
        - "3000:443"
    environment:
        - NGINX_HOST=${NGINX_HOST}
        - VIRTUAL_HOST=mydomain1.com
        - VIRTUAL_HOST=mydomain2.com
    command: /bin/bash -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
    restart: always
    depends_on:
        - php
        - mysqldb`

This addition work as required.
Thank you.