清漆 4 基本身份验证 [英] Varnish 4 Basic authentication

查看:34
本文介绍了清漆 4 基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须缓存多个后端服务器,我从 Nginx 切换到 Varnish,最后发现 2 个服务器需要运行 HTTP 基本身份验证.我试试这个链接 http://blog.tenya.me/blog/2011/12/14/varnish-http-authentication 对我不起作用(他们运行了 Varnish 3)有没有一种简单的方法可以在 Varnish 4 中配置基本身份验证?

I have to cache multiple backend servers, I switch from Nginx to Varnish and finally discover 2 server need to run HTTP Basic Authentication. I try this link http://blog.tenya.me/blog/2011/12/14/varnish-http-authentication and it not work for me (they ran Varnish 3) Is there an easy way to configure Basic Authentication in Varnish 4?

推荐答案

您可以使用 VMOD basicauth

You can use the VMOD basicauth

首先你需要安装它.从 basicauth 的 Git 存储库 下载源代码.提取到您的 homedir,例如~/vmod-basicauth/

First you need to install it. Download the source from the Git repo for basicauth. Extract into your homedir e.g. ~/vmod-basicauth/

您还需要 Varnish 源代码来构建 VMOD.

You'll also need the Varnish source to build the VMOD.

在 Debian/Ubuntu 中类型

In Debian/Ubuntu type

apt-get source varnish

这会将源代码复制到您的密码中.

This will copy the source to your pwd.

然后执行此操作以安装它.请注意,您需要根据您的设置和清漆版本更改路径

Then do this to install it. Note that you need to change the paths according to your setup and version of varnish

cd ~/vmod-basicauth
./configure VARNISHSRC=$HOME/varnish-4.0.2
make 
sudo make install
sudo make check

更新似乎源代码已从 Ubuntu 和 Debian 软件包存储库中删除(很可能是意外).

Update It seems like the source have been removed from the Ubuntu and Debian package repos (most likely by accident).

直接从 Git (v4.0.2)

制作清漆

您必须制作"下载的源

cd ~
wget https://github.com/varnish/Varnish-Cache/archive/varnish-4.0.2.zip
unzip varnish-4.0.2.zip
cd Varnish-Cache-varnish-4.0.2
sudo ./autogen.sh
sudo ./configure --prefix=/usr
sudo make

请注意,您不必安装源代码,因此不要进行安装",因为这可能会破坏您当前的安装.

Note that you don't have to install the source, so don't "make-install" because that might mess up your current installation.

构建&安装 VMOD

cd ~
./configure VARNISHSRC=$HOME/Varnish-Cache-varnish-4.0.2
make 
sudo make install
sudo make check

如果无法自动检测到,您可能还必须指定 VMOD 安装目录.如果 ./configure 失败试试这个

It might be that you also have to specify your VMOD install directory if it can't be autodetected. If ./configure fails try this

./configure VARNISHSRC=$HOME/Varnish-Cache-varnish-4.0.2 VMODDIR=/usr/lib/varnish/vmods/

一些构建依赖

我经常需要很多不同的构建依赖项,所以我经常在设置新的 Varnish 服务器时安装这些依赖项.

I often require alot of different build dependencies so I often install these when I setup a new Varnish server.

sudo apt-get install git-core zlib1g-dev automake build-essential libtool libssl-dev libreadline-dev libyaml-dev libsqlite3-dev ncurses-dev sqlite3 libxml2-dev libxslt1-dev libpcre3-dev libcurl4-openssl-dev python-docutils python-software-properties libvarnishapi-dev

配置 Varnish 以使用 VMOD

它使用 .htpasswd 文件进行身份验证,而不是将密码直接存储在 VCL 中.

Configure Varnish to use the VMOD

It uses a .htpasswd file for authentication instead of storing the password directly in the VCL.

确保将/var/www/.htpasswd"更改为您的 htpasswd 文件的路径.

Make sure to change "/var/www/.htpasswd" to the path of your htpasswd file.

#default.vcl
import basicauth;

sub vcl_recv {
    if (!basicauth.match("/var/www/.htpasswd",  req.http.Authorization)) {
        return(synth(401, "Authentication required"));
    }
}

#Prompt the user for a password
sub vcl_synth {
    if (resp.status == 401) {
        set resp.http.WWW-Authenticate = "Basic";
    }
}

这篇关于清漆 4 基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆