Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Yum install varnish

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

All three virtual machines need to do systemctl stop firewalld / / turn off firewall setenforce 0 / / turn off monitoring 80.101yum install-y httpdvi / etc/httpd/conf/httpd.conf find ServerName www.example.com:80 # remove vi / var/www/html/index.htmlserver 1systemctl start httpd80.102yum install-y httpdvi / etc/httpd/conf/httpd.conf find ServerName www.example.com:80 # remove vi / var/www/html/index.htmlserver 2systemctl start httpd1. Install varnish (since Centos7, varnish has been included in the epel repository) cd / etc/yum.repos.d/mv back/*. / yum install epel-release-y / / requires networking yum-y install varnish

two。 Create a new varnish user useradd-M-s / sbin/nologin varnish

3.varnish profile

/ etc/varnish/varnish.params main configuration file

/ etc/varnish/default.vcl VCL configuration file

VCL

Varnish Configuration Language (VCL) is a dynamic language and a varnish configuration language, which is used to describe request processing and cache policies. The vcl configuration content is converted into C language code by the VCC sub-process created by manager process, then compiled into shared objects by gcc, and finally loaded into cacher process.

The VCL file is divided into several subroutines, and different subroutines are executed at different times, such as one subroutine is executed on request, and the other subroutine is executed when receiving the file transmitted by the back-end server.

VCL processing flow chart

The processing process is roughly divided into the following steps

1. Receive status: the entry status of request processing. Judge whether the request should be Pass or Pipe or enter Lookup (local query) according to VCL rules.

2. Lookup status. Find the object requested by the user in the cache. If there is no object in the cache, the subsequent operation is likely to cache the requested object. After entering this state, it will look up the data in the hash table. If found, it will enter the Hit state, otherwise it will enter the miss state.

3. Pass status. In this state, the backend (source server) request will be entered, that is, the fetch status will be entered without caching.

4. Fetch status. In Fetch state, the backend acquires the request, sends the request, obtains the data of the source server, and stores it locally.

5. Deliver provides the status, sends the obtained data to the client, and then completes this request.

Note:

Pass: bypass the cache, neither querying the content from the cache nor storing the content in the cache

Pipe: instead of testing or doing anything to the client, a dedicated "pipeline" is established between the client and the back-end server, and the data is transferred directly between the client and the back-end server. In this case, the subsequent data transferred in the keep-alive connection will be directly transmitted through this pipeline and will not appear in any log.

Grammar

(1) support comments / / # / /

(2) Loop is not supported

(3) sub $name: used to define subroutines

Sub vcl_recv {

}

(4) there are many built-in variables, and the callable position of variables is closely related to state engine.

(5) the termination statement, return (action), is supported. No value is returned.

(6) dedicated to "domain"

(7) operator =,! , &, | |

Common sentences

If else

Set name=value

Unset name

Req.http.HEADER: invokes the specified variable of http protocol in the request message

Req.request: request method

Varnish variable type

Req-- request

Resp-- response

Client-- client

Server-- server

Req generated when bereq-- requests to the backend

Resp generated when the beresp-- backend responds

Obj-- project object

Storage-- Siz

Common variables:

Bereq and req:

Bereq (req) .http.HEADERS: the specified header of a request message sent by varnish to backend server

Bereq (req) .request: request method

Bereq (req) .url: request path

Bereq (req) .proto: request protocol

Bereq (req) .backend: indicates the backend host to be called

Beresp and resp

Beresp.proto: the protocol used in the response

Beresp.status: status code of the response

Beresp.reason: reason phrase

Beresp.backend.ip: the backend ip address of the response

Beresp.backend.name: response backend domain name

Beresp.http.HEADER: the header of a message that responds from backend server

Beresp.ttl: the remaining lifetime of the content responded by the back-end server

Obj

Obj.ttl: the ttl value of the object

Obj.hits: the number of hits this object has made from the cache

Server

Server.ip

Server.hostname

CDN

Practice: using varnish to accelerate the web server varnish:192.168.80.100web1:192.168.80.101--www.aa.comweb2:192.168.80.102--www.bb.com of several different domain name sites

Vi / etc/varnish/default.vclbackend web1 {.host = "192.168.80.101"; .port = "80";} backend web2 {.host = "192.168.80.102"; .port = "80";} sub vcl_recv {if (req.http.host ~ "(?) ^ (www.)? aa.com$") {set req.http.host = "www.aa.com"; set req.backend_hint = web1 } elsif (req.http.host ~ "(? I) ^ www.bb.com$") {set req.backend_hint = web2;return (hash);}} judge that when accessing the www.aa.com domain name, the data is fetched from web1, and when accessing the www.bb.com domain name, the data is fetched from web2.

# add a Header flag to determine whether the cache hits sub vcl_deliver {if (obj.hits > 0) {set resp.http.X-Cache = "HIT FROM" + req.http.host;set resp.http.X-Cache-Hits = obj.hits;} else {set resp.http.X-Cache = "MISS FROM" + req.http.host;} return (deliver);}

Systemctl restart varnish / / restart varnishvi / etc/hosts192.168.80.100 www.aa.com192.168.80.100 www.bb.com

Yum install-y elinkselinks www.aa.com-- dump # elinks text interface browser

Note if there is a problem, change it here.

Vi varnish.params

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report