# nginx — Webserver, Reverse Proxy und Load Balancer

> Praxis-Guide zu nginx — Service-Verwaltung, CLI-Befehle, Configtest, Virtual Hosts, TLS mit Certbot und Log-Analyse auf Debian/Ubuntu und RHEL.

Source: https://www.jpkc.com/db/cheatsheets/web-servers/nginx/

<!-- PROSE:intro -->
NGINX ist einer der meistgenutzten Webserver im Netz und glänzt überall dort, wo viele gleichzeitige Verbindungen auf wenig Ressourcen treffen: Dank seiner event-getriebenen Architektur bedient ein einzelner Worker-Prozess Tausende Verbindungen parallel. Über die reine Auslieferung statischer Dateien hinaus dient NGINX als Reverse Proxy, Load Balancer und TLS-Terminator vor deinen Anwendungen. Die Steuerung läuft über das schlanke `nginx`-Kommando und – auf systemd-Distributionen – über `systemctl`. Dieser Guide bündelt die Befehle für den Alltag: Service-Verwaltung, Configtest, Virtual Hosts, Zertifikate mit Certbot und Log-Analyse.
<!-- PROSE:intro:end -->

## Service-Verwaltung (systemctl)

`systemctl start nginx` — Startet den NGINX-Webserver.

```bash
systemctl start nginx
```

`systemctl stop nginx` — Stoppt den NGINX-Webserver sofort.

```bash
systemctl stop nginx
```

`systemctl restart nginx` — Startet NGINX neu (Stopp + Start). Trennt kurzzeitig alle aktiven Verbindungen.

```bash
systemctl restart nginx
```

`systemctl reload nginx` — Lädt die NGINX-Konfiguration unterbrechungsfrei neu, ohne aktive Verbindungen zu trennen.

```bash
systemctl reload nginx
```

`systemctl status nginx` — Zeigt Service-Status, PID, Laufzeit und die letzten Logausgaben von NGINX.

```bash
systemctl status nginx
```

`systemctl enable nginx` — Aktiviert den automatischen Start von NGINX beim Systemstart.

```bash
systemctl enable nginx
```

`systemctl disable nginx` — Deaktiviert den automatischen Start von NGINX beim Booten.

```bash
systemctl disable nginx
```

`systemctl is-active nginx` — Prüft, ob NGINX gerade läuft. Gibt 'active' oder 'inactive' zurück.

```bash
systemctl is-active nginx
```

## nginx-CLI-Befehle

`nginx -t` — Prüft die NGINX-Konfiguration auf Syntaxfehler. Immer vor einem Reload ausführen.

```bash
nginx -t
```

`nginx -T` — Prüft die Konfiguration und gibt die komplette aufgelöste Konfiguration auf stdout aus.

```bash
nginx -T
```

`nginx -s reload` — Sendet das Reload-Signal an den laufenden NGINX-Master-Prozess (unterbrechungsfreier Config-Reload).

```bash
nginx -s reload
```

`nginx -s stop` — Sendet das Stop-Signal an NGINX (schnelles Herunterfahren, trennt Verbindungen sofort).

```bash
nginx -s stop
```

`nginx -s quit` — Sendet das Quit-Signal an NGINX (sauberes Herunterfahren, wartet auf das Ende laufender Requests).

```bash
nginx -s quit
```

`nginx -s reopen` — Öffnet die Logdateien neu. Nach einer Logrotation, um in die neue Datei zu schreiben.

```bash
nginx -s reopen
```

`nginx -v` — Zeigt die NGINX-Version.

```bash
nginx -v
```

`nginx -V` — Zeigt NGINX-Version, Compiler-Flags und alle einkompilierten Module.

```bash
nginx -V
```

`nginx -c /path/to/nginx.conf` — Startet NGINX mit einer bestimmten Konfigurationsdatei statt der Standarddatei.

```bash
nginx -c /etc/nginx/nginx.conf
```

`nginx -p /path/to/prefix` — Setzt den NGINX-Prefix-Pfad (zum Auflösen relativer Pfade in der Konfiguration).

```bash
nginx -p /etc/nginx
```

`nginx -t && systemctl reload nginx` — Validiert die Konfiguration und lädt nur bei fehlerfreiem Test neu (empfohlener Workflow).

```bash
nginx -t && systemctl reload nginx
```

## Prozess-Verwaltung

`ps aux | grep nginx` — Listet alle laufenden NGINX-Prozesse auf (Master- und Worker-Prozesse).

```bash
ps aux | grep nginx
```

`cat /var/run/nginx.pid` — Zeigt die PID des NGINX-Master-Prozesses.

```bash
cat /var/run/nginx.pid
```

`kill -HUP $(cat /var/run/nginx.pid)` — Sendet das HUP-Signal an den Master-Prozess, um die Konfiguration unterbrechungsfrei neu zu laden.

```bash
kill -HUP $(cat /var/run/nginx.pid)
```

`kill -USR1 $(cat /var/run/nginx.pid)` — Öffnet die Logdateien neu per USR1-Signal (entspricht nginx -s reopen).

```bash
kill -USR1 $(cat /var/run/nginx.pid)
```

`kill -WINCH $(cat /var/run/nginx.pid)` — Fährt die Worker-Prozesse sauber herunter (der Master bleibt aktiv, genutzt bei Upgrades).

```bash
kill -WINCH $(cat /var/run/nginx.pid)
```

## Virtual Hosts (Debian/Ubuntu)

`ls /etc/nginx/sites-available/` — Listet alle verfügbaren Virtual-Host-Konfigurationsdateien auf.

```bash
ls /etc/nginx/sites-available/
```

`ls /etc/nginx/sites-enabled/` — Listet alle aktuell aktivierten Virtual-Host-Konfigurationen auf (Symlinks).

```bash
ls /etc/nginx/sites-enabled/
```

`ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/` — Aktiviert einen Virtual Host durch Anlegen eines Symlinks in sites-enabled/.

```bash
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
```

`rm /etc/nginx/sites-enabled/example.com` — Deaktiviert einen Virtual Host durch Entfernen des Symlinks aus sites-enabled/.

```bash
rm /etc/nginx/sites-enabled/example.com
```

`ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ && nginx -t && systemctl reload nginx` — Aktiviert eine Site, validiert die Konfiguration und lädt NGINX in einem Schritt neu.

```bash
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ && nginx -t && systemctl reload nginx
```

`nginx -T | grep server_name` — Listet alle konfigurierten server_name-Werte über alle Virtual Hosts hinweg.

```bash
nginx -T | grep server_name
```

## Konfigurationsverzeichnisse

`/etc/nginx/nginx.conf` — Haupt-Konfigurationsdatei von NGINX. Bindet sites-enabled/ und conf.d/ ein.

```bash
nano /etc/nginx/nginx.conf
```

`/etc/nginx/sites-available/` — Verzeichnis für Virtual-Host-Konfigurationsdateien (Debian/Ubuntu). Erst nach Symlink aktiv.

```bash
ls /etc/nginx/sites-available/
```

`/etc/nginx/sites-enabled/` — Verzeichnis mit Symlinks auf aktive Virtual-Host-Konfigurationen (Debian/Ubuntu).

```bash
ls /etc/nginx/sites-enabled/
```

`/etc/nginx/conf.d/` — Drop-in-Konfigurationsverzeichnis. Alle *.conf-Dateien hier werden automatisch eingebunden.

```bash
ls /etc/nginx/conf.d/
```

`/etc/nginx/snippets/` — Wiederverwendbare Konfigurations-Snippets (z. B. ssl-params.conf, fastcgi-php.conf).

```bash
ls /etc/nginx/snippets/
```

`/var/log/nginx/` — Standard-Logverzeichnis mit access.log und error.log.

```bash
ls /var/log/nginx/
```

## Logs

`tail -f /var/log/nginx/access.log` — Verfolgt das NGINX-Access-Log in Echtzeit.

```bash
tail -f /var/log/nginx/access.log
```

`tail -f /var/log/nginx/error.log` — Verfolgt das NGINX-Error-Log in Echtzeit.

```bash
tail -f /var/log/nginx/error.log
```

`tail -f /var/log/nginx/error.log | grep 'crit\|emerg\|alert'` — Beobachtet das Error-Log und filtert nur Meldungen auf kritischer Stufe.

```bash
tail -f /var/log/nginx/error.log | grep 'crit\|emerg'
```

`grep ' 500 \| 502 \| 503 ' /var/log/nginx/access.log` — Findet 5xx-Server-Fehlerantworten im Access-Log.

```bash
grep ' 500 \| 502 \| 503 ' /var/log/nginx/access.log
```

`awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20` — Zeigt die Top 20 IP-Adressen nach Anzahl der Requests aus dem Access-Log.

```bash
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20
```

`nginx -t 2>&1 | grep -v 'successful'` — Führt einen Config-Test aus und zeigt nur Warnungen und Fehler (unterdrückt die OK-Meldung).

```bash
nginx -t 2>&1 | grep -v 'successful'
```

`journalctl -u nginx -f` — Verfolgt die NGINX-Logausgabe über das systemd-Journal (inkl. Start-/Stopp-Ereignisse).

```bash
journalctl -u nginx -f
```

`journalctl -u nginx --since '1 hour ago'` — Zeigt NGINX-Journal-Einträge der letzten Stunde.

```bash
journalctl -u nginx --since '1 hour ago'
```

## SSL / TLS & Certbot

`certbot --nginx -d example.com -d www.example.com` — Bezieht und installiert automatisch ein Let's-Encrypt-Zertifikat für NGINX.

```bash
certbot --nginx -d example.com -d www.example.com
```

`certbot certonly --nginx -d example.com` — Bezieht ein Zertifikat, ohne die NGINX-Konfiguration zu ändern (SSL manuell verwalten).

```bash
certbot certonly --nginx -d example.com
```

`certbot renew --dry-run` — Simuliert den automatischen Erneuerungsprozess, um zu prüfen, ob er erfolgreich wäre.

```bash
certbot renew --dry-run
```

`certbot renew` — Erneuert alle fälligen Zertifikate (per Cron oder systemd-Timer ausführen).

```bash
certbot renew
```

`certbot certificates` — Listet alle verwalteten Let's-Encrypt-Zertifikate und ihre Ablaufdaten auf.

```bash
certbot certificates
```

`openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/self.key -out /etc/nginx/ssl/self.crt` — Erzeugt ein selbstsigniertes SSL-Zertifikat mit 365 Tagen Gültigkeit.

```bash
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
```

`openssl dhparam -out /etc/nginx/dhparam.pem 2048` — Erzeugt eine Diffie-Hellman-Parameterdatei für einen stärkeren SSL-Schlüsselaustausch.

```bash
openssl dhparam -out /etc/nginx/dhparam.pem 2048
```

`openssl s_client -connect example.com:443 -servername example.com` — Testet eine SSL-Verbindung und inspiziert die Zertifikatskette.

```bash
openssl s_client -connect example.com:443 -servername example.com
```

## Debugging & Inspektion

`curl -I http://localhost` — Sendet einen HEAD-Request an das lokale NGINX und inspiziert die Response-Header.

```bash
curl -I http://localhost
```

`curl -I -H 'Host: example.com' http://localhost` — Testet einen bestimmten Virtual Host durch Überschreiben des Host-Headers.

```bash
curl -I -H 'Host: example.com' http://localhost
```

`curl -sk https://localhost -o /dev/null -w '%{http_code}'` — Prüft den HTTP-Statuscode, den NGINX über HTTPS zurückgibt.

```bash
curl -sk https://localhost -o /dev/null -w '%{http_code}'
```

`nginx -T | grep -A5 'server_name example.com'` — Findet den vollständigen Server-Block einer bestimmten Domain in der ausgegebenen Konfiguration.

```bash
nginx -T | grep -A5 'server_name example.com'
```

`ss -tlnp | grep nginx` — Zeigt, auf welchen Ports NGINX lauscht.

```bash
ss -tlnp | grep nginx
```

`strace -p $(cat /var/run/nginx.pid) -e trace=network` — Verfolgt Netzwerk-Systemaufrufe des NGINX-Master-Prozesses für Low-Level-Debugging.

```bash
strace -p $(cat /var/run/nginx.pid) -e trace=network
```

<!-- PROSE:outro -->
## Fazit

NGINX belohnt einen disziplinierten Workflow: Prüfe jede Änderung mit `nginx -t`, bevor du mit `nginx -s reload` oder `systemctl reload nginx` neu lädst – der Reload erfolgt unterbrechungsfrei, sodass aktive Verbindungen nicht abreißen. Nach einer Logrotation sorgt `nginx -s reopen` dafür, dass NGINX in die frischen Logdateien schreibt. Wer Server-, Location- und Upstream-Blöcke feiner abstimmen will, findet die passenden Direktiven in der [Konfigurationsreferenz nginx-conf](https://www.jpkc.com/db/cheatsheets/web-servers/nginx-conf/).

## Weiterführende Links

- [nginx – offizielle Dokumentation](https://nginx.org/en/docs/) – Referenz aller Direktiven und Module (englisch)
- [Nginx – Wikipedia](https://de.wikipedia.org/wiki/Nginx) – Hintergrund und Geschichte
<!-- PROSE:outro:end -->

## Verwandte Kommandos

- [apache](https://www.jpkc.com/db/cheatsheets/web-servers/apache/) – der klassische Webserver mit modularer Konfiguration
- [caddy](https://www.jpkc.com/db/cheatsheets/web-servers/caddy/) – moderner Webserver mit automatischem HTTPS
- [certbot](https://www.jpkc.com/db/cheatsheets/web-servers/certbot/) – Let's-Encrypt-Zertifikate beziehen und erneuern

