reverse proxy – nginx error – recv() failed (104: Connection reset by peer) while waiting for request


I’ve been unable to find exactly this error and without significant experience I’m unable to extrapolate a solution from similar questions that usually infer fastcgi issues that don’t seem to apply here (i’ve tried them anyhow)

I have a server (raspberry pi) running nginx that feeds a local apache server running wordpress and a physically different server on the LAN that runs a different domain/website behind its own nginx, using django, gunicorn and several proxy_pass vhosts.

This week I added an URL to receive pushed REST json messages from an IoT device. I tested the django request using curl and it works as expected, returning the json along with headers etc, the django view is simply:

    @csrf_exempt
    @require_POST
    def tests(request):
        if request.method == 'POST':
            d = request.body.decode('utf-8')
            jsondict = json.loads(d)

I got the code to work once with the IoT data but for the rest of the time it seems to cause an issue between the two servers which appears every 10 minutes in both nginx logs like this:

IoT server : proxy pass to local backend services like gunicorn

2020/09/15 20:20:02 (info) 9842#9842: *14 recv() failed (104: Connection reset by peer) while waiting for request, client: 192.168.1.173, server: 0.0.0.0:80

Main server – redirecting all domain traffic to IoT server

2020/09/15 20:20:02 (info) 10210#10210: *746 recv() failed (104: Connection reset by peer) while waiting for request, client: 192.168.1.173, server: 192.168.1.173:80

nginx conf on IoT server extracted

 server {
    listen 80 default_server;
    listen     (::):80 default_server;
    server_name a.ddns.net;
    root /var/www/html/a.ddns.net;

     location / {
        return 301 https://a.ddns.net$request_uri;
    }   
}

server {
    listen 443 ssl default_server http2; 
    listen (::):443 ssl default_server http2; 
    server_name a.ddns.net;     
    root /var/www/html/a.ddns.net;
    gzip           on;

    client_max_body_size 128M;
    client_body_buffer_size     128M;

    location ~* .php$ {
        fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 127.0.0.1:9000;
        include fastcgi_params;
            fastcgi_index index.php;
                fastcgi_intercept_errors on;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }

        
    location /eliot/bp {
        keepalive_timeout 5;
        proxy_read_timeout 600s;
        client_max_body_size 0;

        proxy_set_header Host $http_host;
        proxy_method POST;
        proxy_http_version 1.1;

    proxy_pass http://unix:/run/gunicorn.sock;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Original-URI $request_uri;
    proxy_set_header X-SERVER-PORT $server_port;
    proxy_set_header X-SERVER-ADDR $server_addr;
    proxy_set_header X-REAL-IP $remote_addr;            
    }   

I think I’ve got some kind of infinite loop between the two nginx servers. Can somebody suggest where I might start to debug the fault?