Varnish is a high-performance, open-source HTTP accelerator

Source: Internet
Author: User
Tags varnish haproxy

    • 如何衡量缓存系统的优劣性
1: Cache Hit ratio:In the memcached server, the value of Get_hits represents the number of cache hits, the value of get_misses indicates the number of missed hits, then the formula for hit rate is: Hit ratio =get_hits/(get_hits+get_ Misses) When an end user accesses an accelerator node, it is called a hit if the node has cached the data to be accessed, and if not, it needs to be taken to the original server, which is no hit. The process of fetching data is synchronized with user access, so even if the new data is re-fetch, the user will not feel the delay. Hit Ratio = number of hits/(number of hits + no hits), cache hit ratio is one of the important factors in judging the acceleration effect.2: There are two indicators of hit ratioDocument hit rate: Measure the byte hit ratio from the number of hit documents: measured from the size of the hit byte
    • 应该让哪些资源被缓存
1: Public resource cache in static resources2: Private resources in static resources are not cached3: Public resource cache in dynamic resources4: Private resources in dynamic resources are not cached
    • 缓存的超时时长Reference http://www.51testing.com/html/28/116228-238337.html
http/1.0 Response Headerexpireshttp/1.1 Response HeaderCache-control:max-age=
    •  

Threevarnish进

    • 安装
RPM Package Yum install-y varnish compile and install wget http://varnish-cache.org/_downloads/varnish-4.0. 5.tgz
    • 官方文档
http://varnish-cache. org
    • varnish存储类型
1) malloc acquires memory and allocates memory through malloc. 2) Mmap file creates large files, which are mapped into chunks within 1G by means of dichotomy segmentation.
    • varnish配置文件讲解
Default. VCL: Master configuration fileVarnish. Params: Server parameter configuration file

Fourvarnish配置文件详解

    • 配置varnish进程参数的配置文件/etc/varnish/varnish.params,在启动varnish的时候,就会调用这个文件,可以在/usr/lib/systemd/system/varnish.service文件中看到,启动是确实调用了这个参数文件
# Specify reload varnish, re-reload the VCL configuration filereload_vcl=1 # Specify profile for VCLVARNISH_VCL_CONF=/ETC/VARNISH/DEFAULT.VCL# Specify Varnish as the address and port to be monitored at the time of the reverse generation# in an enterprise-class scenario, varnish is typically an anti-load balancer for Nginx or haproxy that can implement port mapping, not necessarily port 80# The address of the listener is 192.168.1.5, which is commented by default, which indicates that all IP addresses on this computer are listening# varnish_listen_address=192.168.1.5varnish_listen_port=7000 # Specify VARNISHADM's Shell login interfacevarnish_admin_listen_address=127.0. 0.1 varnish_admin_listen_port=6082 # Specify the key file used by the Shell interfaceVarnish_secret_file=/etc/varnish/secret# Specify the type of varnish storage, and the size, where the default memory cache is used, the size is 256M, or you can use the file's storage# varnish_storage= "file,/var/lib/varnish/varnish_storage.bin,1g"varnish_storage="malloc,256m" # Specify the owner and group of the varnish processVarnish_user=varnishVarnish_group=varnish# Specify runtime parameters, use the-P option to specify, thread_pools=6: means enable 6 thread pools, thread_pool_min=5: Represents a minimum of 5 threads per thread pool, thread_pool_max= 500: Indicates a maximum of 500 threads per thread pool, thread_pool_timeout=300: Indicates the thread's request timeout timedaemon_opts="-P thread_pools=6-p thread_pool_min=5-p thread_pool_max=500-p thread_pool_timeout=300"
    • 请求过程和响应过程分析
Request processing process (1) Requested for can cache object 1: Cache Hit: Response client 2: Cache miss: Varnish plays the role of the client, sends the request message, the back-end server requests the resource object, the object can be cached before it responds to the client, the object can not be cached, Do not cache the direct response to the client (2) requested by the 1:varnish to not cache the object as a client, send a request message, and request a resource object from the back-end server

    • 在编写vcl配置文件中,常用的对象分类
1:req.: Related to HTTP requests sent by the clientReq. http.: Get each header value of the request messageExample: Req. http. Cookie gets the value of the cookie header in the request message 2:RESP.: HTTP response message from varnish to clientResp. http.: Each header of a response messageExample: Set RESP. http. X-cache = "hit from" + Server. Hostname to X-cache header assignment 3:bereq.: HTTP request sent by Varnish to the backend host (Backend request: Requesting message from the backend host)Bereq. http.: The object of the request message sent to the backend host4:BERESP.: HTTP response message sent by backend hostBeresp. http.: The object of the HTTP response message sent by the backend host5:obj.: Cached object properties stored in cache space
    • 在编写vcl配置文件中,常用的对象,已经对象的意义说明
1:reqreq. Method: Methods of the request req. URL: Requested URL 2:Bereqbereq. http. HEADERS:header of Varnish request message bereq. Request: Requesting method bereq. URL: Requested URL bereq. Proto: Protocol version bereq. Backend: Indicates the back-end host to invoke 3:Berespberesp. Proto: Backend host responds to varnish protocol version beresp. Status: Response state code beresp. Backend. Name: Host name of the backend host beresp. http. HEADERS: Header of back-end host response message beresp. TTL: Back-end host response content TTL value 4:objobj. Hits: The number of times an object has been hit in the cache obj. TTL: The TTL value of an object 5:Server:server. IP: IP for host server. Hostname: Host name Vcl4.0;# Import Scheduler ModuleImport directors;############################# (a) ################################# specifies the host backend Web1 {. Hosts to be dispatched to the backend."192.168.23.21";. Port ="8000";. Probe = {# Specifies the URL to do a health check, since given this URL, it is necessary to let the backend host site directory has this URL, if there is no varnish will not dispatch the request to this host. url ="/health.html";# Specifies the timeout length for the check. Timeout =2s;# Check the time interval for each check. Interval =4s;# Specifies how many times a test is detected. window =5;# Specifies the minimum number of successful times that the backend host is considered valid. Threshold =1;}} backend Web2 {. host ="192.168.23.22";. Port ="8000";. Probe = {# Specifies url.url to do health check ="/health.html";# Specifies the timeout length for the check. Timeout =2s;# Check the time interval for each check. Interval =4s;# Specifies how many times a test is detected. window =5;# Specifies the minimum number of successful times that the backend host is considered valid. Threshold =1;}} ############################# (ii) ################################ # defines the Load Balancer Scheduler sub Vcl_init {# Creates a Scheduler object, Directors.round_robin () Specifies the polling schedule algorithm new static = Directors.round_robin (); Static.add_backend (WEB1); Static.add_backend (   WEB2);} ############################# (iii) ################################# the first sub-process, when received request processing sub-process sub Vcl_recv {#  Specifies that the varnish receives the request, if the cache does not hit, all directly to the back end of the static host group set req.backend_hint = Static.backend (); # How to request a method is a PRI and return directly to the synth sub-processif (Req.method = = "PRI") { / * We don't support SPDY or http/2.0 * /Return (Synth (405)); # if it's not one of the seven methods, go straight back to the pipe processif (req.method! = "GET" && Req.method! ="HEAD" &&Req.method! ="PUT" &&Req.method! ="POST" &&Req.method! ="TRACE" &&Req.method! ="OPTIONS" &&Req.method! ="DELETE") { / * non-rfc2616 or CONNECT which is weird. * /return (pipe);} # If it's not a get or head method, we don't have to go to the cache, which is handled directly by the backend host.if (req.method! = "GET" && req.method! = "HEAD") { / * We only deal with GET and HEAD by default * /return (pass);} # if the HTTP header in the client's request message contains authentication information or cookie information, it is also processed directly by the backend host, which is a confidential static content # because in Haproxy the cookie information is set in the user's browser for the dynamic request, the following is commented out first. To prevent requests from querying the cache #if (req.http.Authorization | | req.http.Cookie) {#/ * not cacheable by default * /# return (pass); #} # Otherwise the request is processed by the hash process return (hash);} ############################# (Thu) ################################ # response to back-end host response reported to be modified sub Vcl_backend_response {# How to send a back-end host Request message HTTP header without the S-maxage parameter, then do twoIf Judgment#1: If the request is a CSS style code, set the response back to the resource object with a TTL value of 7200s, and delete the Set-cookie parameter #2: If the request is a JPG picture, set the response back to the resource object with a TTL value of 7200s, and delete the Set-cookie parameter # All the other requests are being processed by the deliver sub-processif (Beresp.http.cache-control!~ "S-maxage") { # here to write as Bereq.url, if written beresp.url will errorif (bereq.url ~ "(? i) \.css$") { Set Beresp.ttl =7200s;Unset Beresp.http.set-cookie; }if (bereq.url ~ "(? i) \.js$") { Set Beresp.ttl =7200s;Unset Beresp.http.set-cookie; }if (bereq.url ~ "(? i) \.jpg$") { Set Beresp.ttl =7200s;Unset Beresp.http.set-cookie; }} return (deliver);} ############################# (Fri) ################################ # This piece of history is the last one # in the deliver sub-process can modify the Response message HTTP header Sub Vcl_deliver {# If the client's request is cached, add an HTTP header parameter that explicitly tells the user that the requested resource is loaded from the cacheif (obj.hits>0) {Set Resp.http.x-cache ="hits from" + Server.hostname;}else{Set Resp.http.x-cache ="MISS from" + server.hostname;} }

(v) varnish工具介绍 (for the cache server, modify the configuration, must not restart, restart will clear all the memory)

    • varnishadm
# Get HelpVarnishadm-h# Login to varnishadm command-line interfaceVarnishadm-s/etc/varnish/secret-t127.0. 0. 1:6082 # get command HelpHelp# mount the specified configuration fileVcl.load <configname> <filename> For example: Vcl.load config1DEFAULT.VCL# using the specified configuration fileVcl.use <configname> For example: Vcl.use config1# Delete configuration fileVcl.discard <configname> Example: Vcl.discard boot# List all available profilesVcl.list: for example: Vcl.list# Display the contents of the configuration fileVcl.show [-v] <configname> For example: Vcl.show config1# List hosts available on back endBackend.list
    • varnishstat
View status information for a varnish server

Varnish is a high-performance, open-source HTTP accelerator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.