Question

502 Bad Gateway error for Ghost after setting up server block for static site and restarting Nginx.

Hello there,

I recently installed Ghost via the One Click Application and tried to host it on a subdomain blog.my-domain.com. I created an Nginx server block for the static site to point to www.my-domain.com which is working fine. But when I try to access the Ghost Blog I get a 502 Bad Gateway error.

The two directories configured to serve the websites are:

  1. /var/www/ghost for the blog which was there by default
  2. /var/www/my-domain.com/html for the static website

The server block config file for ghost is at /etc/nginx/sites-enabled/ghost

server {
	listen 80;
	listen [::]:80;

	root /var/www/ghost;
	index index.html index.htm;

	server_name blog.my-domain.com www.blog.my-domain.com;
	
    	location / {
        	proxy_set_header X-Real-IP $remote_addr;
        	proxy_set_header HOST $http_host;
        	proxy_set_header X-NginX-Proxy true;
       		proxy_pass http://127.0.0.1:2368;
       		proxy_redirect off;
   	 }
}

The server block config for the static site is at /etc/nginx/sites-enabled/my-domain.com

server {
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;

	root /var/www/my-domain.com/html;
	index index.html index.htm;
	server_name my-domain.com www.my-domain.com;

	location / {
		try_files $uri $uri/ =404;
	}
}

The Ghost config.js file which is at /var/www/ghost/ contains

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://blog.my-domain.com',
        mail: {},
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost.db')
            },
            debug: false
        },

        server: {
            host: '127.0.0.1',
            port: '2368'
        }
    },

        // #### Database
        // Ghost supports sqlite3 (default), MySQL & PostgreSQL
        database: {
            client: 'sqlite3',
            connection: {
                filename: path.join(__dirname, '/content/data/ghost-dev.db')
            },
            debug: false
        },
        // #### Server
        // Can be host & port (default), or socket
        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
        // Specify where your content directory lives
        paths: {
            contentPath: path.join(__dirname, '/content/')
        }
    }
};

module.exports = config;

I also tried to recreate the symlink by removing the old one and creating it again using

sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost

I restarted the server using sudo service nginx restart and also ghost with sudo service ghost restart

When I checked it status of ghost with sudo service ghost status , it says it’s running.

The Nginx Logs can be found here

Any help would be much appreciated!

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Accepted Answer

Got it working again! Somehow the permissions had changed. (Realization after reading the Ghost log and doing some research) So running,

chown -R ghost:ghost /var/www/ghost

and then restarting the Ghost service got it to work again!

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.