Skip to main content

Tutorial NextJS - Day 2

Hari 2 – Navigasi, Layout, & Dynamic Routing Setelah berhasil melakukan instalasi dan membuat halaman pertama di Hari 1 , pada Hari 2 ini kita akan membuat website menjadi lebih hidup. Kita akan belajar cara membuat menu navigasi agar bisa pindah-pindah halaman, membuat tampilan menu yang konsisten (Layout), dan membuat halaman artikel yang pintar (Dynamic Routing). Tutorial ini tetap menggunakan Windows + VS Code dan melanjutan project nextjs-basic dari hari sebelumnya. 1. Navigasi Antar Halaman ( <Link> ) Di HTML biasa, kita menggunakan tag <a> untuk berpindah halaman. Namun, di Next.js kita menggunakan komponen khusus bernama <Link> . Kenapa harus pakai <Link> ? Jika pakai <a> , layar akan berkedip (loading ulang) setiap pindah halaman. Dengan <Link> , perpindahan halaman terjadi instan tanpa refresh. 1.1 Edit Halaman Home Kita akan menambahkan tombol untuk pergi ke halaman About. Buka file src/app/page.tsx , lalu ganti isinya dengan kod...

How to Fix 504 Gateway Timeout on Nginx (Real VPS Case)

 

Introduction

A 504 Gateway Timeout error is one of the most common and frustrating problems when running applications behind Nginx, especially on a VPS.
I personally encountered this issue while deploying a production backend behind Nginx as a reverse proxy.

In this article, I’ll explain what causes a 504 Gateway Timeout, how to identify the real problem, and how to fix it properly using real VPS examples — not theory.


What Is a 504 Gateway Timeout?

A 504 Gateway Timeout occurs when:

  • Nginx acts as a reverse proxy

  • Nginx forwards a request to an upstream server (backend)

  • The upstream server does not respond in time

So technically:

Nginx is working, but your backend is slow, unreachable, or misconfigured.

Common Causes of 504 Errors on VPS

From real-world cases, these are the most frequent causes:

1. Backend server is down

  • Node.js / Python / Go app not running

  • Process crashed

  • Wrong port

2. Timeout configuration too low

  • Default Nginx timeout is often too short

  • Backend needs more processing time

3. Wrong reverse proxy configuration

  • Incorrect proxy_pass

  • Missing headers

  • HTTPS upstream without proper config

4. Network or DNS issue

  • Upstream domain not reachable

  • SSL handshake delay


Step 1: Check If Your Backend Is Running

Before touching Nginx, always check your backend first.

Example (Node.js backend on port 3000):

curl http://127.0.0.1:3000

If:

  • Connection refused → backend not running

  • No response → backend frozen

  • Response OK → move to Nginx config

Also check running processes:

pm2 status # or ps aux | grep node

Step 2: Check Nginx Error Logs

This step tells you the truth.

sudo tail -f /var/log/nginx/error.log

Typical 504 log:

upstream timed out (110: Connection timed out) while reading response header from upstream

This confirms:
Backend is too slow or unreachable

Step 3: Fix Nginx Timeout Configuration

This is the most common fix.

Open your Nginx config:

sudo nano /etc/nginx/sites-available/default

Add or update these values:

location / { proxy_pass http://127.0.0.1:3000; 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 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; }

Important

  • proxy_read_timeout is the most critical

  • Increase it if your backend does heavy processing


Step 4: Test and Reload Nginx

Always test before reload:

sudo nginx -t

If OK:

sudo systemctl reload nginx


Step 5: Real VPS Case (What Actually Happened)

In my case:

  • Backend was hosted on a cloud platform

  • Nginx proxied requests via HTTPS

  • Default timeout was too low

  • Result: 504 on every API call

The fix:

  • Increased proxy_read_timeout

  • Added proxy_ssl_server_name on

  • Set correct Host header

After that:

  • API worked normally
  • No more random 504 errors

How to Prevent 504 Errors in Production

Use this checklist:

  • Always monitor backend uptime

  • Use process managers (PM2, Supervisor)

  • Set realistic timeout values

  • Avoid heavy blocking requests

  • Add health check endpoints


Conclusion

A 504 Gateway Timeout on Nginx is rarely an Nginx problem.
Most of the time, it’s a backend performance or configuration issue.

By:

  • Checking backend status

  • Reading Nginx logs

  • Adjusting timeout values

You can fix 90% of 504 errors in minutes.

Final Tip

If you see a 504 error:

  • Don’t panic
  • Check logs first
  • Fix the real bottleneck





Comments

Popular posts from this blog

Why Your React App Shows a Blank Page (And How to Fix It)

Introduction One of the most frustrating problems when working with React JS is opening your app and seeing… nothing . No error message. No UI. Just a blank page . This issue happens frequently, especially after deployment or routing changes. In this article, we’ll explain why React apps show a blank page , the most common causes , and how to fix them step by step . What a “Blank Page” Usually Means in React A blank page usually means: React failed to render the UI JavaScript error stopped execution Routing or build configuration is broken Most of the time, the issue is not React itself , but how the app is configured. 1. JavaScript Error in the Console The first thing you should always check is the browser console. How to Check Open the app Right click → Inspect Open the Console tab If you see a red error message, React stopped rendering. Common Examples Cannot read property of undefined Unexpected token Module not found Fix Read the...

Nginx vs Apache: Which Web Server Should You Use on a VPS?

Introduction When setting up a VPS for hosting websites or applications, one of the first decisions you’ll face is choosing a web server. The two most popular options are Nginx and Apache . Both are powerful, widely used, and production-ready — but they work very differently. In this article, we’ll compare Nginx vs Apache , explain how each one works , highlight their strengths and weaknesses , and help you decide which web server is better for your VPS setup . What Is a Web Server? A web server is software that: Receives requests from browsers Processes those requests Serves HTML, CSS, JavaScript, or API responses Popular web servers include: Nginx Apache LiteSpeed Caddy Among them, Nginx and Apache dominate most VPS environments . What Is Apache? Apache has been around since the mid-1990s and was the most widely used web server for many years. How Apache Works Apache uses a process-based or thread-based model : Each request is handled by a p...

React Build vs Development Mode: What’s the Difference?

Introduction Many React developers are surprised when their app works perfectly during development — but behaves differently after deployment. Features break, routing fails, environment variables disappear, or performance changes dramatically. Most of these issues come from not understanding the difference between React’s development mode and production (build) mode . In this article, we’ll explain: What development mode really does What changes in production builds Why bugs appear only after deployment How to avoid common build-related mistakes What Is Development Mode in React? Development mode is what you use when running commands like: npm start npm run dev In this mode, React prioritizes developer experience , not performance. Key Characteristics of Development Mode Detailed error messages Helpful warnings Hot reloading (instant updates) Slower performance (on purpose) Extra checks for unsafe patterns Development mode helps you find bugs e...