D0060ed3b0d6a3cce027d644bff338f3a7a6770c
By:
marmot7

Bad Gateway error when I restart server

February 21, 2017 652 views
Miscellaneous CentOS

My server has to domains hosted:

  1. One I'm using for a blog with Ghost on Nginx.
  2. One just an ordinary site with HTML and CSS files, etc.

Whenever I reboot my droplet, it comes back with this error on the Nginx/Ghost site.

502 Bad Gateway

nginx/1.10.2

Any ideas on what this might be happening? I fixed this the first time it happened but I made the mistake of not documenting what I did to fix it. Whatever I did to fix it was just a temporary fix because ideally this would work on restart without having to do anything. Thank you.

2 Answers
hansen February 21, 2017
Accepted Answer

Have a look in the log file - if you haven't changed that, it should be in
/var/log/nginx/error.log

  • Thank you. Here's the end of the error log. I'm not sure what this means. Can you help me narrow down the problem? Thank you.

    2017/02/21 06:52:46 [error] 2233#0: *2 no live upstreams while connecting to upstream, client: 24.20.119.244, server: example.tech, request: "GET /favicon.ico HTTP/1.1", upstream: "http://localhost/favicon.ico", host: "example.tech", referrer: "https://example.tech/"
    2017/02/21 06:56:41 [error] 2233#0: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 24.20.119.244, server: example.tech, request: "GET / HTTP/1.1", upstream: "http://[::1]:2368/", host: "example.tech"
    2017/02/21 06:56:41 [error] 2233#0: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 24.20.119.244, server: example.tech, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:2368/", host: "example.tech"
    2017/02/21 06:56:41 [error] 2233#0: *35 no live upstreams while connecting to upstream, client: 24.20.119.244, server: example.tech, request: "GET /favicon.ico HTTP/1.1", upstream: "http://localhost/favicon.ico", host: "example.tech", referrer: "https://example.tech/"
    2017/02/21 06:56:43 [error] 2233#0: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 24.20.119.244, server: example.tech, request: "GET / HTTP/1.1", upstream: "http://[::1]:2368/", host: "example.tech"
    2017/02/21 06:56:43 [error] 2233#0: *35 connect() failed (111: Connection refused) while connecting to upstream, client: 24.20.119.244, server: example.tech, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:2368/", host: "example.tech"
    2017/02/21 06:56:43 [error] 2233#0: *35 no live upstreams while connecting to upstream, client: 24.20.119.244, server: example.tech, request: "GET /favicon.ico HTTP/1.1", upstream: "http://localhost/favicon.ico", host: "example.tech", referrer: "https://example.tech/"

    • You have an upstream backend configured in Nginx referencing localhost:2368 - correct?
      What is that service? Ruby, Node, ...?
      That's your problem - you need to make sure that service is running. So we just need to auto-start that on reboot.

      • I'm running Ghost on Nginx and yes you're probably right one of those is probably not set to start on boot. Does that answer the question Ghost and a Nginx web server?

        • Okay, Ghost seems like it's a Nodejs app, so you need to install PM2 to setup auto-start of Nodejs and it's apps.
          Have a look at this: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

          Node.js is an open source JavaScript runtime environment for easily building server-side and networking applications. Node.js applications can be run at the command line but this guide focuses on running them as a service using PM2, so that they will automatically restart on reboot or failure, and can safely be used in a production environment.
          • Yes, I had installed Node. sorry. Didn't mention that. And it worked. I must not have set it to start on boot as you suggest.

          • I just ran pm2 start Ghost and got an error about a missing script. I will look at article above but I thought I'd already setup Node and everything worked perfectly until I shut down the server. LOL.

            [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
            [PM2] PM2 Successfully daemonized
            [PM2][ERROR] script not found : /root/Ghost
            script not found : /root/Ghost
            ┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────────┐
            │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
            └──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────────┘

          • I use Centos7 but I'll look for similar node docs for centos 7

          • After you run "pm2 start yourapplication.js" then you have to use "pm2 save" and then "pm2 startup" - then you can restart you server.

Here's what ended up working
$ cd /var/www/ghost && npm install --production
$ npm start --production

I want to make sure this will survive a reboot so I'm going to try again. Thanks for working with me on this today as I would have been completely lost without . your help.

Have another answer? Share your knowledge.