用varnish 前端做保護 -新增前端使用者名稱密碼驗證

在日常工作中。用nginx 做驗證很容易。可是有一個客服需要,要用varnish來做這個功能。

折騰了一上午,終於找到了此方法。廢話不多說,直接上乾貨。

適用於varnish

4。1

sub vcl_recv {if ( req。http。Authorization !~ “Basic Zm9vOmJhcg==”){ return (synth(401, “Not allowed ”));}}sub vcl_synth{if (resp。status == 401) { set resp。http。Content-Type = “text/html; charset=utf-8”; set resp。http。WWW-Authenticate = “Basic realm=Secured”; synthetic( user or password error );}}使用者密碼格式:user:passwdfoo:bar生產密碼echo -n “foo:bar” | base64此功能和nginx auth_basic 一樣auth_basic “nginx basic http test for g2345。com”;auth_basic_user_file conf/htpasswd;