Letzte Änderung: 07. August 2023
Sinvolle Links
Link : ➞
Nginx location match tester
Link : ➞
If Is Evil
Link : ➞
Pitfalls and Common Mistakes
Reihenfolge der NGINX location blocks
1. location Block mit = Vorzeichen : Wenn die URI den String exakt trifft, verarbeitet NGINX diesen Block. (Beispiel : location = /doc)
2. location Block mit keinem Vorzeichen : Wenn die URI den String exakt trifft, verarbeitet NGINX diesen Block. (Beispiel : location /doc)
3. location Block mit ^~ Vorzeichen : Wenn die URI den beginn des Strings trifft, verarbeitet NGINX diesen Block UND STOPPT DIE SUCHE. (Beispiel : location ^~ /doc oder location ^~ /(abc|xyz))
4. location Block mit ~ oder ~* Vorzeichen : Wenn die URI den String mit REGEX trifft, verarbeitet NGINX diesen Block. (Beispiel : location ~ ^/doc$ oder location ~* ^/doc$)
Zu 4. Gibt es mehrere Blöcke mit ~ oder ~*, wird der Reihenfolge in der Konfiguration nach verarbeitet.
5. location Block mit keinem Vorzeichen : Wenn die URI den beginn des Strings trifft, verarbeitet NGINX diesen Block. (Beispiel : location /doc)
Zu 5. Hier trifft der längere Treffer. Haben wir location mit /doc und /document und der Request ist /documents.html wird der /document Block verarbeitet.
Welche Blöcke werden verarbeitet ?
Beispiel 1 :
Der "/" Request trifft location Block A und location Block B, Block A hat Vorrang und wird verarbeitet.
Der "/document.html" Request trifft location Block B und Block F. Der längere gewinnt, also wird Block F verarbeitet. (siehe oben 5. location Block)
Der "/documents/document.html" Request trifft location Block B, Block C und Block F. Block C ist der längste Treffer und wird verarbeitet.
Der "/images/1.gif" Request trifft location Block D und stoppt die Verarbeitung ! Block D wird verarbeitet.
Der "/documents/1.jpg" Request trifft location Block B, Block C, Block E und Block F. Block E ist Regex, hat Vorrang vor den "Teiltreffern" und wird verarbeitet.
location = / {
[ location Block A ]
}
location / {
[ location Block B ]
}
location /documents/ {
[ location Block C ]
}
location ^~ /images/ {
[ location Block D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ location Block E ]
}
location /doc {
[ location Block F ]
}
Beispiel 2 :
location = / {
# Trifft nur das / Request.
}
location /data/ {
# Trifft Requests welche mit /data/ beginnen, sucht aber weiter.
# Nimmt diesen Block nur, wenn die weiteren regular expressions (~ und ~*), die Vorrang hätten nichts finden.
}
location ^~ /img/ {
# Trifft /img/ Requests und beendet dann die Suche. Sprich bei Request /img/1.jpg würde DIESER Block ausgeführt werden, da er hat das Priorität Prefix ^ hat.
}
location ~* .(png|gif|ico|jpg|jpeg)$ {
# Trifft alle Requests mit png, gif, ico, jpg or jpeg als Dateiendung.
# Aber Achtung, der /img/ Block weiter oben hätte Vorrang !
}
Location Verzeichnis mit und ohne Slash "treffen"
location ~* ^/admin(?:/(.*))?$ {
}
HTTP nach HTTPS umleiten
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name domain.de www.domain.de;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name domain.de www.domain.de;
root /var/www/html;
index index.html;
}
Verzeichnis geschützt und PHP ausführen. Musterlösung vom NGINX Entwickler
location ~* ^/admin(?:/(.*))?$ {
auth_basic "Admin-Section";
auth_basic_user_file /myfolder/.htpasswd;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Basic Auth
Install htpasswd Tool :
# apt-get install apache2-utils
# yum install -y httpd-tools
Create File and add user
# htpasswd -c /etc/nginx/.httppassword testuser1
add one more user :
# htpasswd /etc/nginx/.httppassword testuser2
# chmod 660 /etc/nginx/.httppassword
# chown root:nginx /etc/nginx/.httppassword
# vi /etc/nginx/conf.d/domain.de
auth_basic "Private Property";
auth_basic_user_file /etc/nginx/.httppassword;
# systemctl reload nginx
Auth für Unterordner aufheben
location /api/ {
auth_basic off;
allow all;
}
SSL Konfiguration
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options nosniff;
ssl_certificate /etc/letsencrypt/live/domain.de/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.de/privkey.pem; # managed by Certbot
ssl_trusted_certificate /etc/letsencrypt/live/domain.de/chain.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1.2;
ssl_ciphers HIGH:DHE:!aNULL:!MD5:!3DES:!CAMELLIA:!AES128:!AES256-SHA:!AES256-SHA256:!AES256-GCM-SHA384:!AES256-CCM8:!AES256-CCM;
ssl_prefer_server_ciphers on;
#openssl dhparam 4096 -out /etc/pki/tls/certs/dhparam.pem
#ssl_dhparam /etc/ssl/certs/dhparam.pem; # reduces to 90% https://www.ssllabs.com/ssltest
# prime256v1:secp384r1:secp521r1 - X25519 for TLS 1.3 # prime256v1 reduces to 90% https://www.ssllabs.com/ssltest
ssl_ecdh_curve secp521r1:secp384r1;
# OCSP
ssl_stapling on;
ssl_stapling_verify on;
# Set HSTS to 365 days
#add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
add_header Strict-Transport-Security 'max-age=31536000;' always;
Location "/" PHP ausführen
location ~* \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
404 und 5xx Fehlerseiten einrichten
error_page 404 /404.html;
location = /404.html { root /home/web/internet; internal; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root /home/web/internet; internal; }
Workaround für GET Requests
if ( $args ~ "service=git-upload-pack" ) { return 418; }
if ( $arg_somerandomfield = "somerandomvaluetomatch" ) { return 418; }
if ( $args ~* "diagnostics" ) { return 418; }
# Fehler 418 nicht laut HTTP-Statuscodes nicht belegt
error_page 418 = @blockaccess;
location @blockaccess {
auth_basic "Restricted area - This system is for the use of authorized users only";
auth_basic_user_file /home/iser/web/nginx_password;
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
Facebooks fbclid entfernen
location / {
# fbclid remove
if ($query_string ~ "^(.*)fbclid=(.*)$") { rewrite ^(.*)$ $uri? permanent; }
}
Autoindex
Besides simply using autoindex on or off, there are also three other directives you can configure with this module. These include the following:
autoindex_exact_size; - This directive specifies whether Nginx should display the exact file sizes of the output in the directory index or simply round to the nearest KB, MB, or GB. This directive has 2 options: on | off.
autoindex_format; - This directive specifies what format the Nginx index listing should be outputted to. This directive has 4 options: html | xml | json | jsonp.
autoindex_localtime; - This directive specifies whether the times for the directory listing should be outputted to local time or UTC. This directive has 2 options: on | off.
An example of a location directive using all 4 autoindex options could resemble the following.
location /somedirectory/ {
autoindex on;
autoindex_exact_size off;
autoindex_format html;
autoindex_localtime on;
}
Weitere Tipps
Installierte Module anzeigen : # nginx -V
Falls nginx mit Debug kompilliert worden ist, nehmen wir debug, sonst notice :
server {
error_log /var/log/nginx/error.log debug;
# oder
# error_log /var/log/nginx/error.log notice;
rewrite_log on;
}
Testen ob ein location Block getroffen wird (mit folgender Konfiguration wird ein File gedownloaded mit dem Textinhalt 'location 1') :
location {
return 200 'location 1';
}