I have a JSON object sent from Ajax to Laravel. My Laravel project on windows with xampp receives the complete JSON and does not give any problem. In production (Debian 11) the Laravel project receives an incomplete request.
This is the body sent from AJAX
I used the VSCODE comparator (Local vs Production) to be sure that in both cases I send the same thing.
This is the body received on laravel 10.48.7
Despite sending the same, Laravel receives less content and closes the JSON.
JSON is closed
I don’t have “Request Entity Too Large” problem.
If I execute:
php -i | grep -E 'post_max_size|upload_max_filesize|max_input_vars|memory_limit'
I have:
max_input_vars => 100000000 => 100000000
memory_limit => -1 => -1
post_max_size => 4G => 4G
upload_max_filesize => 4G => 4G
Nginx has:
client_max_body_size 400M;
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
I have tried to increase the capacity of nginx and php but the problem still occurs.
Nikodem
November 28, 2024, 6:22am
2
Your problem might not be directly related to PHP, Nginx, or system-level limits but could instead be due to one of the following issues:
Potential Causes:
AJAX Configuration :
Ensure your AJAX request correctly handles large payloads. Try enabling content types such as application/json
.
Truncated JSON :
Laravel may be receiving a truncated request due to middleware or application-level handling.
Inspect Laravel’s middleware stack for potential issues like JSON decoding.
Nginx Timeouts :
Verify timeouts are correctly extended in your nginx.conf
:
client_body_timeout 300s;
send_timeout 300s;
proxy_read_timeout 300s;
proxy_connect_timeout 300s;
If the request takes a long time, it might be prematurely closed.
4. Encoding Mismatch :
Check for encoding issues. For example, the production server might fail to handle special characters or multi-byte strings properly.
POST Request Parsing in Laravel :
If Laravel isn’t receiving the full payload, try manually logging the raw body using:
$payload = file_get_contents('php://input');
\Log::info($payload);
Network Differences :
Ensure no intermediary systems or load balancers are truncating the request.
Debugging Steps:
Log Request Payload :
Compare the payload received by Laravel using:
$request->getContent();
Inspect Server Logs :
Look into Nginx logs (/var/log/nginx/error.log
) for hints of request truncation or errors.
Disable Laravel Middleware :
Temporarily disable middleware like TrimStrings
or ConvertEmptyStringsToNull
to see if they affect the payload.
AJAX Debugging :
Log the request headers and body being sent by your browser.
Suggested Fixes:
If middleware is involved, add exceptions for specific routes handling large JSON payloads:
Route::post('/api/endpoint', [Controller::class, 'method'])->withoutMiddleware([TrimStrings::class]);
If AJAX has issues, verify the headers include:
headers: {
'Content-Type': 'application/json'
}
If these steps don’t resolve the issue, please share the exact part of the payload being truncated or any specific errors logged by Laravel or Nginx.