Files
laraundfelix/tests/Unit/NginxConfigTest.php
T
LarsandClaude Sonnet 5.5 acb88e4a1d
CI Feature / test (push) Successful in 2m40s
Deploy Test / test (push) Successful in 2m4s
Deploy Test / deploy (push) Successful in 30s
Leite www- und HTTP-Aufrufe im Shop-Nginx auf lara-und-felix.de um
GET und HEAD auf www.lara-und-felix.de und www.laraundfelix.de sowie auf
http://lara-und-felix.de (nur bei X-Forwarded-Proto http) bekommen ein 301 auf
die feste Adresse https://lara-und-felix.de, Pfad und Query bleiben erhalten.
Andere Methoden (Stripe-Webhook), die ACME-Prüfung, dev.laraundfelix.de und
direkter Zugriff ohne Proxy-Header (Health-Check) bleiben unverändert.

Mit nginx 1.27.5 und php-cgi in 30 Fällen geprüft; Zusagen der Konfiguration
stehen in NginxConfigTest, Betrieb in docs/betrieb/DEPLOYMENT.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-22 11:52:46 +01:00

123 lines
6.0 KiB
PHP

<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
/**
* Der Web-Container (nginx) muss den App-Container zur Laufzeit über Dockers DNS finden. Mit einer festen Adresse
* beim Start antwortete Prod nach einem erneuten Deploy-Lauf mit 502 (App neu erzeugt, nginx nicht).
*
* Außerdem leitet er die Alias-Adressen auf https://lara-und-felix.de weiter. Das Verhalten wurde mit nginx selbst
* geprüft (Ergebnis in docs/betrieb/DEPLOYMENT.md). Diese Tests sichern die Zusagen der Konfiguration gegen
* versehentliche Änderungen, denn im CI-Container läuft kein nginx.
*/
class NginxConfigTest extends TestCase
{
private function config(): string
{
return (string) file_get_contents(dirname(__DIR__, 2).'/docker/nginx/default.conf');
}
/**
* Zeilen der Weiterleitungs-map in der Reihenfolge der Datei (nginx wertet sie in dieser Reihenfolge aus).
*
* @return list<string>
*/
private function redirectMap(): array
{
$found = preg_match('/^map\s+"[^"]*"\s+\$luf_canonical_redirect\s*\{(.*?)^\}/ms', $this->config(), $match);
$this->assertSame(1, $found, 'Die map $luf_canonical_redirect fehlt.');
return array_values(array_filter(array_map('trim', explode("\n", $match[1]))));
}
/**
* @return list<string> Zeilen der map, die weiterleiten (Wert 1)
*/
private function redirectRules(): array
{
return array_values(array_filter($this->redirectMap(), fn (string $line) => str_ends_with($line, '" 1;')));
}
public function test_nginx_resolves_the_app_container_at_request_time(): void
{
$config = $this->config();
$this->assertMatchesRegularExpression('/^\s*resolver\s+127\.0\.0\.11\b[^;]*valid=\d+s[^;]*;/m', $config);
$this->assertMatchesRegularExpression('/^\s*set\s+\$php_upstream\s+app:9000;/m', $config);
$this->assertMatchesRegularExpression('/^\s*fastcgi_pass\s+\$php_upstream;/m', $config);
$this->assertDoesNotMatchRegularExpression('/^\s*fastcgi_pass\s+app:9000;/m', $config);
}
public function test_nginx_keeps_the_fastcgi_and_timeout_settings(): void
{
$config = $this->config();
$this->assertMatchesRegularExpression('/^\s*fastcgi_read_timeout\s+3600s;/m', $config);
$this->assertMatchesRegularExpression('/^\s*fastcgi_send_timeout\s+3600s;/m', $config);
$this->assertMatchesRegularExpression('/^\s*client_max_body_size\s+2048m;/m', $config);
$this->assertMatchesRegularExpression('/^\s*fastcgi_param\s+HTTP_X_FORWARDED_PROTO\s+\$forwarded_proto;/m', $config);
$this->assertStringContainsString('try_files $uri $uri/ /index.php?$query_string;', $config);
$this->assertStringContainsString('location ~ /\.(?!well-known) {', $config);
}
public function test_alias_hosts_are_redirected_to_the_fixed_canonical_domain(): void
{
$config = $this->config();
$this->assertStringContainsString('if ($luf_canonical_redirect) {', $config);
$this->assertStringContainsString('return 301 https://lara-und-felix.de$request_uri;', $config, 'Fest verdrahtetes Ziel, Pfad und Query-String aus $request_uri.');
$this->assertSame(1, preg_match_all('/^\s*return\s+30\d\s/m', $config), 'Es gibt genau eine Weiterleitung.');
$this->assertDoesNotMatchRegularExpression('/return\s+30\d\s+[^;]*\$(host|http_host|http_x_forwarded_host|scheme)/', $config, 'Das Ziel darf nicht aus Anfragewerten entstehen.');
}
public function test_redirect_rules_cover_both_www_aliases_and_the_http_apex_only(): void
{
$rules = implode("\n", $this->redirectRules());
$this->assertCount(3, $this->redirectRules());
$this->assertStringContainsString('www\.lara-und-felix\.de', $rules);
$this->assertStringContainsString('www\.laraundfelix\.de', $rules);
$this->assertStringContainsString('\|\s*http\s*(,[^|]*)?\|lara-und-felix\.de', $rules, 'Der Apex wird nur bei ausdrücklichem X-Forwarded-Proto http weitergeleitet.');
$this->assertStringNotContainsString('dev.', $rules, 'dev.laraundfelix.de bleibt unverändert.');
$this->assertStringNotContainsString('|laraundfelix', $rules, 'laraundfelix.de ohne www übernimmt die STRATO-Weiterleitung.');
$this->assertStringContainsString('default 0;', implode("\n", $this->redirectMap()));
}
public function test_only_get_and_head_are_redirected(): void
{
foreach ($this->redirectRules() as $rule) {
$this->assertStringContainsString('"~*^(GET|HEAD)\|', $rule, 'POST (zum Beispiel der Stripe-Webhook) darf kein 301 bekommen: '.$rule);
}
}
public function test_acme_challenge_is_excluded_before_any_redirect_rule(): void
{
$acme = null;
$firstRedirect = null;
foreach ($this->redirectMap() as $index => $line) {
if ($acme === null && str_contains($line, '/\.well-known/acme-challenge/') && str_ends_with($line, '" 0;')) {
$acme = $index;
}
if ($firstRedirect === null && str_ends_with($line, '" 1;')) {
$firstRedirect = $index;
}
}
$this->assertNotNull($acme, 'Die ACME-Ausnahme fehlt.');
$this->assertNotNull($firstRedirect);
$this->assertLessThan($firstRedirect, $acme, 'nginx wertet map-Einträge der Reihe nach aus: die Ausnahme muss vorn stehen.');
}
public function test_redirect_decision_uses_forwarded_headers_and_never_the_local_scheme(): void
{
$config = $this->config();
$this->assertStringContainsString('map "$request_method|$http_x_forwarded_proto|$luf_public_host|$uri" $luf_canonical_redirect {', $config);
$this->assertStringContainsString('map $http_x_forwarded_host $luf_forwarded_host {', $config);
$this->assertMatchesRegularExpression('/^\s*""\s+\$host;/m', $config, 'Ohne X-Forwarded-Host gilt der Host-Header.');
$this->assertStringNotContainsString('$scheme|', $config, 'Ohne X-Forwarded-Proto darf nie weitergeleitet werden (direkter Zugriff, Health-Check).');
}
}