Score:0

random Nginx 408 errors during ajax post

cn flag

I'm using jQuery to post some form data to my Nginx server but sometimes it fails with a 408 error. When the error happens, apparently the php script to process the posted data is NOT reached at all, as I put some logging function in the first line. And the most weird part is, with the same form data, sometimes the script works, sometimes not. I couldn't figure out why.

Browser: Microsoft Edge 101.0.1210.32

jQuery:1.12.4, code of posting data as below:

$.ajax({
    url: 'save.php',
    dataType: 'json',
    type: 'post',
    contentType: 'application/json',
    data: JSON.stringify( { "comment": comment,"id":id} ),
    processData: false
});

Nginx log:

[03/May/2022:15:11:01 +0700] "POST /save.php HTTP/1.1" 408 0 ...

Nginx conf:

client_max_body_size 10M;
client_body_buffer_size 2048k;
client_header_timeout 60s;
client_body_timeout 60s;
keepalive_timeout 60s;
Score:0
de flag

It is possible to define additional log format - specially for debugging. In nginx.conf in section http

http {
  # ... other config lines

  log_format request_body_log '$remote_addr $remote_user [$time_local] "$request" status:$status bytes_sent:$bytes_sent gzip_ratio:$gzip_ratio connection_requests:$connection_requests request_length:$request_length connection-serial-number:$connection request_time:$request_time "$http_user_agent" "$http_referer" request_body:"$request_body"';

  # ... 
}

To enable it for location:

location / {
  # ...   
  if ($request_method = POST) {
    access_log /var/www/html/logs/nginx_request_body.log request_body_log;
  }
}

This provides in logs a lot of information about every http-request.
P.S. This example for rather old version, maybe Your nginx version has differences.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.