A problem with php request rewriting a problem with php request rewriting
My colleague asked me to help explain a problem: a PHP-generated redirect request generates two completely different records in the Nginx log: The response body size is zero bytes; the other response body size is five bytes.
Now that I am older, my sense of smell in the face of problems is no longer sensitive. I feel that nothing is correct. I wonder if I forget to quit after redirection, and there is content output later, however, I checked the code and found that there was no problem:
1
2
3
4
Header ('Location:/path ');
Exit;
?>
After a large circle, I suddenly realized that the environment was Nginx + PHP and the response was not "Content-Length". The data was sent through "Transfer-Encoding, therefore, the empty response body for redirection is actually similar:
0 \ r \ n
Not many, exactly five bytes. for details, refer to Chunked transfer encoding. In this case, PHP may take the initiative to output a "Content-Length: 0" when the response body is empty.
How can we explain the zero-byte response? Query logs can be found in the following two situations:
HEAD "/path HTTP/1.1" 302 0
GET "/path HTTP/1.0" 302 0
The former is a HEAD request and does not need a response body. The latter is HTTP/1.0 and does not support "Transfer-Encoding 」.
The problem was clearly explained, and the sweat on the forehead was not lost in front of my colleagues.