Zum Inhalt springen

Lösung: Nginx Webserver

nginx-lab/
├── docker-compose.yml
├── html/
│ ├── index.html
│ └── about.html
└── conf/ # Optional
└── default.conf
docker-compose.yml
services:
nginx:
image: nginx:alpine
container_name: nginx-lab
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html:ro
docker-compose.yml
services:
nginx:
image: nginx:alpine
container_name: nginx-lab
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html:ro
- ./conf/default.conf:/etc/nginx/conf.d/default.conf:ro
Terminal-Fenster
$ docker compose up -d
[+] Running 2/2
Network nginx-lab_default Created
Container nginx-lab Started

Bei http://localhost:8080 sollte die HTML-Seite erscheinen.

Terminal-Fenster
$ docker compose logs nginx
nginx-lab | 172.17.0.1 - - [10/Mar/2024:14:30:00 +0000] "GET / HTTP/1.1" 200 1234 "-" "Mozilla/5.0..."
nginx-lab | 172.17.0.1 - - [10/Mar/2024:14:30:01 +0000] "GET /about.html HTTP/1.1" 200 456 "-" "Mozilla/5.0..."
ImageBasisGrösseInhalt
nginx:latestDebian~140 MBVollständiges Debian
nginx:alpineAlpine Linux~40 MBMinimales Linux

Alpine ist kleiner und sicherer (weniger Angriffsfläche), aber manche Tools fehlen (z.B. bash → nur sh).

  • Sicherheit: Container kann die Dateien nicht verändern
  • Best Practice: Nur Schreibrechte geben wo nötig
  • Fehlerprävention: Verhindert versehentliche Änderungen

Ohne :ro könnte ein kompromittierter Container die HTML-Dateien manipulieren.

ports:
- "80:80"
  • Auf Linux: Ports unter 1024 benötigen Root-Rechte
  • Auf macOS/Windows: Funktioniert, aber Port 80 könnte belegt sein
  • Konflikt: Andere Webserver (Apache, IIS) nutzen oft Port 80

Option A: Mit selbstsigniertem Zertifikat

Terminal-Fenster
# Zertifikat erstellen
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout ssl/nginx.key -out ssl/nginx.crt
conf/default.conf
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
root /usr/share/nginx/html;
}
server {
listen 80;
return 301 https://$host$request_uri;
}
docker-compose.yml
services:
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./html:/usr/share/nginx/html:ro
- ./conf/default.conf:/etc/nginx/conf.d/default.conf:ro
- ./ssl:/etc/nginx/ssl:ro

Option B: Mit Let’s Encrypt (Produktion)

Für echte Zertifikate: Certbot oder Traefik als Reverse Proxy verwenden.

Nginx vor anderen Containern (z.B. Node.js App):

docker-compose.yml
services:
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./conf/proxy.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- app
app:
image: node:20-alpine
working_dir: /app
volumes:
- ./app:/app
command: node server.js
expose:
- "3000"
conf/proxy.conf
server {
listen 80;
location / {
proxy_pass http://app:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}