ultimo_frogman
ultimo_frogman

Reputation: 91

Deploy ReactJS & ExpressJS to Nginx

I have a typical Ubuntu VPS setup (20.04LTS) where I have installed the Nginx Server. I have two local repos with front-end reactJS code repo and back-end ExpressJS code repo. I will start the ExpressJS on the 3000 port with forever start. I have also a mysql db. I have configured Nginx to server the static files with the root /var/www/[Your repo name]/build; and I can open the html files and it is working. The question is, do I need to start on another port for an example the ReactJS npm run start or is Nginx enough? Could you help me out with some links or best practices?

Thanks in advance.

Upvotes: 0

Views: 3181

Answers (2)

Vijay122
Vijay122

Reputation: 964

All the static files like index.html and assets will be served from your "root /var/www/[Your repo name]/build" folder, when the user opens your base url and sent as response for the get call. From your code, make sure to append '/api/' to all your backend requests from ui, so that it will get forwarded to your service running on port 3030.

Upvotes: 0

StackedQ
StackedQ

Reputation: 4139

If you're using CRA (create react app), it ships with a build script, so after running npm run build the whole app is built into static files including a index.html and some js and css files. all you need to do is to config nginx to serve that index.html. so nginx is enough for that. if you're using react-router in your app keep in mind that you may need to use try_files directive of nginx for serving that index.html for any incoming requests.

for more information about react-router and nginx see this.

If you're doing SSR (Server Side Rendering) you need a process manager like pm2 or forever to serve your app internally and proxy_pass directive of nginx to reverse proxy incoming requests to your app. More info

Upvotes: 1

Related Questions