Set up nginx proxy for react application

fugasjunior

I'm trying to create a docker-compose using two services, a Spring Boot backend (running on port 8080) and React frontend running on Nginx.

The react app calls backend API like /api/tests. However, when I run the docker compose and frontend makes a request, it always fails with 404 error: GET http://localhost/api/tests 404 (Not Found)

When I set the frontend dockerfile not to use Nginx, just npm start, it worked fine, but I would prefer using production build on Nginx.

Current frontend dockerfile:

FROM node:11.13 as builder

RUN mkdir /usr/src/app
WORKDIR /usr/src/app

ENV PATH /usr/src/app/node_modules/.bin:$PATH

COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install [email protected] -g

COPY ./package-lock.json /usr/src/app/
COPY ./public /usr/src/app/public
COPY ./src /usr/src/app/src

COPY ./nginx.conf /etc/nginx/nginx.conf

RUN npm run build

FROM nginx:1.15.10-alpine
COPY --from=builder /usr/src/app/build /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]

Nginx.conf:

server {
    listen 80;

    location / {
        try_files $uri $uri/ /index.html;
        add_header   Cache-Control public;
        expires      1d;
    }

    location /api {
       proxy_set_header X-Forwarded-Host $host;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_pass http://server:8080/;
    }
}

docker-compose:

version: "3"

services:
  server:
    build: test-server/
    expose:
      - 8080
    ports:
      - 8080:8080
  ui:
    build: test-ui/
    expose:
      - 80
    ports:
      - 80:80

The react app has a line "proxy": "http://server:8080" in its package.json.

Nginx logs the following error:

2019/04/15 12:50:03 [error] 6#6: *1 open() "/usr/share/nginx/html/api/tests" failed (2: No such file or directory), client: 172.20.0.1, server: localhost, request: "GET /api/tests HTTP/1.1", host: "localhost", referrer: "http://localhost/"
fugasjunior

I found the problem. In the multi-stage build of the docker image, I accidentally copied the nginx.conf file into the builder image, not the production one.

The fixed Dockerfile now looks like this:

# build environment
FROM node:11.13 as builder

RUN mkdir /usr/src/app
WORKDIR /usr/src/app

ENV PATH /usr/src/app/node_modules/.bin:$PATH

COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install [email protected] -g

COPY ./package-lock.json /usr/src/app/
COPY ./public /usr/src/app/public
COPY ./src /usr/src/app/src


RUN npm run build

# production environment
FROM nginx:1.15.10-alpine
COPY --from=builder /usr/src/app/build /var/www
COPY ./nginx.conf /etc/nginx/nginx.conf
CMD ["nginx", "-g", "daemon off;"]

and nginx.conf:

server {
    listen 80;
    include /etc/nginx/mime.types;
    root /var/www;
    index index.html index.htm;
    location /api {
        resolver 127.0.0.11;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://server:8080$request_uri;
    }
    location / {
        try_files $uri $uri/ =404;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set up Nginx reverse proxy on the same port as the application?

Nginx Proxy Pass Nodejs to React Application

Set up proxy server for create react app

NGINX/JENKINS: It appears that your reverse proxy set up is broken

How should I set up a reverse proxy with nginx?

node.js & nginx proxy set up -- something is wrong, but what?

proxy jboss application with nginx

Set up proxy for selenide?

Nodejs won't set cookie for React CRA application even with proxy

How do I set up a stand-alone Gunicorn App server if I already have an Nginx proxy set up?

How do I set up an nginx conf with a subdomain and reverse_proxy and NO use of sites-enabled?

I can't disable Prometheus port 9090 (default) after set up a reverse proxy in Nginx

How do I set up a container without a subdomain using nginx-proxy?

Set up a proxy with python requests

Set up proxy on selenium geckodriver

How can we set up .env in react native application?

How to set up nested routes in react-redux application?

Set Virtual Path to ASP NET Core MVC Application behind nginx reverse proxy

How to set up a proxy in React app without create-react-app when deploying to Heroku

Setting up Jenkins with Nginx reverse proxy

Application on nginx reverse proxy redirects wrong

dotnet application in GKE with nginx ingress controller and proxy

Nginx unable to proxy django application through gunicorn

Springboot application with nginx as proxy deploy on Heroku

Setting up proxy headers in IIS for flask application

How to set nginx reverse proxy in remote server

How to set up terraform behind proxy?

How to set up a proxy for Webpack in production

Set up Firebase admin sdk with proxy (Java)