Works for a couple of hours and then I receive a 504 gateway timeout error on the backend of the application.
EC2 instance is running ubuntu with nginx and PM2.
/etc/nginx/sites-available
.conf file:
server {
listen 80;
server_name mydomain.com;
root /home/ubuntu/app;
index index.html;
access_log /var/log/nginx/app.access.log;
error_log /var/log/nginx/app.error.log;
location / {
try_files $uri /index.html =404;
}
}
server {
listen 8080;
server_name mydomain.com;
location / {
proxy_pass http://127.0.0.1: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;
proxy_redirect off;
}
}
I believe my configuration is correct as it works correctly, but only for a few hours. Then I get the error. PM2 instance is still online and running. I think it's probably the node app crashing for some reason, but how can I troubleshoot this on ubuntu EC2? It works perfectly on my local machine.
Any suggestions would be appreciated.
Spent hours on this. Turns out PM2 and Nginx don't always work well together, changing the Nginx config and restarting Nginx and pm2 eventually fixed the problem for me.
Specifically, adding these two lines:
proxy_set_header Connection '';
keepalive_timeout 10;