Ngnix transfers data to the unix unix domain completely

My application listens on a unix domain socket (UDS) for incoming data, and nginx sends data using PHP. Sending small blocks of data in a few KB works fine, but as soon as it reaches a certain limit, the browser receives an error 504 Gateway Time-out , nginx logs

timeout up (110: connection timeout) when reading the response header from upstream, client: 127.0.0.1, server: _, request: "GET /foo/bar.php HTTP / 1.1", upstream: "fastcgi: //unix:/run/php/php7.0-fpm.sock ", host:" localhost "

The socket still receives some data (always shrinking by about 1.5 MB) and responds, but the web server does not seem to receive a response. Are there UDS stream stream restrictions or nginx variables that need to be adjusted?

PHP code:

 public function send ($msg) { $str = "{$msg}".chr(27); $ret = socket_write($this->socket, $str, strlen($str)); if ($ret == FALSE) { return false; } else { $response = ""; while (($chunk = socket_read($this->socket, 2048, PHP_BINARY_READ)) !== FALSE) { $response .= $chunk; if (substr($chunk, -1) == chr(27)) break; } //close the connection if ($this->connected !== false) { socket_shutdown($this->socket); socket_close($this->socket); $this->connected = false; } return $response; } } 
+5
source share
2 answers

Good for staters. Nginx has nothing to do with this issue, which is understandable that PHP sends and receives data.

It is more than likely that your remote system is not closing the socket at the right time or just taking a long time to respond.

 while (($chunk = socket_read($this->socket, 2048, PHP_BINARY_READ)) !== FALSE) { $response .= $chunk; if (substr($chunk, -1) == chr(27)) break; } 

This code block can be an endless loop with this code if the remote system has not closed the connection / socket and told you about it, will try to read and wait for 2048 (bits or bytes - I can never remember what size it asks for the comment to report ) about the passage of data or about closing the socket before reading is completed.

So, a few things to try to reduce your read bytes, set it to something like 128 , put a timer in your socket (requires asynchronous programming in PHP), so kill it after 28 seconds, give your code 2 more seconds to execute ( safe exit). or use set_time_limit to increase the term.

If you increase your time limit, you will need to increase nginx time to get a response from connecting to PHP, to do this, set fastcgi_read_timeout

+1
source

This error indicates that the connection time between the Nginx server and the upstream server.

Does your request take more than 1 minute?

Try changing nginx location as instructed and read fastcgi_read_timeout

 location ~* \.php$ { include        fastcgi_params; fastcgi_index  index.php; fastcgi_read_timeout 120; fastcgi_pass   127.0.0.1:9000; fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name; } 
0
source

Source: https://habr.com/ru/post/1274500/


All Articles