'React App in Docker on OpenShift: Router returns HTTP Code 301

I have created an react App with react-router-dom as Router and this application runs inside a Docker container on OpenShift. For use of OpenShift I created an /health "endpoint" to check if application is healthy.

That is my Router Config:

<BrowserRouter>
    <Routes>
        <Route path="/" element={App} />
        <Route path="/page1" element={<ShowPage1 />} />
        <Route path="/health" element={ <h1>App is healthy!</h1> } />
    </Routes>
</BrowserRouter>

And here is my Dockerfile:

FROM registry.access.redhat.com/ubi8/nodejs-16 AS build
WORKDIR /workspace/
USER root
RUN npm install && \
    npm run build

FROM registry.access.redhat.com/ubi8/nginx-120 AS runtime
USER root
COPY /build/ /opt/app-root/src/
RUN echo "error_log /dev/stdout info;" > /usr/share/nginx/modules/error_log.conf && \
    echo "access_log /dev/stdout;" > /opt/app-root/etc/nginx.d/access_log.conf && \
    echo -e "location / { \n\ttry_files $uri $uri/ /index.html;\n}" > /opt/app-root/etc/nginx.default.d/config_route.conf
USER 1001
HEALTHCHECK CMD [ "service", "nginx", "status" ]
CMD nginx -g "daemon off;"

But when OpenShift calls the /health URL a HTTP Status 301 is given. OpenShift does not recognize that the service is healthy and the container keeps restarting.

Is there anything wrong with my Router Config? Or something inside my nginx Config in the Dockerfile? Or how to handle Health and Readines Checks with OpenShift and React?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source