Shevtsov
Shevtsov

Reputation: 140

Is there way to run React app in nginx(Docker, nginx knowledge required)

I have react app and nodejs api server. React app make fetch requests to nodejs server. React app and nodejs server deployed in own containers.

The problem is I can access nodejs server directly in browser, so is there way to 'hide' nodejs backend server, and let access only through frontend.

It should work something like this

  1. React app make fetch request
  2. nginx intercept request and forward to nodejs server
  3. nodejs handles request

I think it can be done with nginx reverse proxy or docker networks or somehow...

Upvotes: 0

Views: 353

Answers (1)

Rami
Rami

Reputation: 530

yes there is. What you do is run a docker-compose that runs 3 docker containers. One container runs nginx, the second one runs create-react-app host ui, and the 3rd one runs a node js api. You then set the nginx routing rule for all /api/* routes to be reverse proxied to the nodejs api then you make sure all other get requests /* go to the create-react-app being hosted.

Here is a similar example on medium: https://medium.com/@xiaolishen/develop-in-docker-a-node-backend-and-a-react-front-end-talking-to-each-other-5c522156f634

Upvotes: 1

Related Questions