Server:Nginx Einstellung: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
// via Wikitext Extension for VSCode Markierung: Zurückgesetzt |
Keine Bearbeitungszusammenfassung Markierungen: Manuelle Zurücksetzung Visuelle Bearbeitung |
||
Zeile 9: | Zeile 9: | ||
<pre> | <pre> | ||
/** | |||
* NGINX-Konfigurationsdatei für mehrere virtuelle Hosts mit SSL-Unterstützung. | |||
* | |||
* Enthält folgende Server-Blöcke: | |||
* | |||
* 1. ahrensburg.city & www.ahrensburg.city (Drupal): | |||
* - Lauscht auf Port 443 mit SSL und HTTP/2. | |||
* - Verwendet Let's Encrypt-Zertifikate. | |||
* - Setzt Sicherheitsheader (X-Frame-Options, X-Content-Type-Options, X-XSS-Protection). | |||
* - Aktiviert Gzip-Komprimierung für verschiedene Dateitypen. | |||
* - Schützt sensible Verzeichnisse und Dateien (z.B. private Dateien, .php in /sites/files). | |||
* - Leitet Anfragen über try_files an index.php weiter (Drupal-typisch). | |||
* - Spezielle Behandlung für statische Assets (js, css, Bilder) mit Caching. | |||
* - FastCGI-Konfiguration für PHP über php8.3-fpm. | |||
* | |||
* 2. wiki.ahrensburg.city (MediaWiki): | |||
* - Lauscht auf Port 443 mit SSL und HTTP/2 (IPv4 & IPv6). | |||
* - Root-Verzeichnis: /var/www/mediawiki. | |||
* - Alias für /karte-Verzeichnis mit Autoindex. | |||
* - Reverse Proxy für /hot auf lokalen Dienst (Port 8080). | |||
* - Standard-URL-Rewriting für MediaWiki. | |||
* - FastCGI für PHP-Dateien. | |||
* - Schutz vor Zugriff auf .ht*-Dateien. | |||
* | |||
* 3. alterwiki.ahrensburg.city: | |||
* - Wie wiki.ahrensburg.city, aber Root: /var/www/alterwiki. | |||
* - Gleiches URL-Rewriting und PHP-Handling. | |||
* - Schutz vor .ht*-Dateien. | |||
* | |||
* 5. HTTP-zu-HTTPS-Redirect: | |||
* - Leitet alle Anfragen auf Port 80 (IPv4 & IPv6) für ahrensburg.city und www.ahrensburg.city per 301-Redirect auf HTTPS um. | |||
* | |||
* Hinweise: | |||
* - Alle Server verwenden die gleichen SSL-Zertifikate. | |||
* - PHP wird über Unix-Socket an php8.3-fpm weitergeleitet. | |||
* - Die Konfiguration ist für produktive Umgebungen optimiert (Sicherheit, Performance, Redirects). | |||
*/ | |||
server { | |||
listen 443 ssl http2; | |||
server_name ahrensburg.city www.ahrensburg.city; | |||
root /var/www/drupal/web; | |||
index index.php index.html; | |||
ssl_certificate /etc/letsencrypt/live/ahrensburg.city/fullchain.pem; | |||
ssl_certificate_key /etc/letsencrypt/live/ahrensburg.city/privkey.pem; | |||
ssl_protocols TLSv1.2 TLSv1.3; | |||
ssl_ciphers HIGH:!aNULL:!MD5; | |||
# Sicherheitsheader | |||
add_header X-Frame-Options "SAMEORIGIN"; | |||
add_header X-Content-Type-Options "nosniff"; | |||
add_header X-XSS-Protection "1; mode=block"; | |||
# Gzip-Komprimierung | |||
gzip on; | |||
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; | |||
location = /favicon.ico { log_not_found off; access_log off; } | |||
location = /robots.txt { allow all; log_not_found off; access_log off; } | |||
location ~ \..*/.*\.php$ { return 403; } | |||
location ~ ^/sites/.*/private/ { return 403; } | |||
location ~ ^/sites/[^/]+/files/.*\.php$ { deny all; } | |||
location ~ (^|/)\. { return 403; } | |||
location / { | |||
try_files $uri /index.php?$query_string; | |||
} | |||
location @rewrite { | |||
rewrite ^/(.*)$ /index.php?q=$1; | |||
} | |||
location ~ /vendor/.*\.php$ { | |||
deny all; | |||
return 404; | |||
} | |||
location ~ '\.php$|^/update.php' { | |||
include fastcgi_params; | |||
fastcgi_split_path_info ^(.+?\.php)(/.*)?$; | |||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |||
fastcgi_param PATH_INFO $fastcgi_path_info; | |||
fastcgi_param HTTP_PROXY ""; | |||
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; | |||
fastcgi_intercept_errors on; | |||
} | |||
location ~ ^/sites/.*/files/styles/ { | |||
try_files $uri @rewrite; | |||
} | |||
location ~ ^(/[a-z\-]+)?/system/files/ { | |||
try_files $uri /index.php?$query_string; | |||
} | |||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { | |||
try_files $uri @rewrite; | |||
expires max; | |||
log_not_found off; | |||
} | |||
} | |||
server { | server { | ||
listen 443 ssl http2; | listen 443 ssl http2; | ||
Zeile 50: | Zeile 155: | ||
ssl_certificate_key /etc/letsencrypt/live/ahrensburg.city/privkey.pem; | ssl_certificate_key /etc/letsencrypt/live/ahrensburg.city/privkey.pem; | ||
root /var/www/alterwiki; | root /var/www/alterwiki; | ||
index index.php index.html index.htm; | |||
location / { | |||
try_files $uri $uri/ /index.php?$args; | |||
} | |||
location ~ \.php$ { | |||
include snippets/fastcgi-php.conf; | |||
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; | |||
} | |||
location ~ /\.ht { | |||
deny all; | |||
} | |||
} | |||
server { | |||
listen 443 ssl http2; | |||
listen [::]:443 ssl http2; | |||
server_name stadtwiki.ahrensburg.city; | |||
ssl_certificate /etc/letsencrypt/live/ahrensburg.city/fullchain.pem; | |||
ssl_certificate_key /etc/letsencrypt/live/ahrensburg.city/privkey.pem; | |||
root /var/www/stadtwiki; | |||
index index.php index.html index.htm; | index index.php index.html index.htm; | ||
Aktuelle Version vom 20. Juni 2025, 07:26 Uhr
Erstellen Sie eine neue Konfigurationsdatei
Um eine neue Konfigurationsdatei für Nginx zu erstellen, öffnen Sie ein Terminal und geben Sie folgenden Befehl ein:
sudo nano /etc/nginx/conf.d/start.conf
Dies öffnet den Texteditor `nano` mit Root-Rechten und erstellt (oder bearbeitet) die Datei `start.conf` im Verzeichnis `/etc/nginx/conf.d/`. In dieser Datei können Sie Ihre gewünschten Nginx-Konfigurationen eintragen. Nach dem Bearbeiten speichern Sie die Datei mit `Strg + O`, bestätigen mit `Enter` und schließen den Editor mit `Strg + X`.
Folgende Text eingeben in Datei
/** * NGINX-Konfigurationsdatei für mehrere virtuelle Hosts mit SSL-Unterstützung. * * Enthält folgende Server-Blöcke: * * 1. ahrensburg.city & www.ahrensburg.city (Drupal): * - Lauscht auf Port 443 mit SSL und HTTP/2. * - Verwendet Let's Encrypt-Zertifikate. * - Setzt Sicherheitsheader (X-Frame-Options, X-Content-Type-Options, X-XSS-Protection). * - Aktiviert Gzip-Komprimierung für verschiedene Dateitypen. * - Schützt sensible Verzeichnisse und Dateien (z.B. private Dateien, .php in /sites/files). * - Leitet Anfragen über try_files an index.php weiter (Drupal-typisch). * - Spezielle Behandlung für statische Assets (js, css, Bilder) mit Caching. * - FastCGI-Konfiguration für PHP über php8.3-fpm. * * 2. wiki.ahrensburg.city (MediaWiki): * - Lauscht auf Port 443 mit SSL und HTTP/2 (IPv4 & IPv6). * - Root-Verzeichnis: /var/www/mediawiki. * - Alias für /karte-Verzeichnis mit Autoindex. * - Reverse Proxy für /hot auf lokalen Dienst (Port 8080). * - Standard-URL-Rewriting für MediaWiki. * - FastCGI für PHP-Dateien. * - Schutz vor Zugriff auf .ht*-Dateien. * * 3. alterwiki.ahrensburg.city: * - Wie wiki.ahrensburg.city, aber Root: /var/www/alterwiki. * - Gleiches URL-Rewriting und PHP-Handling. * - Schutz vor .ht*-Dateien. * * 5. HTTP-zu-HTTPS-Redirect: * - Leitet alle Anfragen auf Port 80 (IPv4 & IPv6) für ahrensburg.city und www.ahrensburg.city per 301-Redirect auf HTTPS um. * * Hinweise: * - Alle Server verwenden die gleichen SSL-Zertifikate. * - PHP wird über Unix-Socket an php8.3-fpm weitergeleitet. * - Die Konfiguration ist für produktive Umgebungen optimiert (Sicherheit, Performance, Redirects). */ server { listen 443 ssl http2; server_name ahrensburg.city www.ahrensburg.city; root /var/www/drupal/web; index index.php index.html; ssl_certificate /etc/letsencrypt/live/ahrensburg.city/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ahrensburg.city/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; # Sicherheitsheader add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; add_header X-XSS-Protection "1; mode=block"; # Gzip-Komprimierung gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location ~ \..*/.*\.php$ { return 403; } location ~ ^/sites/.*/private/ { return 403; } location ~ ^/sites/[^/]+/files/.*\.php$ { deny all; } location ~ (^|/)\. { return 403; } location / { try_files $uri /index.php?$query_string; } location @rewrite { rewrite ^/(.*)$ /index.php?q=$1; } location ~ /vendor/.*\.php$ { deny all; return 404; } location ~ '\.php$|^/update.php' { include fastcgi_params; fastcgi_split_path_info ^(.+?\.php)(/.*)?$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param HTTP_PROXY ""; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; fastcgi_intercept_errors on; } location ~ ^/sites/.*/files/styles/ { try_files $uri @rewrite; } location ~ ^(/[a-z\-]+)?/system/files/ { try_files $uri /index.php?$query_string; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { try_files $uri @rewrite; expires max; log_not_found off; } } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name wiki.ahrensburg.city; ssl_certificate /etc/letsencrypt/live/ahrensburg.city/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ahrensburg.city/privkey.pem; root /var/www/mediawiki; index index.php index.html index.htm; location /karte { alias /var/www/karte; autoindex on; } location /hot { proxy_pass http://localhost:8080/; 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; } location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; } location ~ /\.ht { deny all; } } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name alterwiki.ahrensburg.city; ssl_certificate /etc/letsencrypt/live/ahrensburg.city/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ahrensburg.city/privkey.pem; root /var/www/alterwiki; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; } location ~ /\.ht { deny all; } } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name stadtwiki.ahrensburg.city; ssl_certificate /etc/letsencrypt/live/ahrensburg.city/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ahrensburg.city/privkey.pem; root /var/www/stadtwiki; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; } location ~ /\.ht { deny all; } } server { listen 80; listen [::]:80; server_name ahrensburg.city www.ahrensburg.city; return 301 https://$host$request_uri; }
Hinweis:
- Nach dem Speichern und Schließen der Datei müssen Sie die Nginx-Konfiguration neu laden, damit die Änderungen wirksam werden:
sudo nginx -t sudo systemctl reload nginx
- Prüfen Sie mit
sudo nginx -t
immer zuerst, ob die Konfiguration fehlerfrei ist. - Weitere Informationen finden Sie in der offiziellen Nginx-Dokumentation.