~/issues/react-js-router-issue-nginx-docker.md

React JS router not working on Nginx docker container

2 min readJuly 26, 2019by Gorav Singal#reactjs#react-js#nginx
// TL;DR

Add try_files $uri /index.html in the Nginx configuration to redirect all routes to index.html so React Router can handle client-side routing.

React JS router not working on Nginx docker container

Problem Statement

I developed a simple ReactJS application where I have used BrowserRouter, and have 4-5 different url paths, which was working fine on development server. I prepared a build using npm run build, and it created a build folder which can be served on an nginx server.

I have a Dockerfile which just take a Nginx base docker image and copy my build folder into it.

FROM nginx

COPY ./build /usr/share/nginx/html

I make a docker image using:

docker build -t my-test-app:latest .

But, when I ran it. Home page opens perfectly fine.

Nginx React

But, not other pages. As I clicked on other routes. I got a 404 page.

Nginx React

Solution

Since apps made by reactjs are single page app. All the routing is being handled by index.html page. But, nginx doesn’t know about this. It simply tries to find the file by that name, and the file doesn’t found. Hence the 404 error.

I had to change the default configuration of nginx. I ran the container again, copy the default configuration to my local copy.

docker cp <container_id>:/etc/nginx/conf.d/default.conf .

Look for the area where location / is defined. In my case, it was:

location / {
      root   /usr/share/nginx/html;
      index  index.html index.htm;
}

I had to tell nginx, that put all reqeust to index.html.

The change I did to default.conf

location / {
      root   /usr/share/nginx/html;
      index  index.html index.htm;
      try_files $uri /index.html;
}

What is try_files

It accepts a param $uri, the path coming in request. It checks, if it exists or not. If yes, pass it there. If not exists, try the URL we are passing.

Now my Dockerfile looks like:

FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
COPY ./build /usr/share/nginx/html

And, now prepare docker image, run it. And, it runs as expected.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How To Create Admin Subdomain In Cloudflare with Nginx Proxy using Docker with SSL

How To Create Admin Subdomain In Cloudflare with Nginx Proxy using Docker with SSL

Introduction I have my main website, which I run on Lets say: . Now, there is my…

Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

How to Copy Local Docker Image to Another Host Without Repository and Load

How to Copy Local Docker Image to Another Host Without Repository and Load

Introduction Consider a scenario where you are building a docker image on your…

How to Deploy Strapi with Next.js Frontend with Nginx Proxy and URL Redirect with Docker

How to Deploy Strapi with Next.js Frontend with Nginx Proxy and URL Redirect with Docker

Agenda I will cover following in this post: Prepare Docker image for Next.js app…

ReactJS - Understanding SetState Asynchronous Behavior and How to correctly use it

ReactJS - Understanding SetState Asynchronous Behavior and How to correctly use it

ReactJS setState is Asynchronous setState() method in ReactJS class components…

ReactJS - How to restrict data type for different kind of data

ReactJS - How to restrict data type for different kind of data

Introduction Javascript is not a strict type language. We can use a variable to…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…