This are some blueprints for Nginx dockerized. For a more detailed overview of Nginx check out my “Nginx Tutorial: HTTP, HTTPS, Reverse Proxy for Apache2” post.
Nginx Prerequisites #
Folder Structure #
# Create folders
sudo mkdir -p /opt/nginx/{conf.d,html,custom-logs} && cd /opt/nginx
# Set permissions
sudo chmod 755 /opt/nginx/{conf.d,html,custom-logs}
Certbot #
# Install Certbot
sudo apt install certbot -y
# Create certificate
sudo certbot certonly --standalone -d mywebsitet.jklug.work
HTML Site #
# Create HTML site
sudo vi /opt/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>jklug.work</title>
</head>
<body>
<h1>Nginx Docker Compose</h1>
</body>
</html>
Nginx Overview #
HTTP Version #
Docker Compose #
# Create Docker Compose file
sudo vi docker-compose.yml
version: '3.9'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
- ./conf.d:/etc/nginx/conf.d
restart: unless-stopped
Default Configuration #
sudo vi conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
HTTPS Version #
Docker Compose #
# Create Docker Compose file
sudo vi docker-compose.yml
version: '3.9'
services:
nginx:
image: nginx:latest
ports:
- "443:443"
volumes:
- ./html:/usr/share/nginx/html
- ./conf.d:/etc/nginx/conf.d
- /etc/letsencrypt:/etc/letsencrypt
restart: unless-stopped
Note: Alternative a Virtual Host configuration can also be directly mounted.
version: '3.9'
services:
nginx:
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
Virtual Host Configuration #
# Create Virtual Host configuration
sudo vi /opt/nginx/conf.d/mywebsite.jklug.work
server {
listen 443 ssl;
server_name mywebsite.jklug.work;
ssl_certificate /etc/letsencrypt/live/mywebsite.jklug.work/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.jklug.work/privkey.pem;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
HTTPS with Custom Logs #
Docker Compose #
version: '3.9'
services:
nginx:
image: nginx:latest
ports:
- "443:443"
volumes:
- ./html:/usr/share/nginx/html
- ./conf.d:/etc/nginx/conf.d
- ./custom-logs:/var/log/nginx/custom-logs
- /etc/letsencrypt:/etc/letsencrypt
restart: unless-stopped
Virtual Host Configuration #
# Create Virtual Host configuration
sudo vi /opt/nginx/conf.d/mywebsite.jklug.work
server {
listen 443 ssl;
server_name mywebsite.jklug.work;
ssl_certificate /etc/letsencrypt/live/mywebsite.jklug.work/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.jklug.work/privkey.pem;
access_log /var/log/nginx/custom-logs/website-access.log;
error_log /var/log/nginx/custom-logs/websiteerror.log;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Reverse Proxy for Apache2 #
Docker Network #
# Create external Docker network
sudo docker network create nginx
Docker Compose #
# Create Docker Compose file
sudo vi docker-compose.yml
version: '3.9'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./custom-logs:/var/log/nginx/custom-logs
- /etc/letsencrypt:/etc/letsencrypt
networks:
- nginx
restart: unless-stopped
networks:
nginx:
external: true
Virtual Host Configuration #
# Create Virtual Host configuration
sudo vi /opt/nginx/conf.d/mywebsite.jklug.work
server {
listen 80;
server_name mywebsite.jklug.work;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name mywebsite.jklug.work;
ssl_certificate /etc/letsencrypt/live/mywebsite.jklug.work/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.jklug.work/privkey.pem;
access_log /var/log/nginx/custom-logs/website-access.log;
error_log /var/log/nginx/custom-logs/website-error.log;
location / {
proxy_pass http://webserver:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Start Docker Container #
# Start Docker container
sudo docker compose up -d
Apache2 Prerequisites #
Folder Structure #
# Create folders
sudo mkdir -p /opt/apache2/html && cd /opt/apache2
# Set permissions
sudo chmod 755 /opt/apache2/html
HTML Site #
# Create HTML site
sudo vi /opt/apache2/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>jklug.work</title>
</head>
<body>
<h1>Apache2 Docker Compose</h1>
</body>
</html>
PHP Site #
# Create index.php file
sudo vi /opt/apache2/html/index.php
# /opt/apache2/html/index.php
<?php
phpinfo();
?>
Apache2 Overview #
Default Configuration #
Docker Compose #
# Create Docker Compose file
sudo vi docker-compose.yml
version: '3.9'
services:
webserver:
image: httpd:latest
ports:
- "80:80"
volumes:
- ./html:/usr/local/apache2/htdocs/
restart: unless-stopped
- Adaption for Nginx Reverse Proxy
version: '3.9'
services:
webserver:
image: httpd:latest
ports:
- "80"
volumes:
- ./html:/usr/local/apache2/htdocs/
networks:
- nginx
restart: unless-stopped
networks:
nginx:
external: true
PHP Image with Apache2 #
Docker Compose #
# Create Docker Compose file
sudo vi docker-compose.yml
version: '3.9'
services:
webserver:
image: php:8.3-apache
ports:
- "80:80"
volumes:
- ./html:/var/www/html/
restart: unless-stopped
- Adaption for Nginx Reverse Proxy
version: '3.9'
services:
webserver:
image: php:8.3-apache
ports:
- "80"
volumes:
- ./html:/var/www/html/
networks:
- nginx
restart: unless-stopped
networks:
nginx:
external: true
Start Docker Container #
# Start Docker container
sudo docker compose up -d
Links #
# DockerHub Nginx
https://hub.docker.com/_/nginx
# DockerHub Apache
https://hub.docker.com/_/httpd
# DockerHub PHP with Apache
https://hub.docker.com/_/php