• Konfiguration NGinX/PHP

    From Jan Kohnert@21:1/5 to All on Wed Dec 1 14:00:01 2021
    This is a multi-part message in MIME format.

    Hallo,

    ich versuche gerade, NGinX so zu konfigurieren, dass es den ersten Teil des Pfades
    abscheidet, wenn es ein PHP-Script an PHP-FPM weiterleitet. Die Konfig dazu sieht
    folgendermaßen aus:

    log_format scripts '$document_root$fastcgi_script_name > $request';
    access_log /var/log/nginx/scripts.log scripts;

    server {
    listen 80;

    root /usr/share/nginx/html/;
    index index.php index.html index.htm;

    location / {
    return 406;
    }

    location /simplesaml {
    location ~ \..*/.*\.php$ {
    return 406;
    }
    try_files $uri $uri/ =404;

    alias /usr/share/simplesamlphp/www;

    location ~ \.php(/|$) {
    include fastcgi_params;
    fastcgi_split_path_info ^/simplesaml/(.+?\.php)(/.+)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    }
    }
    }

    Ich bin davon ausgegangen, dass die fastcgi_split_path_info unter $1 dann das Script
    liefert, unter $2 all das, was nach dem Script und einem weiteren / kommt; aber leider ist
    das laut log nicht der Fall. SCRIPT_FILENAME wird immer auf /usr/share/simplesamlphp/
    www/simplesaml/${SCRIPTFILE}.php gesetzt und nicht wie gewünscht auf /usr/share/
    simplesamlphp/www/${SCRIPTFILE}.php

    $ docker exec 3b tail -f /var/log/nginx/scripts.log /usr/share/simplesamlphp/www/simplesaml/index.php > GET /simplesaml/ HTTP/1.1

    Wo genau steckt mein Denkfehler? Ich sehe ihn gerade nicht.

    Das ganze findet in einem NGinX-Container statt; dieser basiert auf Debian stable
    (Bullseye).

    Lieben Dank!

    --
    Mit freundlichen Grüßen
    Jan Kohnert

    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>
    <body><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="font-family:Hack;">Hallo,</span></p>
    <br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">ich versuche gerade, NGinX so zu konfigurieren, dass es den ersten Teil des Pfades abscheidet, wenn es ein PHP-Script an PHP-FPM weiterleitet. Die Konfig dazu sieht folgendermaß
    en aus:</p>
    <br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">log_format scripts '$document_root$fastcgi_script_name &gt; $request';</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">access_log /var/log/nginx/scripts.log scripts;</p>
    <br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">server {</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&nbsp;&nbsp;&nbsp; listen 80;</p>
    <br /><p style="margi
  • From Jan Kohnert@21:1/5 to All on Fri Dec 3 10:00:02 2021
    This is a multi-part message in MIME format.

    Am Mittwoch, 1. Dezember 2021, 13:34:52 CET schrieb Jan Kohnert:
    [...]
    location ~ \.php(/|$) {
    include fastcgi_params;
    fastcgi_split_path_info ^/simplesaml/(.+?\.php)(/.+)$;
    fastcgi_param SCRIPT_FILENAME
    $document_root$fastcgi_script_name; fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    }


    Merke: Immer die RegExp genau ansehen und testen. :-D Der RegExp hier matcht natürlich nur, wenn nach dem php ein / kommt, was nicht zwingend der Fall ist. Und
    soweit ich es in der NGinX Doku gesehen habe, sollte das include doch erst nach den
    Eigendefinitionen kommen


    Jedenfalls funktioniert das:
    location ~ \.php(/|$) {
    # Strip out location base in the calculation of script name/parameters
    fastcgi_split_path_info ^/simplesaml/(.+\.php)(.*)$;

    # Check the script exists before proxying
    if (!-f $document_root/$fastcgi_script_name) {
    return 404;
    }

    # Mitigate https://httpoxy.org/ vulnerabilities
    fastcgi_param HTTP_PROXY "";

    # Now set the script name and proxy
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;

    # include Debians main configuration
    include fastcgi_params;
    }


    --
    Mit freundlichen Grüßen
    Jan Kohnert

    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>
    <body><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">Am Mittwoch, 1. Dezember 2021, 13:34:52 CET schrieb Jan Kohnert:</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">[...]</p> <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt;         location ~ \.php(/|$) {</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt;             include fastcgi_params;</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt;             fastcgi_split_path_info ^/simplesaml/(.+?\.php)(/.+)$;</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt;             fastcgi_param SCRIPT_FILENAME</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt; $document_root$fastcgi_script_name; fastcgi_pass</p>
    <p style="margin-top:0;margin-bo
  • From Jan Kohnert@21:1/5 to All on Fri Dec 3 10:20:02 2021
    This is a multi-part message in MIME format.

    Am Mittwoch, 1. Dezember 2021, 13:34:52 CET schrieb Jan Kohnert:
    [...]
    location ~ \.php(/|$) {
    include fastcgi_params;
    fastcgi_split_path_info ^/simplesaml/(.+?\.php)(/.+)$;
    fastcgi_param SCRIPT_FILENAME
    $document_root$fastcgi_script_name; fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    }

    Merke: Immer die RegExp genau ansehen und testen. :-D Der RegExp hier matcht natürlich nur, wenn nach dem php ein / kommt, was nicht zwingend der Fall ist. Und
    soweit ich es in der NGinX Doku gesehen habe, sollte das include doch erst nach den
    Eigendefinitionen kommen

    Jedenfalls funktioniert das:
    location ~ \.php(/|$) {
    # Strip out location base in the calculation of script name/parameters
    fastcgi_split_path_info ^/simplesaml/(.+\.php)(.*)$;

    # Check the script exists before proxying
    if (!-f $document_root/$fastcgi_script_name) {
    return 404;
    }

    # Mitigate https://httpoxy.org/ vulnerabilities
    fastcgi_param HTTP_PROXY "";

    # Now set the script name and proxy
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;

    # include Debians main configuration
    include fastcgi_params;
    }

    --
    Mit freundlichen Grüßen
    Jan Kohnert

    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>
    <body><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">Am Mittwoch, 1. Dezember 2021, 13:34:52 CET schrieb Jan Kohnert:</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">[...]</p> <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; location ~ \.php(/|$) {</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; include fastcgi_params;</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fastcgi_split_path_info ^/simplesaml/(.+?\.php)(/.+)$;</p>
    <p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&