grokut
By:
grokut

Unable to Reverse Proxy Nginx - 502 error

February 19, 2015 2.3k views

I am trying to reverse proxy my ghost blog so that port 80 redirects to port 2368. However, reaching the site at port 80 gives me a 502 Bad Gateway error.

This is a manual install of ghost on a Debian system.

I am running in production with pm2. I am able to connect to port 2368.

My conf file on nginx/conf.d/ reads:

server {
    listen 80;
    server_name levity.io;

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;
    }
}

My conf.js file for ghost reads:

// # Ghost Configuration
// Setup your Ghost install for various environments
// Documentation can be found at http://support.ghost.org/config/


var path = require('path'),

    config;

config = {

    // ### Production

    // When running Ghost in the wild, use the production environment

    // Configure your URL and mail settings here

    production: {

        url: 'http://levity.io',

        mail: {},

        database: {

            client: 'sqlite3',

            connection: {

                filename: path.join(__dirname, '/content/data/ghost.db')

            },

            debug: false

        },

        server: {

            // Host to be passed to node's `net.Server#listen()`

            host: '104.236.114.240',

            // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`

            port: '2368'

        }

    },

    // ### Development **(default)**

    development: {

        // The url to use when providing links to the site, E.g. in RSS and email.

        // Change this to your Ghost blogs published URL.

        url: 'http://localhost:2368',



        // Example mail config

        // Visit http://support.ghost.org/mail for instructions

        // ```

        //  mail: {

        //      transport: 'SMTP',

        //      options: {

        //          service: 'Mailgun',

        //          auth: {

        //              user: '', // mailgun username

        //              pass: ''  // mailgun password

        //          }

        //      }

        //  },

        // ```



        database: {

            client: 'sqlite3',

            connection: {

                filename: path.join(__dirname, '/content/data/ghost-dev.db')

            },

            debug: false

        },

        server: {

            // Host to be passed to node's `net.Server#listen()`

            host: '127.0.0.1',

            // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`

            port: '2368'

        },

        paths: {

            contentPath: path.join(__dirname, '/content/')

        }

    },



    // **Developers only need to edit below here**



    // ### Testing

    // Used when developing Ghost to run tests and check the health of Ghost

    // Uses a different port number

    testing: {

        url: 'http://127.0.0.1:2369',

        database: {

            client: 'sqlite3',

            connection: {

                filename: path.join(__dirname, '/content/data/ghost-test.db')

            }

        },

        server: {

            host: '127.0.0.1',

            port: '2369'

        },

        logging: false

    },



    // ### Testing MySQL

    // Used by Travis - Automated testing run through GitHub

    'testing-mysql': {

        url: 'http://127.0.0.1:2369',

        database: {

            client: 'mysql',

            connection: {

                host     : '127.0.0.1',

                user     : 'root',

                password : '',

                database : 'ghost_testing',

                charset  : 'utf8'

            }

        },

        server: {

            host: '127.0.0.1',

            port: '2369'

        },

        logging: false

    },



    // ### Testing pg

    // Used by Travis - Automated testing run through GitHub

    'testing-pg': {

        url: 'http://127.0.0.1:2369',

        database: {

            client: 'pg',

            connection: {

                host     : '127.0.0.1',

                user     : 'postgres',

                password : '',

                database : 'ghost_testing',

                charset  : 'utf8'

            }

        },

        server: {

            host: '127.0.0.1',

            port: '2369'

        },

        logging: false

    }

};

// Export config

module.exports = config;

Checking the nginx error logs, I find these messages:

2015/02/19 06:25:10 [error] 23331#0: *13 connect() failed (111: Connection refused) while connecting to upstream, client: 70.36.197.234, server: levity.io, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:2368/", host: "levity.io"
1 Answer

What's the exact command you are using to start Ghost?

It looks like you Nginx configuration is good. Make sure it's using --production or that NODE_ENV=production is set. Also update the host configuration for your production block to listen on 127.0.0.1

    production: {

        url: 'http://levity.io',
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },

        server: {
            // Host to be passed to node's `net.Server#listen()`
            host: '127.0.0.1',
            // Port to be passed to node's `net.Server#listen()`, for iisnode set this to `process.env.PORT`
            port: '2368'
        }

    }

Then restart Ghost. If you're still seeing not connecting correctly, could you share the output of sudo netstat -plunt ?

Have another answer? Share your knowledge.