By shivaprasad
Hi Team,
We have developed app frontend as React js which has a URL of https://localhost:3000 and node on https://localhost:4000, as per documentation we have configured nginx reverse proxy as below in file path: /etc/nginx/sites-available/default
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location / app2 {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
so when we make an api calls to node js from react.
example
axios.get('https://localhost:4000/app2/sparters')
.then(res => console.log(res.data))
we are getting error as below : GET http://localhost:4000/app2/spartners 404 not found
Kindly assist on this issue to fix it as soon as possible , so are we doing something wrong or some thing is missing here.
Thanks and Regards, Shivaprasad.G
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there,
The issue you’re facing seems to be related to the way you’ve set up the Nginx reverse proxy and how you’re making requests from your React frontend.
Let’s diagnose and fix the issue step-by-step:
Nginx Configuration:
In your location /app2 block, you’re proxying to http://localhost:4000. This means that any request that comes to https://yourdomain.com/app2/* will be proxied to http://localhost:4000/*.
You should modify the proxy pass so it strips the /app2 prefix:
location /app2/ {
proxy_pass http://localhost:4000/;
...
}
location directive and the proxy_pass directive; they’re important.React Frontend Axios Call:
Instead of making a request directly to https://localhost:4000/app2/sparters, you should make the request to /app2/sparters.
Example:
axios.get('/app2/sparters').then(res => console.log(res.data))
Ensure Nginx Configuration is Reloaded: After you make changes to the Nginx configuration, ensure you reload (or restart) Nginx for the changes to take effect:
sudo nginx -s reload
CORS Configuration: Make sure your Node.js backend (running on localhost:4000) has CORS headers configured to allow requests from your frontend (especially if they’re on different origins or if you ever move to a non-localhost setup). You can use the cors middleware for Express.js if you’re using that framework.
Use HTTPS: Your Axios request is using https, but your Nginx configuration is proxying to an http backend. Ensure that:
mkcert to generate trusted localhost certificates.Check Browser Developer Console: Always check your browser’s developer console for any additional details or errors that might provide more information about what’s going wrong.
Nginx Logs: Look into the Nginx error and access logs (/var/log/nginx/error.log and /var/log/nginx/access.log). These logs might have more information about why a request is being returned with a 404 status.
On another note, serving a React application as a static site through Nginx is a common setup and is actually simpler than proxying to a service. Running react as a Node.js service is more commonly done for development environments.
React Build: Start by building your React application. This will generate a build directory that contains static files suitable for serving with Nginx.
Navigate to your React project directory and run:
npm run build
Move the Build to Nginx’s Serving Directory: After the build completes, move or copy the build directory contents to the location where Nginx expects static files, typically /var/www/html or another directory of your choice:
sudo cp -r build/* /var/www/html/
Nginx Configuration: Update the Nginx configuration to serve the React static files and to proxy requests to your backend service. Edit the configuration file located at /etc/nginx/sites-available/default:
server {
listen 80;
root /var/www/html; # Path to your React app's build directory
index index.html;
server_name your_domain_or_ip;
location / {
try_files $uri $uri/ =404;
}
location /app2/ {
proxy_pass http://localhost:4000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Make sure to adjust your_domain_or_ip to your server’s domain or IP address.
sudo nginx -s reload
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.