When self-hosting, use Nginx for serving static assets to improve performance

Right now, The Ghost CLI sets up Nginx to forward all requests to Ghost.

There is one category of content, static content, which Nginx can serve much faster than Node. This both improves the user experience as well as lowering the CPU load. This would help make Ghost more resilient to traffic spikes or DoS attacks as well.

In the code base, the related code can be found by searching for express.static and serveStatic to find places where the Express middleware for serving static is used.

All this code would remain the same, for cases where Nginx isn’t involved, such as local development or cases when the Ghost CLI is not used.

But there would be new Ghost CLI logic which updates the template that generates the Nginx config file. Nginx would then implements the same logic for static file serving as is currently embedded in Ghost. Nginx would then serve the file directly and not pass the request to Ghost.

To make this easier to maintain, there could be a Ghost “include file” for Nginx which should never be touched by end users.

Ghost could re-generate the related Nginx include file during upgrades and self-hosters could be advised to run ghost setup nginx again if they modify a related config variable or change their storage adapter. This optimization would only apply to the “local storage” storage adapter.