I’m building a docker image and the React app routing is not working. When I click on the "/about"
route, it gives the “404 Not Found in nginx/1.27.1 error”. It works fine with npm start
. I’m using React-Router V6.
Here is my docker file:
FROM node:lts-alpine as builder
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
#COPY ./.nginx/nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
EXPOSE 443
ENTRYPOINT ["nginx", "-g", "daemon off;"]
Here is my nginx.conf file:
# nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#include /etc/nginx/conf.d/*.conf;
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' '*';
}
}
Here is my client-side routing:
return (
<div>
<NavigationBar />
<Routes>
<Route path="/" element={<Hero />} />
<Route path="/about" element={<About />} />
<Route path="/services" element={<Services />} />
<Route path="/team" element={<Team />} />
</Routes>
</div>
);
How do I go about making the routing work? I’m hoping to solve the the routing error.
I have managed to solve it.
COPY nginx.conf /etc/nginx/nginx.conf