Ich habe einen Nginx-Reverse-Proxy für meine PHP-API mit konfigurierten CORS-Headern eingerichtet. Während GET- und POST-Anfragen einwandfrei funktionieren, erreichen PUT-Anfragen mein PHP-Backend nicht, obwohl die API-Endpunkte für die Verarbeitung von PUT-Anfragen konfiguriert sind.
Konfiguration
Hier ist meine aktuelle Nginx-Konfiguration:
Code: Select all
server {
listen 80;
server_name _;
root /var/www/api;
index index.php;
location / {
# CORS headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization' always;
add_header 'Access-Control-Max-Age' '3600' always;
# Handle OPTIONS method
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
add_header 'Access-Control-Max-Age' '3600';
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api-php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
[*]CORS-Header sind ordnungsgemäß eingerichtet und OPTIONS-Preflight-Anfragen werden bearbeitet
Die PHP-API-Endpunkte sind so konfiguriert, dass sie PUT-Anfragen akzeptieren
[*]GET- und POST-Anfragen funktionieren über dasselbe Setup korrekt
Erwartetes Verhalten
PUT-Anfragen sollten an das PHP-Backend weitergeleitet und von den API-Endpunkten verarbeitet werden.
Tatsächliches Verhalten
PUT-Anfragen sind nicht vorhanden Erreichen des PHP-Backends: „PUT /users?id=10 HTTP/1.1“ 405 157 „-“ „-“ „-“
Header:
Code: Select all
* Trying 172.18.0.11:80...
* Connected to api-nginx (172.18.0.11) port 80 (#0)
> PUT /users?id=10 HTTP/1.1
> Host: api-nginx
> User-Agent: curl/7.88.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 15
>
< HTTP/1.1 405 Not Allowed
< Server: nginx/1.27.3
< Date: Sun, 19 Jan 2025 19:55:35 GMT
< Content-Type: text/html
< Content-Length: 157
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
< Access-Control-Allow-Headers: Content-Type, Authorization
< Access-Control-Max-Age: 3600
<
405 Not Allowed
405 Not Allowed
nginx/1.27.3
* Connection #0 to host api-nginx left intact
Welche Konfigurationsänderungen sind erforderlich, damit PUT-Anfragen das PHP-Backend ordnungsgemäß über Nginx erreichen können?