Nginx Error Recv() Failed (104: Connection Reset by Peer)

nginx errors readv() and recv() failed

I was using php-fpm in the background and slow scripts were getting killed after a said timeout because it was configured that way. Thus, scripts taking longer than a specified time would get killed and nginx would report a recv or readv error as the connection is closed from the php-fpm engine/process.

nginx tomcat7 ERROR : - “recv() failed (104: Connection reset by peer) while reading response header from upstream”

I ended up using the following configuration : -

  1. We had to speed up our internal code i.e. code Execution time. Now each request is taking less than 50 ms.

  2. Nginx configuration used is : -

user www-data;worker_processes auto;worker_rlimit_nofile 10000;pid /run/nginx.pid;
events { worker_connections 2000; multi_accept on; use epoll;}
http { open_file_cache max=200000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; reset_timedout_connection on; client_body_timeout 200s; # Use 5s for high-traffic sites client_header_timeout 200s;
## # Basic Settings ## sendfile on;
tcp_nopush on; tcp_nodelay on; keepalive_timeout 900; keepalive_requests 10000; types_hash_max_size 2048; #proxy_buffering off; proxy_connect_timeout 1600; proxy_send_timeout 1600; proxy_read_timeout 1600; send_timeout 1600; include /etc/nginx/mime.types; default_type application/octet-stream;
access_log /var/log/nginx/stream.access.log; error_log /var/log/nginx/stream.error.log;
gzip on; gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;}

Nginx error readv() failed (104: Connection reset by peer) while reading upstream

We increased the Http header size for tomcat and this issue has been resolved. We made maxHttpHeaderSize="65536" so that tomcat can accept 64KB headers default is 8KB.

Nginx uwsgi (104: Connection reset by peer) while reading response header from upstream

After spending a lot of time on this, I finally figured it out. There are many references to Nginx and connection reset by peer. Most of them seemed to be related to PHP. I couldn't find an answer that was specific to Nginx and uwsgi.

I finally found a reference to fastcgi and a 502 bad gateway error (https://support.plesk.com/hc/en-us/articles/213903705). That lead me to look for a buffer size limit in the uwsgi configuration which exists as buffer-size. The default value is 4096. From the documentation, it says:

If you plan to receive big requests with lots of headers you can increase this value up to 64k (65535).

There are many ways to configure uwsgi, I happen to use a .ini file. So in my .ini file I tried:

buffer-size=65535

This fixed the problem. You can adjust that to taste. Maybe start with the max and work back until you have an acceptable value, or just leave it at the max.

This was frustrating to track down because there was no error on the uwsgi side of things.



Related Topics



Leave a reply



Submit