Erro Nginx + php-fpm "504 Gateway Time-out" com carga quase zero (em um servidor de teste)

Erro Nginx + php-fpm "504 Gateway Time-out" com carga quase zero (em um servidor de teste)

Depois de depurar por 6 horas - estou desistindo disso :|

Temos um nginx+php-fpm+mysql em LAN com quase 100 wordpress (criado e usado por diferentes designers/desenvolvedores, todos trabalhando na configuração de teste do wordpres)

Estamos usando o nginx sem problemas há muito tempo.

Hoje, de repente - o nginx começou a retornar "504 Gateway Time-out" do nada ...

Verifiquei o log de erros do nginx em busca de um host virtual ...

2010/09/06 21:24:24 [error] 12909#0: *349 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 21:25:11 [error] 12909#0: *349 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 21:25:11 [error] 12909#0: *443 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 21:25:12 [error] 12909#0: *443 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:08:32 [error] 12909#0: *1025 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:09:33 [error] 12909#0: *1025 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:09:40 [error] 12909#0: *1064 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:09:40 [error] 12909#0: *1064 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:24:44 [error] 12909#0: *1313 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:24:53 [error] 12909#0: *1313 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"

Ao executar o php-fpm na porta 9000 via modo TCP, executei "netstat | grep 9000" e notei algo incomum... (Colando a saída parcial aqui para facilitar a leitura)

tcp        9      0 localhost:9000          localhost:36094         CLOSE_WAIT  14269/php5-fpm  
tcp        0      0 localhost:46664         localhost:9000          FIN_WAIT2   -               
tcp     1257      0 localhost:9000          localhost:36135         CLOSE_WAIT  -               
tcp     1257      0 localhost:9000          localhost:36125         CLOSE_WAIT  -               
tcp        9      0 localhost:9000          localhost:36102         CLOSE_WAIT  14268/php5-fpm  
tcp        0      0 localhost:46662         localhost:9000          FIN_WAIT2   -               
tcp      745      0 localhost:9000          localhost:46644         CLOSE_WAIT  -               
tcp        0      0 localhost:46658         localhost:9000          FIN_WAIT2   -               
tcp     1265      0 localhost:9000          localhost:46607         CLOSE_WAIT  -               
tcp        0      0 localhost:46672         localhost:9000          ESTABLISHED 12909/nginx: worker
tcp     1257      0 localhost:9000          localhost:36119         CLOSE_WAIT  -               
tcp     1265      0 localhost:9000          localhost:46613         CLOSE_WAIT  -               
tcp        0      0 localhost:46646         localhost:9000          FIN_WAIT2   -               
tcp     1257      0 localhost:9000          localhost:36137         CLOSE_WAIT  -               
tcp        0      0 localhost:46670         localhost:9000          ESTABLISHED 12909/nginx: worker
tcp     1265      0 localhost:9000          localhost:46619         CLOSE_WAIT  -               
tcp     1336      0 localhost:9000          localhost:46668         ESTABLISHED -               
tcp        0      0 localhost:46648         localhost:9000          FIN_WAIT2   -               
tcp     1336      0 localhost:9000          localhost:46670         ESTABLISHED -               
tcp        9      0 localhost:9000          localhost:36108         CLOSE_WAIT  14274/php5-fpm  
tcp     1336      0 localhost:9000          localhost:46684         ESTABLISHED -               
tcp        0      0 localhost:46674         localhost:9000          ESTABLISHED 12909/nginx: worker
tcp     1336      0 localhost:9000          localhost:46666         ESTABLISHED -               
tcp     1257      0 localhost:9000          localhost:46648         CLOSE_WAIT  -               
tcp     1336      0 localhost:9000          localhost:46678         ESTABLISHED -               
tcp        0      0 localhost:46668         localhost:9000          ESTABLISHED 12909/nginx: wo             

Existem muitos pares "CLOSE_WAIT" e "FIN_WAIT2" conforme destacado abaixo (na saída acima):

tcp     1337      0 localhost:9000          localhost:46680         CLOSE_WAIT  -               
tcp        0      0 localhost:46680         localhost:9000          FIN_WAIT2   -

Observe a porta 46680 acima.

Ativei o log de erros de consultas lentas do mysql, mas não funcionou.

A partir de agora, reiniciando o php5-fpm a cada minuto através de um cronjob (veja o comando abaixo) mantendo tudo funcionando "suavemente", mas eu odeio patchwork e quero resolver isso...

1 * * * * service php5-fpm restart > /dev/null

Pesquisei bastante no Google e não obtive ajuda. Como mencionado, este é um servidor de teste em LAN, a carga da CPU nunca ultrapassa 0,10 e o uso de memória também está abaixo de 25% (o sistema tem 2 GB de RAM e o servidor Ubuntu instalado). pelo menos deixe uma dica.

Agradecemos antecipadamente pela ajuda.

-Rahul

(nota - esta é uma repostagem de -http://forum.nginx.org/read.php?11,127694)

Atualização: encontrei a resposta, que está postada abaixo.

Responder1

Encontrei a resposta na minha postagem no fórum nginx -http://forum.nginx.org/read.php?2,127854

A resposta, no meu caso, é definir:

request_terminate_timeout=30s

na configuração do php-fpm (geralmente /etc/php5/fpm/php-fpm.conf)

Observe que você também pode usar valores diferentes de 30.

Usei-o para corresponder ao meu valor no php.iniarquivo principal, que é:

max_execution_time = 30

Obrigado a todos. :-)

Responder2

Veja como isso resolveu meu problema:

faça as seguintes alterações em /etc/nginx/nginx.conf na seção http {

proxy_connect_timeout  600s;
proxy_send_timeout  600s;
proxy_read_timeout  600s;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;

e então reinicie o nginx

/etc/init.d/nginx reiniciar

Responder3

Se você estiver usando php 5.3, aumente o backlog.

Se você estiver usando o php 5.2, faça backport do patch para aumentar o tamanho do backlog de 128.

Além disso, use um soquete Unix em vez de um soquete TCP. unix:/tmp/php5-cgi.sock (ou o caminho relevante)

Responder4

no meu caso (mesma mensagem de erro nginx), alguns scripts php problemáticos não estão terminando de executar e esperando por algo, resultando em não mais filhos php5-fpm para o nginx escolher.

consertar:

  1. adicione limite de tempo de execução, outro mencionou neste post. request_terminate_timeout=30s
  2. aumentar o número de filhos. e tudo funcionou perfeitamente. pm.max_spare_servers=16 pm.min_spare_servers=2

agora tudo funcionou perfeitamente.

informação relacionada