Ghost Blog - Nginx Location@Content

Hi All,

I’m struggling with my configuration (Nginx-Ghost).
I added some location blocks to stop some of the requests being passed back to Node and have Nginx serve them itself but it doesn’t work.

I tried to update my “Publication Cover” (background image) but this one is not displayed however I’m able to view it if I copy/past the path(url) in my browser.
Everything works fine when I remove those block locations.

Did I miss something ?

What’s your URL? Internal Network
What version of Ghost are you using? 2.25.4
What configuration? Ubuntu 18.04
What browser? Firefox/Chrome
What errors or information do you see in the console? None
What steps could someone else take to reproduce the issue you’re having?
-Install Ghost
-Copy/paste my Nginx configuration
-Try to update your publication cover
server {
	listen 80;
	listen [::]:80;
        server_name internal.tld;

## NGINX## <-- ISSUE w/ Content

location ^~ /assets/ {
    root /var/www/ghost/content/themes/casper/;
}

location ^~ /content/images/ {
    root /var/www/ghost/;
}


#PROXY#
        location / {
	proxy_set_header Host $http_host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Forwarded-Proto $scheme;
	proxy_pass http://127.0.0.1:2368;

}


}
location ^~ /content/images/ {
    root /var/www/ghost/;
}

This could cause you problems if you’re using dynamic image resizing. You probably want to exclude /content/images/size/* otherwise the request won’t get through to Ghost for it to generate the resized image files.

I have not tested this at all but you might be able to use nginx’s try_files module to look at the local filesystem first then fall back to proxying to your Ghost instance.

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

It’s definitely possible, I’ve set up a proxy like that in the past (though it was to Netlify instead of Ghost) -

Thank you very much for your quick anwsers ! @Kevin @vikaspotluri123
I will have a look to nginx's try_files and will keep you posted.

For my understanding is it a “good pratice” to run a Ghost Blog in a such way or should I consider an other option ? (Proxy Cache etc…)

1 Like

I’ve been experimenting with advanced nginx configurations recently. I haven’t given this a thorough testing in production yet, so use at your own risk, but this is what I believe to be the correct config:

location ^~ /assets/ {
    root /var/www/ghost/content/themes/casper/;
    add_header Cache-Control 'public,max-age=31536000';
    try_files $uri ghost;
}

location ^~ /content/images/ {
    root /var/www/ghost/;
    add_header Cache-Control 'public,max-age=31536000';
    try_files $uri @ghost;
}

location / {
    try_files _ @ghost;
}

location @ghost {
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://127.0.0.1:2368;
}

We change the default location block to be a named blog @ghost. This allows us to reference it elsewhere.

For the default block, we simply forward straight to the named @ghost block, but try_files requires two args. Using _ to force try_files straight to the second argument is the accepted workaround.

Finally we add 2 blocks, one for assets and one for images. Each defines the root folder where files should be found, and uses try_files to check $uri (path match) first, and fallback to @ghost.

For files that are found on disk, there will be no cache control header, as Ghost doesn’t serve the file, so I’ve added a one year cache control header. These files shouldn’t change without the name/query param changing, so it should be fine to cache for this long.

This will allow Ghost to generate image sizes on request, and then serve them from disk thereafter.

It still requires a manual update if the theme name changes.

2 Likes