Skip to content

Commit df35607

Browse files
committed
Add a Nginx template using a dynamic hostname from .env
1 parent 58613a9 commit df35607

File tree

5 files changed

+88
-32
lines changed

5 files changed

+88
-32
lines changed

.env

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
# See https://docs.docker.com/compose/environment-variables/#the-env-file
44

5+
# Nginx
6+
NGINX_HOST=localhost
7+
8+
# MySQL
59
MYSQL_HOST=mysql
610
MYSQL_DATABASE=test
711
MYSQL_ROOT_USER=root

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ docker-stop:
5353
@make clean
5454

5555
gen-certs:
56-
@docker run --rm -v $(shell pwd)/etc/ssl:/certificates -e "SERVER=localhost" jacoelho/generate-certificate
56+
@docker run --rm -v $(shell pwd)/etc/ssl:/certificates -e "SERVER=$(NGINX_HOST)" jacoelho/generate-certificate
5757

5858
logs:
5959
@docker-compose logs -f

docker-compose.yml

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
version: '3'
22
services:
33
web:
4-
image: nginx:latest
5-
ports:
6-
- "8000:80"
7-
- "3000:443"
8-
restart: always
4+
image: nginx
95
volumes:
106
- "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
117
- "./etc/ssl:/etc/ssl"
128
- "./web:/var/www/html"
9+
- "./etc/nginx/default.template:/etc/nginx/conf.d/default.template"
10+
ports:
11+
- "8000:80"
12+
- "3000:443"
13+
environment:
14+
- NGINX_HOST=${NGINX_HOST}
15+
command: /bin/bash -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
16+
restart: always
1317
depends_on:
1418
- php
1519
- mysqldb
@@ -49,4 +53,4 @@ services:
4953
ports:
5054
- "8989:3306"
5155
volumes:
52-
- "./data/db/mysql:/var/lib/mysql"
56+
- "./data/db/mysql:/var/lib/mysql"

etc/nginx/default.conf

+25-25
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ server {
2121
}
2222
}
2323

24-
# server {
25-
# server_name localhost;
26-
27-
# listen 443 ssl;
28-
# fastcgi_param HTTPS on;
29-
30-
# ssl_certificate /etc/ssl/server.pem;
31-
# ssl_certificate_key /etc/ssl/server.key;
32-
# ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
33-
34-
# index index.php index.html;
35-
# error_log /var/log/nginx/error.log;
36-
# access_log /var/log/nginx/access.log;
37-
# root /var/www/html/public;
38-
39-
# location ~ \.php$ {
40-
# try_files $uri =404;
41-
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
42-
# fastcgi_pass php:9000;
43-
# fastcgi_index index.php;
44-
# include fastcgi_params;
45-
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
46-
# fastcgi_param PATH_INFO $fastcgi_path_info;
47-
# }
48-
# }
24+
server {
25+
server_name localhost;
26+
27+
listen 443 ssl;
28+
fastcgi_param HTTPS on;
29+
30+
ssl_certificate /etc/ssl/server.pem;
31+
ssl_certificate_key /etc/ssl/server.key;
32+
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
33+
34+
index index.php index.html;
35+
error_log /var/log/nginx/error.log;
36+
access_log /var/log/nginx/access.log;
37+
root /var/www/html/public;
38+
39+
location ~ \.php$ {
40+
try_files $uri =404;
41+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
42+
fastcgi_pass php:9000;
43+
fastcgi_index index.php;
44+
include fastcgi_params;
45+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
46+
fastcgi_param PATH_INFO $fastcgi_path_info;
47+
}
48+
}

etc/nginx/default.template

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Nginx configuration
2+
3+
server {
4+
listen 80 default_server;
5+
listen [::]:80 default_server;
6+
server_name ${NGINX_HOST};
7+
8+
index index.php index.html;
9+
error_log /var/log/nginx/error.log;
10+
access_log /var/log/nginx/access.log;
11+
root /var/www/html/public;
12+
13+
location ~ \.php$ {
14+
try_files $uri =404;
15+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
16+
fastcgi_pass php:9000;
17+
fastcgi_index index.php;
18+
include fastcgi_params;
19+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
20+
fastcgi_param PATH_INFO $fastcgi_path_info;
21+
}
22+
}
23+
24+
server {
25+
server_name ${NGINX_HOST};
26+
27+
listen 443 ssl;
28+
fastcgi_param HTTPS on;
29+
30+
ssl_certificate /etc/ssl/server.pem;
31+
ssl_certificate_key /etc/ssl/server.key;
32+
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
33+
34+
index index.php index.html;
35+
error_log /var/log/nginx/error.log;
36+
access_log /var/log/nginx/access.log;
37+
root /var/www/html/public;
38+
39+
location ~ \.php$ {
40+
try_files $uri =404;
41+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
42+
fastcgi_pass php:9000;
43+
fastcgi_index index.php;
44+
include fastcgi_params;
45+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
46+
fastcgi_param PATH_INFO $fastcgi_path_info;
47+
}
48+
}

0 commit comments

Comments
 (0)