title: NGINX and nodejs upstream connection timed out while connecting to upstream but request will get responded after exactly 60s
body: <p>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.</p>
  <p>This is the nginx error that I am getting fairly frequently:</p>
  <pre><code>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: &quot;GET /getTemp?api_key=xxxxxxx HTTP/1.1&quot;, upstream: &quot;http://127.0.0.1:8080/getTemp?api_key=xxxxxxx&quot;, host: &quot;sub.domain.tld&quot;
  </code></pre>
  <p>I have tried to increase nginx <code>proxy_read_timeout</code> to 120s as suggested on other posts but that isn't the fix here. I have also switched from <code>mysql.createConnection()</code> to <code>mysql.createPool()</code> but that didn't fix it.</p>
  <p>Here is my nodejs connection to mysql database:</p>
  <pre><code>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'
  });
  </code></pre>
  <p>Here is snippet of getTemp endpoint:</p>
  <pre><code>app.get('/getTemp', (req, res) =&gt;{
      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:&quot;One or more variables are undefined&quot;});
          }
          
          sql_query = &quot;SELECT * FROM tempData ORDER BY id DESC&quot;;
  
          sql_query = mysql.format(sql_query);
          
          // attempt to query mysql server with the sql_query 
          pool.query(sql_query, (error, result) =&gt;{
              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 &gt; 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:&quot;Temperature not found&quot;});
              }
          });
  
      }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);
      }
  })
  </code></pre>
  <p>And here is my nginx config:</p>
  <pre><code>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;
              }
  
          }
  }
  </code></pre>