Facebook Pixel

Can I host frontend and backend on the same EC2 instance?

Yes. Configure Nginx to serve static files from the React build directory for the root path, and proxy /api/ and /socket.io/ to the Node.js backend on port 3000. This works well for small to medium apps. For larger apps, separate them for independent scaling.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Deploying a React App to EC2 with Nginx Serving the Frontend from the Same Server

Build the React app (npm run build), transfer the dist/ or build/ directory to EC2, configure Nginx with root pointing to the build directory and try_files $uri $uri/ /index.html for client-side routing. Proxy /api/ requests to the Node.js backend on port 3000.

try_files $uri $uri/ /index.html tells Nginx to fall back to index.html for any path that doesn't match a static file. Without this, navigating directly to /profile or /connections returns 404 because Nginx looks for a physical file at that path.

Add a location block matching file extensions (js, css, png, jpg, etc.) with expires 1y and Cache-Control 'public, immutable'. React build files have hashed names, so they can be cached aggressively the hash changes when content changes.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.