Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react.js application showing 404 not found in nginx server

Tags:

nginx

reactjs

I uploaded react.js application to server. I'm using nginx server. Application is working fine. But when I go to other page & refresh, site is not working. It's showing 404 Not found error.

How can I solve this.

Thank You.

like image 265
Sunil tc Avatar asked Apr 22 '17 04:04

Sunil tc


People also ask

How do I fix error 404 in React?

An alternative way to handle 404 page not found in React router is to redirect the user to a different page when they try to go to a page that doesn't exist. Copied! In the example, instead of rendering a PageNotFound component, we redirect the user to / if they go to a route that doesn't exist.

What is 404 not found nginx?

Essentially, the “404 error” indicates that your or your visitor's web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.

Can Nginx serve React app?

NGINX is a powerful tool that we can use with React to leverage its awesomeness. Our target server can be any ubuntu server instance like EC2 Instance in AWS or a Droplet in Digital ocean or even your local machine.


Video Answer


2 Answers

When your react.js app loads, the routes are handled on the frontend by the react-router. Say for example you are at http://a.com. Then on the page you navigate to http://a.com/b. This route change is handled in the browser itself. Now when you refresh or open the url http://a.com/b in the a new tab, the request goes to your nginx where the particular route does not exist and hence you get 404.

To avoid this, you need to load the root file(usually index.html) for all non matching routes so that nginx sends the file and the route is then handled by your react app on the browser. To do this you have to make the below change in your nginx.conf or sites-enabled appropiately

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

This tells nginx to look for the specified $uri, if it cannot find one then it send index.html back to the browser. (See https://serverfault.com/questions/329592/how-does-try-files-work for more details)

like image 138
Panther Avatar answered Oct 05 '22 00:10

Panther


The answers given here are correct. But, I was struggling with this when trying to deploy my React Application in a docker container. The problem was on, how to change the nginx configs inside a docker container.

Step 1: Prepare your Dockerfile

# Stage 1 FROM node:8 as react-build WORKDIR /app COPY . ./ RUN yarn RUN yarn build  # Stage 2 - the production environment FROM nginx:alpine COPY nginx.conf /etc/nginx/conf.d/default.conf COPY --from=react-build /app/build /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] 

See line with command: COPY nginx.conf /etc/nginx/conf.d/default.conf. Here, we are telling Docker to copy the nginx.conf file from the docker host, to the docker container.

Step 2: Have a nginx.conf file in your application root folder

server {     listen       80;     server_name  localhost;      location / {         root   /usr/share/nginx/html;         index  index.html index.htm;         try_files $uri /index.html;                      }      error_page   500 502 503 504  /50x.html;     location = /50x.html {         root   /usr/share/nginx/html;     } } 

Step 3: Now build the docker image and run it

$ docker build . -t react-docker 

This should build your docker image successfully. To check that, run

$ docker images 

Now run

$ docker run -p 8000:80 react-docker  

and navigate to http://localhost:8000

This was inspired by this blog.

like image 44
Keet Sugathadasa Avatar answered Oct 05 '22 00:10

Keet Sugathadasa