Nginx Befehle – Cheatsheet
Schnellreferenz für Nginx-Administration.
🔧 Service verwalten
# Status
sudo systemctl status nginx
nginx -v # Version
# Steuern
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx # Voller Neustart
sudo systemctl reload nginx # Config neu laden (ohne Downtime!)
# Autostart
sudo systemctl enable nginx
sudo systemctl disable nginx
✅ Konfiguration prüfen
# Syntax prüfen (IMMER vor reload!)
sudo nginx -t
# Detaillierte Ausgabe
sudo nginx -T
# Aktive Config anzeigen
sudo nginx -T | grep -A 20 "server {"
📁 Wichtige Pfade
| Pfad |
Beschreibung |
/etc/nginx/nginx.conf |
Hauptkonfiguration |
/etc/nginx/sites-available/ |
Alle Seiten-Configs |
/etc/nginx/sites-enabled/ |
Aktive Seiten (Symlinks) |
/etc/nginx/snippets/ |
Wiederverwendbare Teile |
/var/www/html/ |
Standard Document Root |
/var/log/nginx/access.log |
Zugriffs-Log |
/var/log/nginx/error.log |
Fehler-Log |
🌐 Seiten verwalten
# Neue Seite erstellen
sudo nano /etc/nginx/sites-available/example.com
# Seite aktivieren (Symlink)
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
# Seite deaktivieren
sudo rm /etc/nginx/sites-enabled/example.com
# Config prüfen + neu laden
sudo nginx -t && sudo systemctl reload nginx
📊 Logs & Debugging
# Logs live verfolgen
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
# Beide Logs gleichzeitig
sudo tail -f /var/log/nginx/*.log
# Fehler der letzten Stunde
sudo grep "$(date '+%d/%b/%Y:%H')" /var/log/nginx/error.log
# Bestimmte Domain filtern
sudo grep "example.com" /var/log/nginx/access.log
# Top 10 IPs
sudo cat /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head
# Top 10 URLs
sudo cat /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head
# 404-Fehler finden
sudo grep " 404 " /var/log/nginx/access.log
🔒 SSL mit Let's Encrypt
# Certbot installieren
sudo apt install certbot python3-certbot-nginx -y
# Zertifikat holen (automatisch)
sudo certbot --nginx -d example.com -d www.example.com
# Nur Zertifikat (manuell einbinden)
sudo certbot certonly --nginx -d example.com
# Zertifikate anzeigen
sudo certbot certificates
# Erneuerung testen
sudo certbot renew --dry-run
# Manuell erneuern
sudo certbot renew
📝 Server Block Vorlage
Statische Seite
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Reverse Proxy
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
Mit SSL (nach Certbot)
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/example.com/html;
index index.html;
}
# HTTP → HTTPS Redirect
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
⚡ Nützliche Snippets
Gzip aktivieren
gzip on;
gzip_types text/plain text/css application/json application/javascript;
gzip_min_length 1024;
Statische Dateien cachen
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
Upload-Größe erhöhen
client_max_body_size 100M;
Basic Auth
# Passwort erstellen
sudo apt install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd admin
location /admin {
auth_basic "Admin";
auth_basic_user_file /etc/nginx/.htpasswd;
}
❌ Häufige Fehler
| Fehler |
Ursache |
Lösung |
| 502 Bad Gateway |
Backend nicht erreichbar |
Backend prüfen |
| 403 Forbidden |
Keine Berechtigung |
chown www-data:www-data |
| 404 Not Found |
Datei nicht gefunden |
root Pfad prüfen |
| 413 Too Large |
Upload zu groß |
client_max_body_size |