Question

Deploying nuxt with pm2

Posted June 17, 2021 897 views
NginxUbuntuNode.js

I am trying to deploy a nuxtJs application together with pm2.

When I access the website, I get the classic nginx 502 bad gateway error message, and quiet honestly- Im clueless why.

Nginx configuration file:

server {
    index index.html;
    server_name subdomain.somedomain.com;

    location / {
        proxy_pass http://subdomain.somedomain.com:3001;
        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;
    }



    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/subdomain.somedomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/subdomain.somedomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = subdomain.somedomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}

Output of my nginx errors:

2021/06/17 21:48:19 [error] 20202#20202: *134 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xxx.xxx.xxx, server: subdomain.somedomain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://164.90.164.216:3001/favicon.ico", host: "subdomain.somedomain.com", referrer: "https://subdomain.somedomain.com/"

Output of my pm2 list:

id  │ name           │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 5   │ subdomainApp    │ default     │ 2.15.3  │ cluster │ 20299    │ 14m    │ 0    │ online    │ 0.3%     │ 75.7mb   │ root     │ enabled  │
│ 6   │ subdomainApp    │ default     │ 2.15.3  │ cluster │ 20306    │ 14m    │ 0    │ online    │ 0.3%     │ 76.0mb   │ root     │ enabled

No errors when I try to reload nginx, build the nuxt app or other stuff like that. Im honestly out of options regarding debugging. Any suggestions please?

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

Hi there @soduno,

I would recommend checking if the service is actually listening on port 3001 by running the following netstat command:

netstat -plant | grep 3001

Feel free to share the output here.

Also I would recommend changing the following line from:

        proxy_pass http://subdomain.somedomain.com:3001;

To:

        proxy_pass http://127.0.0.1:3001;

That way the traffic would not be routed externally but would be proxied locally so if you have a firewall blocking the port it would not cause any issues.

Once you make the Nginx change make sure to run a config test with the following command:

sudo nginx -t

And if you get Syntax OK message, then restart Nginx:

sudo systemctl restart nginx

Let me know how it goes.
Regards,
Bobby

  • Okay so far so good, thanks for your suggestions!

    netstat -plant | grep 3001, returns the node process as expected.

    This is the output of the pm2 logs:
    https://imgur.com/a/nHEDY6t

    I have rebuilded the nuxt app with npm run build just in case because of the error, and it looks like it’s listening well on port 3001.

    Is there some kind of port configuration I need to make when it’s another port than the default 3000?

    The url is throwing bad gateway again. It’s super weid.

    Full output of grep 3001:

    /etc/nginx/sites-available# netstat -plant | grep 3001
    tcp        0      0 164.90.164.216:3001     0.0.0.0:*               LISTEN      9229/PM2 v4.4.0: Go
    

    And nginx file after your suggestions:

    [location / {
            proxy_pass http://127.0.0.1:3001;
            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;
        }](http://)
    
    • Hi there,

      This looks correct. Maybe one thing that I could suggest is to change the listen address for your service from 164.90.164.216:3001 to 0.0.0.0:3001.

      Let me know how this goes.
      Regards,
      Bobby