Skip to main content

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 Routing Explained (And Why It Breaks in Production)

 

Introduction

React routing often works perfectly during development — but suddenly breaks in production. Pages show a blank screen, return 404 errors, or fail when users refresh the browser.

If you’ve ever asked:

“Why does my React routing work locally but not after deployment?”

This article will give you a clear, practical explanation.

We’ll cover:

  • How React routing actually works

  • Why it breaks in production

  • The most common mistakes

  • How to fix routing issues correctly


How React Routing Works

React applications use client-side routing, most commonly with React Router.

Instead of the server handling routes like:

/about /contact /products

React:

  • Loads one HTML file (index.html)

  • Uses JavaScript to switch views

  • Updates the URL without reloading the page

This is fast and smooth — but it creates problems if the server is not configured properly.


Why Routing Works in Development

During development:

  • You usually run npm run dev or npm start

  • A development server (Vite, CRA, etc.) is running

  • The dev server automatically redirects all routes to index.html

So when you refresh /about, everything still works.

👉 This safety net disappears in production.


Why React Routing Breaks in Production

In production, your app is served by:

  • Nginx

  • Apache

  • Cloud hosting

  • Static file servers

These servers do not know about React routes.

When a user refreshes:

https://example.com/about

The server thinks:

“There is no /about file or folder”

So it returns:

  • 404 error

  • Blank page

  • Default server page


The Core Problem (One Sentence)

The server does not know it should serve index.html for React routes.


Most Common React Routing Mistakes

1. No Fallback to index.html

This is the number one cause of broken routing.

When the server cannot find a route, it should always return:

index.html

Without this fallback, routing breaks on refresh.


2. Using BrowserRouter Without Server Support

BrowserRouter relies on clean URLs:

/about /profile /settings

This requires server configuration.

If the server is not set up correctly, routing fails.


3. Incorrect Base Path

If your app is deployed in a subdirectory:

https://example.com/app/

React may still assume it’s running at /.

This causes:

  • Blank pages

  • Assets not loading

  • Routes breaking


4. Missing Build Configuration

Developers sometimes:

  • Upload the wrong folder

  • Serve source files instead of build files

  • Point the server to the wrong root directory

React only works in production using the build output.


How to Fix React Routing in Production

✅ Fix for Nginx

Add this inside your server block:

location / { try_files $uri /index.html; }

This tells Nginx:

“If the file doesn’t exist, always serve index.html.”


✅ Fix for Apache

Create or update .htaccess:

RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L]

✅ Fix for Vite (Base Path)

If your app is not at root:

export default defineConfig({ base: "/app/" });

✅ Fix for Create React App

Add to package.json:

"homepage": "/app/"

Then rebuild the app.


Alternative: HashRouter (When You Can’t Control the Server)

If you cannot configure the server, use HashRouter.

URLs will look like:

/#/about /#/profile

Pros

  • No server configuration needed

  • Works everywhere

Cons

  • Less clean URLs

  • Not ideal for SEO

Use this only if server configuration is impossible.


How to Debug Routing Issues

When routing breaks:

  1. Refresh a non-homepage route

  2. Open browser DevTools

  3. Check:

    • Network errors

    • 404 responses

  4. Confirm server fallback

  5. Verify build folder is served

Most routing issues can be found in minutes with this process.


Best Practices for React Routing

  • Use BrowserRouter with proper server config

  • Always test refreshing routes

  • Test production build locally

  • Keep routing simple

  • Avoid deploying without fallback rules

Routing bugs often appear only after deployment — test early.


Why This Issue Is So Common

React makes routing feel easy — but hides the server dependency.

Many tutorials:

  • Skip production setup

  • Focus only on development

  • Assume hosting “just works”

In reality, routing is a collaboration between React and the server.


Conclusion

React routing doesn’t break randomly. It breaks because the server is not prepared to handle client-side routes.

Once you understand:

  • How routing works

  • What the server expects

  • How to configure fallback rules

You can fix routing issues permanently and deploy React apps with confidence.

Comments

Popular posts from this blog

Common React JS Mistakes Beginners Make (And How to Fix Them)

Introduction React JS is powerful, but many beginners struggle not because React is hard — but because of common mistakes made early on . These mistakes can lead to confusing bugs, poor performance, and messy code that becomes difficult to maintain. In this article, we’ll cover the most common React JS mistakes beginners make , explain why they happen , and show how to fix them properly . If you’re learning React or recently started building projects, this guide will save you hours of frustration. 1. Modifying State Directly One of the most frequent beginner mistakes is changing state directly instead of using the state updater function. ❌ Wrong Example count = count + 1 ; This does not trigger a re-render. ✅ Correct Way setCount (count + 1 ); Why This Matters React relies on state updates to know when to re-render . Direct mutation breaks that mechanism and causes unpredictable UI behavior. 2. Using State When It’s Not Needed Beginners often store everything in state...

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.

Top 10 AI Tools to Generate Images in 2026 (Free & Paid)

  Introduction AI image generation has evolved rapidly over the past few years. What once required advanced design skills can now be done by simply describing an idea in words. As we move into 2026, AI image tools are no longer experimental — they are widely used for content creation, marketing, education, and personal projects. In this article, we explore 10 AI image generation tools that are widely used in 2026 , covering both free and paid options , along with their strengths and ideal use cases. What Makes a Good AI Image Generator? Before diving into the list, it’s important to understand what users usually look for: Image quality and realism Prompt understanding Style flexibility Speed and usability Pricing and usage limits The tools below were selected based on popularity, capability, and real-world usage , not hype alone. 1. DALL·E 3 Best for: Accurate text-to-image interpretation DALL·E 3 is known for its ability to understand complex prompts wi...