NGINX and nodejs upstream connection timed out while connecting to upstream but request will get responded after exactly 60s

Subsequent HTTP requests GET and POST will sometimes take exactly 60s to get a response and other times requests get a response under 100 ms. How should I try to fix this? I think it has something to do with my nodejs backend and mysql connection and not necessarily nginx reverse proxy and docker containers that these both live in.

This is the nginx error that I am getting fairly frequently:

2022/01/10 16:43:15 [error] 33#33: *985 upstream timed out (110: Connection timed out) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: sub.domain.tld, request: "GET /getTemp?api_key=xxxxxxx HTTP/1.1", upstream: "http://127.0.0.1:8080/getTemp?api_key=xxxxxxx", host: "sub.domain.tld"

I have tried to increase nginx proxy_read_timeout to 120s as suggested on other posts but that isn't the fix here. I have also switched from mysql.createConnection() to mysql.createPool() but that didn't fix it.

Here is my nodejs connection to mysql database:

let pool = mysql.createPool({
    connectionLimit: 10,
    host: process.env.DB_HOST,
    user: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE,
    timezone: 'Europe/Helsinki'
});

Here is snippet of getTemp endpoint:

app.get('/getTemp', (req, res) =>{
    console.log(req.path);
    let ip = req.header('x-forwarded-for') || req.socket.remoteAddress;

    let ua = req.get('user-agent');
    console.log(ua);

    let {api_key} = req.query;
    console.log(req.query);

    if(api_key == process.env.API_KEY){
        console.log(`Authorized access from IP: '${ip}'`);
        // if one of the variables are undefined then send 400 status code to the client
        if(api_key == undefined){
            return res.sendStatus(400).send({message:"One or more variables are undefined"});
        }
        
        sql_query = "SELECT * FROM tempData ORDER BY id DESC";

        sql_query = mysql.format(sql_query);
        
        // attempt to query mysql server with the sql_query 
        pool.query(sql_query, (error, result) =>{
            if (error){
                // on error log the error to console and send 500 status code to client
                console.log(error);
                return res.sendStatus(500);
            };
            
            // if temp is found we send 200 status code to client
        
            if(result.length > 0){
                let currentTemp = result[0].currentTemp;
                console.log(`Temp: ${currentTemp}`);
                return res.status(200).send({temperature:currentTemp});
            }else{
                // incase temp is not found then we send 500 status code to client
                console.log('Temperature not found');
                return res.status(500).send({error:"Temperature not found"});
            }
        });

    }else{
      // if client sends invalid api key then send 403 status code to the client
  
      console.log(`Unauthorized access using API key '${api_key}' from IP: '${ip}'`);
      return res.sendStatus(403);
    }
})

And here is my nginx config:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 4096;
}

http {
         server {
            listen 80;
            listen [::]:80;

            listen 443 default_server ssl;
            listen [::]:443 ssl;

            ssl_certificate /cert.pem;
            ssl_certificate_key /cert.key;

            server_name sub.domain.tld;

            location / {
                proxy_read_timeout 120;

                proxy_pass http://localhost:8080;
                proxy_set_header X-Real-IP $http_cf_connecting_ip;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
            }

        }
}

I assigned static value to the endpoint so it wont query anything from the database and it also would take a minute to respond so it wasn't the database connection or the endpoint that was causing issues. I disabled rate limiting in the app and I didn't timeout anymore. So the reason for my timeouts was a misconfigured rate limit.