Search code examples
node.jsnginxghost-blog

Ghost as an npm module not serving assets?


I am using ghost as an npm module on an existing node app, basically it's a child app.

So my app runs on port 9200, I have setup a reverse proxy for that.

 location / {
    proxy_pass http://localhost:9200;
    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;
}

Then I configured my ghost app inside my main app.

// server.js
ghost().then(function (ghostServer) {
    app.use(ghostServer.config.paths.subdir, ghostServer.rootApp);

    ghostServer.start(app);
});

// node_modules/ghost/config.js
production: {
    url: 'http://example.com/blog',
    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'
    },
    paths: {
      contentPath: path.join(__dirname, '/blog/')
    }
}

Since ghost handles the /blog route the assets path expects /blog to be in the route, so I had to change it from /content.

This works fine at http://example.com:9200/blog, but after setting up a reverse proxy for /blog

location /blog {
    proxy_pass http://localhost:9200;
    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;
}

Then trying to go to http://example.com/blog all I can get is the html, the assets are not being served on this route, I suspect the location needs to include a wildcard like location /blog/*?


Solution

  • The following helped me solve my problem.

    Nginx - reverse proxy a Ghost blog with /subfolder redirect

    http://www.allaboutghost.com/how-to-install-ghost-in-a-subdirectory/

    location ^~ /blog {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
    
        proxy_pass http://127.0.0.1:2368;
        proxy_redirect off;
    }