Hello, I’m trying to deploy, my mern stack app, but it seems that something goes wrong, it is my first website, so can anyone help?
My server runs on the following ports:
API: localhost://5000 Frontend: localhost://3000 My server.js file looks like this:
const __dirname = path.resolve()
app.use('/uploads', express.static(path.join(__dirname, '/uploads')))
if(process.env.NODE_ENV === 'production'){
app.use(express.static(path.join(__dirname, '/frontend/build')))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'))
})
} else {
app.get('/', (req, res) => {
res.send('App Is Running...')
})
}
app.use(notFound)
app.use(errorHandler)
const PORT = process.env.PORT || 5000
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.listen(5000, () => {
console.log(`Server Is Running In ${process.env.NODE_ENV} Mode On Port ${PORT}`.yellow.bold)
})
When I deploy it, it doesn’t deploy successfully.
I Bought a node.js server and following the instructions that gave me a digital ocean,
When starting the server.js file:
root@obstacle-sports-node:~/Obstacle-Sports# pm2 start backend/server.js
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /root/Obstacle-Sports/backend/server.js in fork_mode (1 instance)
[PM2] Done.
root@obstacle-sports-node:~/Obstacle-Sports#
This is my NGINX configuration:
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name hellonode;
location ^~ /assets/ {
gzip_static on;
expires 12h;
add_header Cache-Control public;
}
location / {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:5000;
}
but when I am visiting IP address, I get This error
502 Bad Gateway
nginx/1.17.10 (Ubuntu)
please help :)
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!
Hello,
It appears that Nginx is not able to connect to your Node.js application running on port 5000, hence the 502 Bad Gateway error.
Let’s go through the steps to troubleshoot this issue:
1. Ensure Node.js App is Running:
Firstly, check if your Node.js application is running properly.
You can use the PM2 command pm2 logs to check the logs of your application. If there’s an issue, the logs should give you some indication of what’s going wrong.
pm2 logs
2. Check Listening Port:
Ensure that your Node.js application is listening on the correct port (5000) and that it’s the correct address (0.0.0.0).
In your server.js, where the app starts listening:
app.listen(5000, '0.0.0.0', () => {
console.log(`Server Is Running In ${process.env.NODE_ENV} Mode On Port ${PORT}`.yellow.bold)
})
3. Check Firewall Settings:
If your Node.js application is running correctly and is accessible on the server, it could be an issue with firewall settings.
To list all firewall rules:
sudo ufw status
If the firewall is not allowing traffic to port 5000, you can add a rule to allow it:
sudo ufw allow 5000
4. NGINX Configuration:
Your Nginx configuration looks fine, but let’s double-check it anyway. Here’s a basic setup:
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://localhost:5000;
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;
}
error_page 502 /502.html;
location = /502.html {
root /var/www/html;
internal;
}
}
This configuration will route all HTTP traffic to the backend server running on port 5000.
Don’t forget to replace your_domain.com with your actual domain name.
Once you’ve made changes, you can test the configuration:
sudo nginx -t
If the test is successful, reload Nginx:
sudo systemctl reload nginx
If these steps do not resolve the issue, I could suggest following the steps from this tutorial here on how to troubleshoot common Nginx problems:
https://www.digitalocean.com/community/tutorials/how-to-troubleshoot-common-nginx-errors
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.