IPv4 & IPv6 Leasing - Any RIR, Any LocationOrder Now
Hostperl

Nginx Timeouts for Node.js Apps on AlmaLinux 9

By Raman Kumar

Share:

Updated on Nov 15, 2026

Nginx Timeouts for Node.js Apps on AlmaLinux 9

What this fixes and when to use it

Slow API calls, upload requests, and background-rendered pages can fail behind Nginx even when the Node.js app is healthy. On AlmaLinux 9, the usual cause is a mismatch between Nginx proxy timeouts, upstream response time, and systemd resource limits. This tutorial shows you how to tune both layers safely, verify the change, and roll back if needed.

You’ll work on a fresh AlmaLinux 9 VPS, start as root, create a non-root admin named deploy, then configure Nginx to wait long enough for real production requests. If you are standardizing a new environment, Hostperl VPS hosting gives you the control you need for this kind of application tuning, while still keeping the deployment small enough to manage well.

This guide is for operators who already have a Node.js service listening on 127.0.0.1:8000 and want Nginx to proxy it reliably. If you also need a broader check on slow upstream behavior, our timeout troubleshooting guide for PHP-FPM covers the same symptom from a different runtime angle.

Before you change anything

Keep your existing root SSH session open until the new admin login works. If you are doing this on a Hostperl VPS, confirm the server is reachable from your local computer first, then proceed carefully through user creation, key setup, package updates, and firewall checks.

ssh root@203.0.113.10

203.0.113.10 is a reserved documentation address. Replace it with the real public IP assigned to your server. If your provider uses a different default login, use that account name with the same example IP after you confirm access.

1) Confirm the operating system

cat /etc/os-release

Run this on the VPS as root. You should see AlmaLinux 9 details. The remainder of this tutorial is written for AlmaLinux 9 and other RHEL-compatible behavior where noted.

2) Create a non-root admin user

useradd -m -G wheel deploy
passwd deploy

Run this on the VPS as root. The first command creates the deploy account and adds it to the wheel group. The second sets a password you can use once, before you move to SSH keys. If you already manage access with keys only, you can lock the password instead of setting one:

passwd -l deploy

Now create the SSH directory and copy your public key. Replace the example key path with the key on your local computer.

install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
cat >> /home/deploy/.ssh/authorized_keys <<'EOF'
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyReplaceWithYourOwnKey local-admin
EOF
chown deploy:deploy /home/deploy/.ssh/authorized_keys
chmod 600 /home/deploy/.ssh/authorized_keys

This sequence creates the directory, appends a public key, and locks permissions so SSH accepts the file. If you need to remove a stale key later, edit /home/deploy/.ssh/authorized_keys and keep one key per line.

3) Open a second terminal and test the new login

ssh deploy@203.0.113.10

Run this on your local computer in a second terminal. Replace 203.0.113.10 with your server IP. If login succeeds, confirm sudo access from that new session:

sudo whoami

Run it on the VPS as the non-root sudo user. A successful response is root. Do not disable root login until this step works.

Update packages and install the runtime stack

Keeping the system current before service changes reduces the chance that you debug an unrelated package issue later. On AlmaLinux, use dnf for the initial refresh and install Nginx, Node.js, and the firewall tools you need for this setup.

sudo dnf -y update
sudo dnf -y install nginx nodejs firewalld

Run these on the VPS as the non-root sudo user. If Node.js 20 or later is already available in your environment, confirm the version before you continue:

node --version
npm --version
nginx -v

You should see current versions and no command errors. If the distribution repository provides an older Node.js release than your application needs, use the vendor-supported repository or application packaging method your deployment standard requires. For most customer sites, keeping the runtime consistent across staging and production matters more than chasing the newest patch level.

Prepare the Node.js app directory and a simple service

In production, keep the application outside the web root and let Nginx sit in front as a reverse proxy. This example uses /opt/myapp and a minimal server that responds after a short delay, which makes timeout behavior easy to test.

sudo mkdir -p /opt/myapp
sudo chown -R deploy:deploy /opt/myapp
cd /opt/myapp
cat > server.js <<'EOF'
const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === '/health') {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    return res.end('ok\n');
  }

  if (req.url === '/slow') {
    setTimeout(() => {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('slow response\n');
    }, 7000);
    return;
  }

  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('hello from node\n');
});

server.listen(8000, '127.0.0.1', () => {
  console.log('listening on 127.0.0.1:8000');
});
EOF

This file listens only on localhost, which is the safer pattern for a proxied app. You can replace the demo handler with your own application later.

4) Run it under systemd

sudo tee /etc/systemd/system/myapp.service > /dev/null <<'EOF'
[Unit]
Description=My Node.js App
After=network.target

[Service]
Type=simple
User=deploy
Group=deploy
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/server.js
Restart=on-failure
RestartSec=3
Environment=NODE_ENV=production
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now myapp

Run this on the VPS as the non-root sudo user. The LimitNOFILE line helps avoid descriptor pressure for busier sites. After startup, check service status and the listening socket:

systemctl status myapp --no-pager
ss -ltnp | grep :8000

You should see active (running) and a listener bound to 127.0.0.1:8000. If it fails, inspect the journal before touching Nginx.

journalctl -u myapp -b --no-pager

Configure Nginx timeout settings for the proxy

Nginx defaults are often fine for static sites but too short for slow upstream responses, file uploads, or long-running API work. The safest change is to raise the proxy timeout values only for the application server block you actually need.

sudo tee /etc/nginx/conf.d/myapp.conf > /dev/null <<'EOF'
upstream myapp_backend {
    server 127.0.0.1:8000;
    keepalive 16;
}

server {
    listen 80;
    server_name server.example.com;

    access_log /var/log/nginx/myapp.access.log;
    error_log /var/log/nginx/myapp.error.log;

    location / {
        proxy_pass http://myapp_backend;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_connect_timeout 10s;
        proxy_send_timeout 75s;
        proxy_read_timeout 75s;
        send_timeout 75s;
    }
}
EOF

Replace server.example.com with your actual hostname. The higher read and send timeouts give the backend time to finish requests without forcing a premature 504. The values here are conservative enough for most API workloads while still catching genuine stalls.

If you need to support large uploads, add client_max_body_size inside the same server block. Keep the setting intentional and workload-specific, not global.

5) Test the Nginx config before reload

sudo nginx -t

This command must report syntax is OK and the test is successful. If it does not, fix the referenced file before reloading the service.

sudo systemctl enable --now nginx
sudo systemctl reload nginx

Run these on the VPS as the non-root sudo user. On AlmaLinux 9, Nginx should start cleanly after the syntax check. If the service is already active, the reload applies your changes without dropping live connections.

Open the firewall and keep SSH safe

Do not remove SSH access before you confirm the new admin login. Open the web ports first, then test browser access, then tighten anything else if you later change the SSH policy.

sudo systemctl enable --now firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

Run this on the VPS as the non-root sudo user. You should see http and https in the active zone list. Because this tutorial keeps SSH on the default port and does not modify sshd, there is no lockout risk from the firewall step.

Verify the fix from the server and from a client

First confirm the backend works locally on the VPS, then test through Nginx. This separates app failures from proxy failures quickly.

curl -i http://127.0.0.1:8000/health
curl -i http://127.0.0.1/slow
curl -I http://server.example.com/

The first command should return 200 OK with ok. The second should return the slow response after several seconds instead of failing immediately. The third checks the public hostname through Nginx.

Now test from your local computer against the public server address or hostname:

curl -i http://server.example.com/slow

If the request completes and does not time out at the proxy, your Nginx timeout changes are working. For a live site, also inspect the log files for any 499, 502, or 504 entries after the test.

sudo tail -n 50 /var/log/nginx/myapp.error.log
sudo tail -n 20 /var/log/nginx/myapp.access.log

Run these on the VPS as the non-root sudo user. A clean pass usually shows normal upstream timing and no repeated gateway errors.

Check reboot persistence

Production changes should survive a reboot. Confirm both services are enabled, then reboot during a maintenance window if this is a customer-facing server.

systemctl is-enabled myapp
systemctl is-enabled nginx
sudo reboot

After the VPS returns, reconnect from your local computer and re-run the status checks:

ssh deploy@203.0.113.10
systemctl status myapp --no-pager
systemctl status nginx --no-pager

If both services come back cleanly, your timeout fix is persistent.

Rollback if the proxy change causes trouble

If you see unexpected 504 responses or the application starts returning errors only after the Nginx change, revert to the previous server block and reload. Because the app itself runs under systemd and listens locally, you can back out the proxy settings without stopping the backend.

sudo cp /etc/nginx/conf.d/myapp.conf /etc/nginx/conf.d/myapp.conf.bak
sudo tee /etc/nginx/conf.d/myapp.conf > /dev/null <<'EOF'
upstream myapp_backend {
    server 127.0.0.1:8000;
}

server {
    listen 80;
    server_name server.example.com;

    location / {
        proxy_pass http://myapp_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
EOF
sudo nginx -t
sudo systemctl reload nginx

If the rollback still does not resolve the issue, the problem is probably inside the Node.js app or upstream logic. Use the app journal, Nginx error log, and upstream health route together before making a second change.

Troubleshooting the most likely failures

Nginx still returns 504 Gateway Timeout

Check whether the backend is actually slow, not just proxied poorly.

curl -i http://127.0.0.1:8000/slow
sudo tail -n 50 /var/log/nginx/myapp.error.log

If the local curl already takes too long, raise the Node.js-side timeout or optimize the request path. If the local request is fast but Nginx still times out, increase proxy_read_timeout and review buffering behavior.

systemd service starts and stops immediately

Use the journal to see the real crash reason.

journalctl -u myapp -b --no-pager

If the log shows a missing file, wrong path, or permission problem, correct ExecStart, WorkingDirectory, or ownership, then reload systemd and restart the service:

sudo systemctl daemon-reload
sudo systemctl restart myapp

Nginx fails the config test

Use the test output to find the exact file and line number.

sudo nginx -t

If it points to a typo in /etc/nginx/conf.d/myapp.conf, edit the file, save the correction, and run the syntax test again before reloading.

Why this pattern works well on Hostperl VPS plans

This setup keeps the application private on localhost, lets Nginx handle external traffic, and gives you a clear way to tune request lifetimes without changing app code first. That is the kind of operational split many customer sites need when they move from a simple launch to real traffic.

For projects that need more headroom, easier failover, or heavier request concurrency, managed VPS hosting is often the cleanest starting point, while dedicated server hosting is the better fit when the upstream workload is consistently long-running or CPU-heavy. If you are planning a migration from another provider, pair this guide with the agency cutover checklist so your change window, DNS, and service validation stay in order.

If your Node.js service is hitting proxy timeouts or slow upstream responses, Hostperl can help you size the right VPS or dedicated server for the workload and keep the cutover controlled. Start with VPS hosting for a compact deployment, or move to dedicated server hosting when request volume and response-time targets are tighter.

Our support approach is practical: verify the service, confirm the logs, and then tune the proxy layer with the least disruption possible.

FAQ

How do I know whether the timeout is in Nginx or Node.js?

Test the backend directly on 127.0.0.1:8000 first. If it is slow there, the app needs attention. If it is fast locally but slow through Nginx, the proxy settings are the issue.

Should I raise all timeout values to several minutes?

No. Set only the values your workload needs. Very large timeouts can hide broken requests and delay useful error reporting.

Can I use this setup for uploads?

Yes, but you may also need client_max_body_size and application-side upload limits. Match those settings to the real file sizes your users send.

What if I use Apache instead of Nginx?

The proxy directives are different, but the operational idea is the same: confirm the backend first, then tune the web server in front of it.