Nginx ist einer der beliebtesten Webserver weltweit - schnell, ressourcenschonend und vielseitig. In dieser Anleitung installieren und konfigurieren wir Nginx von Grund auf.
Installation
Ubuntu/Debian
apt update
apt install nginx
systemctl enable nginx
systemctl start nginxInstallation prüfen
systemctl status nginx
nginx -vÖffnen Sie http://ihre-server-ip im Browser - Sie sollten die Nginx-Willkommensseite sehen.
Wichtige Verzeichnisse
| Pfad | Inhalt | |------|--------| | /etc/nginx/ | Konfigurationsdateien | | /etc/nginx/nginx.conf | Hauptkonfiguration | | /etc/nginx/sites-available/ | Verfügbare Server Blocks | | /etc/nginx/sites-enabled/ | Aktivierte Server Blocks | | /var/www/html/ | Standard-Webroot | | /var/log/nginx/ | Log-Dateien |
Erster Server Block (Virtual Host)
Verzeichnis erstellen
mkdir -p /var/www/example.com
chown -R www-data:www-data /var/www/example.comTest-Seite erstellen
echo "<h1>Willkommen auf example.com</h1>" > /var/www/example.com/index.htmlServer Block konfigurieren
nano /etc/nginx/sites-available/example.comInhalt:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# Logs
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}Aktivieren
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
nginx -t # Konfiguration testen
systemctl reload nginxSSL mit Let's Encrypt
apt install certbot python3-certbot-nginx
certbot --nginx -d example.com -d www.example.comCertbot passt die Nginx-Konfiguration automatisch an und richtet HTTPS ein.
PHP mit Nginx (PHP-FPM)
PHP-FPM installieren
apt install php-fpm php-mysqlServer Block für PHP
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}Testen
echo "<?php phpinfo(); ?>" > /var/www/example.com/info.phpRufen Sie http://example.com/info.php auf. Nach dem Test: rm /var/www/example.com/info.php
Reverse Proxy
Nginx kann als Reverse Proxy vor anderen Anwendungen stehen:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_cache_bypass $http_upgrade;
}
}Performance-Optimierung
Gzip-Komprimierung
In /etc/nginx/nginx.conf:
http {
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss;
}Browser-Caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}Worker-Prozesse
In /etc/nginx/nginx.conf:
worker_processes auto;
worker_connections 1024;Sicherheitsheader
server {
# ...
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
}Häufige Befehle
# Konfiguration testen
nginx -t
# Reload (ohne Downtime)
systemctl reload nginx
# Neustart
systemctl restart nginx
# Logs anzeigen
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.logTroubleshooting
"Address already in use"
Anderer Dienst blockiert Port 80/443:
ss -tulpn | grep :80502 Bad Gateway
PHP-FPM läuft nicht oder Socket falsch:
systemctl status php8.1-fpm
ls /run/php/403 Forbidden
Berechtigungen prüfen:
ls -la /var/www/example.com
namei -l /var/www/example.com/index.htmlFazit
Nginx ist schnell installiert und konfiguriert. Die Server-Block-Struktur macht die Verwaltung mehrerer Websites einfach. Für beste Performance: Gzip aktivieren, Browser-Caching nutzen, und für PHP unbedingt PHP-FPM statt mod_php verwenden.