Installation Nginx
Die letzten 15 Jahre habe ich mit Apache (prefork) gearbeitet. Da meine Seiten fast ausschliesslich auf PHP basieren und die Anforderung bezüglich der Performance mit dem Erfolg der einzelnen Webseiten immer höher wurde, wollte ich auf Apache Worker MPM wechseln. Beim Tuning und der Konfiguration bin ich dann jedoch auf einige Probleme gestossen. Bei meiner Suche nach Antworten ist immer wieder der Name „Nginx“ aufgetaucht. Nach längerer Recherche hab ich mich entschlossen, den „Elefanten“ Apache fallen zu lassen und auf den leichten und schnellen Webserver Nginx (ausgesprochen Engine-X) zu wechseln.
Für die beste Performance wird der Webserver mit folgenden Komponenten gebaut:
Installation
pkg install nginx
Nun die gewünschten Optionen / Module aktivieren:
lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk x Options for nginx 1.0.11,1 x x lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk x x x[*] HTTP_MODULE Enable HTTP module x x x x[*] HTTP_CACHE_MODULE Enable http_cache module x x x x[*] HTTP_GZIP_STATIC_MODULE Enable http_gzip_static module x x <- analog zu mod_defalte (apache) x x[*] HTTP_REWRITE_MODULE Enable http_rewrite module x x x x[*] HTTP_SSL_MODULE Enable http_ssl module x x x x[*] HTTP_STATUS_MODULE Enable http_stub_status module x x x x[*] WWW Enable html sample files x x x mqqqqv(+)qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq18%qqqqqj x tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu x < OK > <Cancel> x mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
WWW könnte man natürlich auch weglassen. Ich lasse es aber mal drin, da es meine ersten Gehversuche mit Nginx sind 😉
Nun rc.conf anpassen:
vi /etc/rc.conf
Und folgendes einfügen:
#-----------------------------------------------# # Web Server # #-----------------------------------------------# nginx_enable="YES"
Alias erstellen
Damit das handling etwas schneller geht ein Alias erstellen 😉
alias nxctl='/usr/local/etc/rc.d/nginx'
und diese Zeile jeweils auch noch in den entsprechenden .bashrc Files in allen Home Directories hinzufügen.
Nun Webserver starten:
/usr/local/etc/rc.d/nginx start
bzw.
nxctl start
Konfiguration
Verzeichnisse Erstellen
cd /www/server ln -s /usr/local/etc/nginx nginx mkdir VirtualHosts
nginx.conf
Nun die VirtualHosts im ConfigFile includen:
vi /www/server/nginx/nginx.conf
Folgendes ergänzen innerhalb http:
http { {...} # virtual hosts include /www/server/VirtualHosts/*; }
Virtual Hosts
Nun im Verzeichnis
/www/server/VirtualHosts/
pro VirtualHost ein File erstellen.
Configtest
# nginx -c /usr/local/etc/nginx/nginx.conf -t
oder
/usr/local/etc/rc.d/nginx configtest
Konvertiere Apache -> Nginx Konfigurationen
ServerAlias
Apache:
<VirtualHost *:80> ServerName example.com ServerAlias www1.example.com ServerAlias www2.example.com </VirtualHost>
Nginx:
server { server_name example.com www1.example.com www2.example.com; }
ExpiresDefault
Apache:
ExpiresDefault "access plus 3 day"
Nginx:
expires 3d;
mod_deflate
Apache:
<IfModule mod_deflate.c> # Insert filter SetOutputFilter DEFLATE # Netscape 4.x has some problems BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine # BrowserMatch bMSIE !no-gzip !gzip-only-text/html # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48 # the above regex won't work. You can use the following # workaround to get the desired effect: BrowserMatch bMSI[E] !no-gzip !gzip-only-text/html # Don't compress images SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary # or pdfs SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary # or binary archives SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar|iso|dia)$ no-gzip dont-vary # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary # logging #DeflateFilterNote ratio #LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate #CustomLog logs/deflate_log deflate </IfModule>
Nginx:
gzip on; gzip_http_version 1.1; gzip_comp_level 6; gzip_min_length 1100; gzip_buffers 4 8k; gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascr$ gzip_proxied any; gzip_disable "MSIE [1-6].";
Rewrite Rules
Hier eine kleine Übersicht über das Konvertieren von Rewrite Rules.
.htaccess
.htaccess wird von nginx nicht unterstützt. Hier steht weshalb. Bei einem Wechsel von Apache -> Nginx müssen daher alle bestehenden .htaccess Inhalte in der Konfiguration von nginx eingetragen werden.
Also zuerst alle .htaccess Dateien suchen:
# find /www/ -name „.htaccess“
Und jetzt abwägen, was gemacht werden muss und was nicht. Wenn man knapp 300 .htaccess Dateien konvertieren sollte lohnt sich das abwägen 😉
mod_auth kann man wie folgt ersetzen:
.htaccess Version (befand sich in diesem Beispiel im /admin Verzeichnis)
AuthUserFile /www/example.com/etc/admin.pwd AuthGroupFile /dev/null AuthName Member-Bereich AuthType Basic require valid-user order deny,allow deny from all allow from all
nginx.conf
location /admin/ { auth_basic "Restricted"; auth_basic_user_file /www/example.com/etc/admin.pwd; }
htpasswd
Um ohne Apache das Passwort für das auth_basic_user_file zu erstellen, kann man folgenden Befehl verwenden:
printf "<USERNAME>:`openssl passwd -apr1`n" >> .htpasswd
falls die Option -apr1 nicht verfügbar sein sollte, kann stattdessen auch -crypt verwendet werden
Tipps & Tricks bei der Konfiguration
Web-Verzeichnis auf ein anderes Root Verzeichnis verweisen.
Ich möchte, wenn ich
https://www.domain1.com/stat/matomo.php
aufrufe, auf ein anderes Root Verzeichnis von domain2.com pointen.
https://www.domain2.com/matomo.php
Ausserdem möchte ich das /stat/
Verzeichnis beim Verweis gelöscht haben. Und das ganze unsichtbar, also ohne Redirect.
Das Root Verzeichnis von DOMAIN1 ist:
/www/domain1.com/doc
Das „stat“ Verzeichnis existiert nicht! Es wird nur für die interne Weiterleitung benötigt.
das Root Verzeichnis von DOMAIN2 ist:
/www/domain2.com/analyze
Um jetzt Server-Intern korrekt umgeleitet zu werden, kann es so gelöst werden:
server { listen 443 ssl; ssl on; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; server_name domain1.com; root /www/domain1.com/doc; ## BOF MATOMO internal tracking config # wir tracken im /stat/ verzeichnis. dieses wird auf matomo umgeleitet. # zugriff gibt es nur auf das matomo.php file. alle anderen php files # ist deny all. man kann sich also ueber den fabulous link nicht auf matomo einloggen. location ^~ /stat { rewrite ^/stat(.*)$ /analyze$1 last; } location ^~ /analyze { internal; root /www/domain2.com/; #location ~ .(php|php3|inc) { location ~ /matomo.php { # php73 fastcgi_pass 127.0.0.1:9073; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } location ~* .(js|css|png|jpg|jpeg|gif|ico)$ { expires 3d; } # deny everything that doesn't match another location location ~ / { deny all; } } ## EOF MATOMO internal tracking config
Bei dieser Konfig ist ausschliesslich Zugriff auf matomo.php sowie Java Script Files / Images erlaubt. Alles andere wird denied.