504 Gateway time-out, nginx, nodejs

Posted June 3, 2018 22.1k views
NginxNode.jsUbuntu 16.04

First of all. The autorenew of my SSL certificate failed for some unknown reason. After re-installing the ssl certs from let’s encrypt, nginx gave me 504 gateway-timeout. I checked the error log and this appear.

2018/06/03 12:37:32 [error] 9128#9128: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client:, server:, request: "GET / HTTP/2.0", upstream: "", host: ""

So I have nginx reverse proxy for a node js app.
This is my nginx config:

# HTTP — redirect all traffic to HTTPS
server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;

# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;

    # Include the SSL configuration from
    include snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:3000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;

My node app is running with pm2

this is the nginx status:

nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-06-03 12:35:25 UTC; 16min ago
  Process: 8883 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/ (code=exited, status=0/SUCCESS)
  Process: 9122 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 9118 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 9127 (nginx)
    Tasks: 2
   Memory: 2.1M
      CPU: 66ms
   CGroup: /system.slice/nginx.service
           ├─9127 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─9128 nginx: worker process

But I think nginx is not the problem, for some reason my app is not working, but I don’t know how to debug it, thanks for any help

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

This happens due to lack of RAM.......check this by using pm2 log or pm2 monit commands........