配置Nginx加快Ghost.io博客的速度

Ghost is a hot new blogging platform. It’s simpler than WordPress, and exclusively focused on blogging (rather than being a full CMS, as WordPress has become). Like WordPress, you can sign up and host your blog on their site (for a price), or you can download and host Ghost yourself. We’ll focus on the latter.

Ghost是一个热门的新博客平台。 它比WordPress更简单,并且只专注于博客(而不是像WordPress一样成为完整的CMS)。 与WordPress一样,您可以注册博客并将其托管在其网站上(收费),也可以自己下载并托管Ghost 。 我们将重点放在后者上。

为什么是鬼? (Why Ghost?)

I recently set up a blog for my wife (who writes about our travels) on my virtual server. Said VPS is shared with quite a few other apps, so I wanted to keep things efficient. The most efficient way to serve a blog is to use a static site generator like Middleman or Jekyll, and serve your static resources with a web server like Nginx. That’s fine for me — my personal blog uses Middleman and I updated it with Git post-receive hooks — but my wife’s not so into the command line, so I thought Ghost might be a better fit.

我最近在虚拟服务器上为我的妻子 (写关于我们的旅行)建立了一个博客 。 说VPS与许多其他应用共享,所以我想保持效率。 服务博客的最有效方式是使用静态网站生成器(例如MiddlemanJekyll) ,并通过Nginx这样的Web服务器提供静态资源。 这对我很好-我的个人博客使用Middleman,并使用Gi​​t接收后挂钩对其进行了更新-但我的妻子不太喜欢进入命令行,因此我认为Ghost可能更合适。

Said server is also serving a bunch of other little sites and apps, so I needed to keep it lightweight.

所说的服务器还提供了许多其他小站点和应用程序,因此我需要保持它的轻量级。

Ghost is a very well-made and good-looking piece of software written in Node.js — but that doesn’t mean it’s perfect. Node is great for many things, but when it comes to serving up static files, that task can be handled more efficiently with Nginx. Nginx is a fast, lightweight web server that is gaining more and more popularity, and one of the things it’s best at is serving as a proxy in front of other apps.

Ghost是用Node.js编写的非常精美且美观的软件-但这并不意味着它是完美的。 Node在许多方面都很有用,但是在提供静态文件方面,可以使用Nginx更有效地处理该任务。 Nginx是一种快速,轻量级的Web服务器,正越来越受欢迎,它最擅长的功能之一是充当其他应用程序的代理。

Today, I’ll show you how I configured my server so that:

今天,我将向您展示如何配置服务器,以便:

  • Ghost serves up the admin pages (the only dynamic pages)

    Ghost提供了管理页面(唯一的动态页面)
  • Nginx serves static assets (images, JavaScript, CSS)

    Nginx提供静态资产(图像,JavaScript,CSS)
  • All non-admin pages are cached by Nginx, further reducing load

    所有非管理员页面均由Nginx缓存,从而进一步减轻了负载

All this will let my server’s resources go a lot farther. Plus, since Ghost ends up doing almost no work, I can leave it configured to use SQLite 3 instead of something more suited for parallel connections, which is a nice bonus.

所有这些将使我的服务器的资源走得更远。 另外,由于Ghost几乎不做任何工作,因此我可以将其配置为使用SQLite 3,而不是更适合并行连接的东西,这是一个不错的好处。

在我们继续之前 (Before We Continue)

The process of installing Nginx and Ghost is beyond the scope of this article. For Nginx, you might try Nginx’s docs; for Ghost, you can follow Ghost’s installation instructions.

Nginx和Ghost的安装过程不在本文讨论范围之内。 对于Nginx,您可以尝试Nginx的docs ; 对于Ghost,您可以按照Ghost的安装说明进行操作

Configure Ghost to run on port 2368. We’ll proxy port 80 (i.e. the default http port) to it with Nginx. We’ll also assume you’re running on example.com.

将Ghost配置为在端口2368上运行。我们将使用Nginx将端口80(即默认的http端口)代理给它。 我们还将假设您正在example.com运行。

配置Nginx服务静态资产 (Configuring Nginx to Serve Static Assets)

To start with, you should have this bare-bones Nginx configuration:

首先,您应该具有以下基本的Nginx配置:

server{
    listen 80;
    server_name example.com www.example.com;
}

First, we’ll make sure to serve the blog, which is running on port 2368. To do this, simply add the following:

首先,我们将确保为运行在端口2368上的博客提供服务。为此,只需添加以下内容:

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

Now, make sure Ghost is running, and restart Nginx. When you navigate to your home page at example.com, you should see a blank Ghost blog.

现在,确保Ghost正在运行,然后重新启动Nginx。 当您导航到example.com上的主页时,应该会看到一个空白的Ghost博客。

We want to serve images and assets with Nginx. Ghost hides these around in a few different places, so you’ll need to make a few aliases. Add the following (changing the path to your Ghost install, and to your theme, appropriately):

我们希望通过Nginx提供图像和资产。 Ghost将它们隐藏在几个不同的位置,因此您需要创建一些别名。 添加以下内容(相应地更改Ghost安装路径和主题的路径):

location /content/images {
    alias /path/to/ghost/content/images;
}

location /assets {
    alias /path/to/ghost/content/themes/<mytheme>/assets;
}

location /public {
    alias /path/to/ghost/core/built/public;
}

location /ghost/scripts {
    alias /path/to/ghost/core/built/scripts;
}

设置静态站点样式缓存 (Setting Up Static Site-style Caching)

In order to use up as few cycles as possible, we want to be really aggressive with caching. So, for all the non-admin pages, we’re going to configure Nginx to ignore cache-control headers entirely and just cache each page for an hour.

为了尽可能少地使用周期,我们希望在缓存方面更加主动。 因此,对于所有非管理员页面,我们将配置Nginx以完全忽略缓存控制标头,而仅将每个页面缓存一个小时。

Take out the other proxy_pass block. We’ll need to split it into two. The first will proxy requests to the admin backend, which we must not cache. The second will be the rest, which will be cached with a vengeance. Here’s what that looks like:

取出另一个proxy_pass块。 我们需要将其分为两部分。 第一个将请求代理到管理后端,我们不能缓存。 第二个是剩下的,将被复仇地缓存。 看起来像这样:

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

location / {
    proxy_cache STATIC;
    proxy_cache_valid 200 60m;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:2368;
}

Ignoring the headers is optional, but we don’t mind clearing out the cache ourselves every now and then if it really needs it.

忽略标头是可选的,但是我们不介意偶尔一次真正地清除高速缓存。

Speaking of which, you’ll also want to configure Nginx’s cache path. Above, we’ve called our configuration STATIC, so we need to add this line to our Nginx configuration’s http block (I put mine in /etc/nginx/nginx.conf):

说到这,您还需要配置Nginx的缓存路径。 上面,我们将配置称为STATIC ,因此我们需要将此行添加到Nginx配置的http块中(我将我的内容放在/etc/nginx/nginx.conf ):

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=512m;

This tells Nginx to keep cache files at the path /data/nginx/cache, and sets the levels and various configurations. If you ever want to clear the cache, just execute rm -r /data/nginx/cache/* to remove all the cached files.

这告诉Nginx将缓存文件保留在/data/nginx/cache路径中,并设置级别和各种配置。 如果要清除缓存,只需执行rm -r /data/nginx/cache/*即可删除所有缓存的文件。

一切都在一起 (Everything All Together)

Here’s the whole configuration, for reference:

这是整个配置,以供参考:

server{
    listen 80;
    server_name example.com www.example.com;

    location /content/images {
        alias /path/to/ghost/content/images;
    }

    location /assets {
        alias /path/to/ghost/content/themes/<mytheme>/assets;
    }

    location /public {
        alias /path/to/ghost/core/built/public;
    }

    location /ghost/scripts {
        alias /path/to/ghost/core/built/scripts;
    }

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

    location / {
        proxy_cache STATIC;
        proxy_cache_valid 200 60m;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
        proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;
    }

}

And that’s really all there is to it. With this setup, you should be able to serve many thousands of visitors from even a small private server.

这就是全部。 通过此设置,即使从小型私人服务器,您也应该能够为成千上万的访客提供服务。

翻译自: https://www.sitepoint.com/configuring-nginx-speed-ghost-blog/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值