Answer the question
In order to leave comments, you need to log in
How to set up multiple local domains in NGINX using Docker?
Hello. Build containers on Windows 10 via WSL Docker to work with Laravel. Suddenly there was a need for several sites with different domains on one copy of running containers. At the moment, 1 site is working with all the specified subdomains, the remaining domains display a 404 error from the first site (there is a Laravel design). It is necessary to configure all domains and split into folders. I assembled the configuration on my own, because. it was extremely difficult to find a normal working version with support for https and self-signed SSL certificates (required for local development).
At the moment my docker-compose.yml config looks like this:
version: "3"
#Docker Networks
networks:
laravel:
#Volumes
volumes:
dbdata:
driver: local
services:
nginx:
image: nginx:alpine
container_name: nginx
extra_hosts:
host.docker.internal: host-gateway
volumes:
- ./www:/var/www/html
- ./config/nginx/nginx-cache-control-headers.conf:/etc/nginx/nginx-cache-control-headers.conf:ro
- ./config/nginx/nginx-security-headers.conf:/etc/nginx/nginx-security-headers.conf:ro
- ./config/nginx/nginx-ssl-options.conf:/etc/nginx/nginx-ssl-options.conf:ro
- ./config/nginx/nginx-deny-invisible-file-access.conf:/etc/nginx/nginx-deny-invisible-file-access.conf:ro
- ./config/nginx/nginx-proxy-to-local.conf:/etc/nginx/nginx-proxy-to-local.conf:ro
- ./config/nginx/certs/youhit.top.loc/cert.crt:/etc/nginx/certs/youhit.top.loc/cert.crt:ro
- ./config/nginx/certs/youhit.top.loc/cert.key:/etc/nginx/certs/youhit.top.loc/cert.key:ro
- ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
ports:
- 80:80
- 443:443
command: [nginx-debug, '-g', 'daemon off;']
networks:
- laravel
php:
build:
context: .
dockerfile: php.dockerfile
container_name: php
volumes:
- ./www:/var/www/html:delegated
networks:
- laravel
mysql:
image: mysql:5.7.29
container_name: mysql
restart: unless-stopped
tty: true
ports:
- 3306:3306
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./config/mysql/dbdata:/var/lib/mysql/
- ./config/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- laravel
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- mysql
depends_on:
- mysql
environment:
PMA_HOST: mysql
PHP_UPLOAD_MAX_FILESIZE: 1G
PHP_MAX_INPUT_VARS: 1G
ports:
- 8081:80
- 8082:443
networks:
- laravel
redis:
image: redis:alpine
container_name: redis
restart: unless-stopped
ports:
- 6379:6379
networks:
- laravel
composer:
build:
context: .
dockerfile: composer.dockerfile
container_name: composer
volumes:
- ./www:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
npm:
image: node:13.7
container_name: npm
volumes:
- ./www:/var/www/html
ports:
- 3000:3000
- 3001:3001
working_dir: /var/www/html
entrypoint: ['npm']
networks:
- laravel
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: artisan
volumes:
- ./www:/var/www/html:delegated
depends_on:
- mysql
working_dir: /var/www/html
user: laravel
entrypoint: ['php', '/var/www/html/artisan']
networks:
- laravel
mailhog:
image: mailhog/mailhog:latest
container_name: mailhog
ports:
- 1025:1025
- 8025:8025
networks:
- laravel
user nginx;
worker_processes auto;
events {
multi_accept on;
worker_connections 5120;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
gzip_vary on;
gzip_proxied any;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_referer" "$gzip_ratio"';
map $request_uri $loggable {
~/(favicon\.ico|robots\.txt|sitemap\.xml|apple-touch-icon.*) 0;
default 1;
}
access_log /var/log/nginx/access.log main if=$loggable;
map $http_upgrade $connection_upgrade {
default upgrade;
"" close;
}
server_tokens off;
server {
listen 443 ssl;
index index.php index.html;
server_name domain1.loc *.domain1.loc;
root /var/www/html/domain1.loc/public;
ssl_certificate "/etc/nginx/certs/domain1.loc/cert.crt";
ssl_certificate_key "/etc/nginx/certs/domain1.loc/cert.key";
include nginx-ssl-options.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
include nginx-proxy-to-local.conf;
include nginx-security-headers.conf;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
server {
listen *:80;
# Comment out this line to enable browsing via HTTP
return 301 https://$host$request_uri;
location / {
include nginx-proxy-to-local.conf;
include nginx-security-headers.conf;
}
include nginx-deny-invisible-file-access.conf;
}
}
Answer the question
In order to leave comments, you need to log in
Yes, indeed, you need to copy the server section, replacing the name in server_name, as well as the name of the root directory.
Create a root directory in ./www
Add another domain name/names domain2.loc and domain3.loc to hosts
Restart
containers
Logs can be viewed by running docker-compose up without the -d
flag happens on your machine and the address on the loopback interface will of course be pinged.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question