首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在Ubuntu 13.10上使用Varnish和Nginx配置集群Web服务器

如何在Ubuntu 13.10上使用Varnish和Nginx配置集群Web服务器

原创
作者头像
楠宝宝
修改2018-07-30 17:32:07
1K0
修改2018-07-30 17:32:07
举报

介绍

关于集群Web服务器

集群Web服务器是Web托管中使用的一种技术,用于在多个计算机或“节点”之间分配负载。此技术的目的是消除单点故障并提高网站可用性和正常运行时间。通常,Web群集将使用多个后端和前端节点。

集群不一定非常昂贵且开始非常容易 -本教程将演示如何使用Nginx和Varnish创建循环双节点集群Web服务器。

关于Varnish

Varnish是一个HTTP加速器;换句话说,一个缓存服务器。它允许我们通过指导由Varnish维护和生成的网站的静态副本来加速网站。

关于Nginx

Nginx是一个轻量级,高性能的HTTP服务器,将作为Varnish的后端服务。它不会直接为访问者提供网站服务;但是,只要需要构建缓存,它就会响应Varnish的请求。

设置

要执行本教程中的步骤,您将需要三个最小为512mb的实例。

建议命名实例的主机名如下:

  • varnish
  • nginx01
  • nginx02

当然你可以添加任意数量的“nginx0x”,但是在本教程中我将坚持使用2。

在初始SSH进入三个新创建的实例时,执行以下命令:

sudo apt-get update

第一步 - 安装Nginx

Nginx是负责将我们的网站提供给Varnish的软件。

为您的varnish实例跳过此步骤。您必须在nginx01和nginx02实例上安装它,这意味着在您希望使用的每个nginx0x服务器上重复此过程。

建议从源代码安装Nginx,以确保我们获得最新版本。

Nginx有两个主要依赖项:PCRE(Perl兼容的正则表达式库)和zlib(压缩库)。在编写本指南时,最新版本是:

Nginx:1.4.4

PCRE:8.34

zlib:1.2.8

我们现在必须下载上面的源代码,准备提取和构建; 分别输入以下每个命令:

wget http://nginx.org/download/nginx-1.4.4.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf nginx-1.4.4.tar.gz
tar -zxvf pcre-8.34.tar.gz
tar -zxvf zlib-1.2.8.tar.gz

在我们继续构建Nginx之前,我们必须首先获得一个名为“Make”的程序和一个C ++源代码'g++'的编译器,它将负责执行在我们的实例上构建Nginx所需的所有命令。你可以通过apt-get获得它:

sudo apt-get install make g++

在这个阶段,我们现在可以继续将Nginx/First更改目录构建到刚刚创建的提取的Nginx文件夹中:

cd nginx-1.4.4

接下来,我们必须为特定实例配置构建选项:

./configure --with-pcre=../pcre-8.34 --with-zlib=../zlib-1.2.8

然后我们可以继续创建Nginx二进制文件:

make

最后,我们可以将Nginx安装到我们的系统中:

sudo make install

第二步 - 安装Varnish

Varnish将负责向访客提供我们的网站。

您只能在varnish实例上安装它。

首先,我们需要获取GPG Key varnish,以便我们访问其存储库。我们可以通过运行以下命令下载它:

wget http://repo.varnish-cache.org/debian/GPG-key.txt

然后安装密钥:

sudo apt-key add GPG-key.txt

然后,我们需要将Varnish存储库列表添加到我们的实例源列表中:

echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list

然后确保apt-get知道Varnish软件包:

sudo apt-get update

最后,安装Varnish:

sudo apt-get install varnish

在这个阶段,我们准备配置Nginx和Varnish为外部世界提供服务!

第三步 - 配置Nginx

我们不需要过多地修改Nginx的配置,它的默认值对于本教程来说没问题。但是我建议我们修改“欢迎使用nginx”页面。

导航到Nginx欢迎页面所在的根html目录:

cd /usr/local/nginx/html/

现在编辑index.html:

vim index.html

修改文件以匹配以下内容:

nginx01

<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>I am nginx01</p>

nginx02

<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>I am nginx02</p>

现在我们可以启动Nginx(注意:如果此命令不产生输出,则表示已成功执行):

sudo /usr/local/nginx/sbin/nginx

第四步 - 配置Varnish

首先,您必须设置Varnish才能在端口80上运行。为此,您必须修改默认的Varnish配置文件。首先将目录更改为此文件所在的位置:

cd /etc/default

然后我们必须打开varnish文件:

sudo vim varnish

在文件中找到以下块:

## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a 1GB
# fixed-size cache file.
#
    DAEMON_OPTS="-a :6081 \
         -T localhost:6082 \
         -f /etc/varnish/default.vcl \
         -S /etc/varnish/secret \
         -s malloc,256m"

修改它以匹配以下内容:

## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a 1GB
# fixed-size cache file.
#
    DAEMON_OPTS="-a :80 \
         -T localhost:6082 \
         -f /etc/varnish/default.vcl \
         -S /etc/varnish/secret \
         -s malloc,256m"

接下来我们需要配置负载均衡器。将目录更改为我们的Varnish配置脚本所在的位置:

cd /etc/varnish

然后打开default.vcl文件:

sudo vim default.vcl

您必须删除backend default此文件中的块,如下所示:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

用以下内容替换它。确保您将nginx01和nginx02的.host分别更改为您的公共(如果您的实例具有此功能,则为私有)IP:

# define our first nginx server
backend nginx01 {
    .host = "192.168.0.100";
    .port = "80";
}

# define our second nginx server
backend nginx02 {
    .host = "192.168.0.101";
    .port = "80";
}

# configure the load balancer
director nginx round-robin {
    { .backend = nginx01; }
    { .backend = nginx02; }
}

# When a request is made set the backend to the round-robin director named nginx
sub vcl_recv {
    set req.backend = nginx;
}

第五步 - 测试可用性

让我们检查一下我们是否可以通过我们的Varnish服务器访问我们的网站。找到您启动的varnish实例的公共IP,并通过Web浏览器浏览它。如果您看到以下文字,则一切正常!

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
I am nginx01
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.

您可以通过在Nginx报告服务的服务器上关闭Nginx来测试该站点是否保持在线状态。在我的情况下是nginx01,要关闭nginx,您可以执行以下操作:

/usr/local/nginx/sbin -s stop

再次尝试您的Varnish公共IP。您可能仍会看到刚关闭的服务器报告为活动服务器;这是因为Varnish持有缓存。一旦此缓存过期,您将看到nginx02正在提供内容。

要强制Varnish清除其缓存,请重新启动该服务:

sudo service varnish restart

结论

在此阶段,您已拥有一个完全配置的Varnish负载平衡循环集群。您可能还对varnish原理感兴趣,如果您需要搭建Nginx和Tomcat的web集群环境可以参考腾讯云社区的相关教程。


参考文献:《How To Configure a Clustered Web Server with Varnish and Nginx on Ubuntu 13.10》

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
    • 关于集群Web服务器
      • 关于Varnish
        • 关于Nginx
          • 设置
      • 第一步 - 安装Nginx
      • 第二步 - 安装Varnish
      • 第三步 - 配置Nginx
      • 第四步 - 配置Varnish
      • 第五步 - 测试可用性
      相关产品与服务
      负载均衡
      负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档