Varnish Learning application installation and configuration monitoring

Source: Internet
Author: User
Tags varnish

Varnish, as a reverse proxy software, is applied to the Web Front-end. Its good cache performance greatly improves the website carrying capacity.
The following is a record of recent practical experiences in varnish learning and application. This article focuses on installing configuration monitoring.

1. Install Varnish
There are two methods:
1. Download the installation file and compile and install it locally.
2. Connect to the official website for automatic installation (relatively simple)
Https://www.varnish-cache.org/installation/redhat

Ii. Configuration File
1. Configure varnish startup and running parameters. For example, specify the corresponding execution script, that is, the VCL file.
Varnish Management port. The default value is 6082. You can change it to a custom port, for example, 2000.
Varnish_admin_listen_port = 2000
Listening port. The default value is 8080. Generally, it is changed to 80.
Varnish_listen_port = 80
Cache file storage path
Varnish_storage_file =/var/lib/Varnish/varnish_storage.bin
The cached file varnish_storage.bin can be up to 2 GB in a 32-bit operating system,
If you need a larger cache file, you need to install 64 as the Linux operating system.


2. Configure varnish to run as a service
Version 3.0 or later. After varnish is installed, it is automatically configured, you can directly use the service varnish start command to execute
versions earlier than 3.0. You need to manually configure the corresponding configuration file
1) Find varnish under the corresponding version of the installation package. initr file, such as
C :/... /Varnish-3.0.1/RedHat/Varnish-3.0.1/RedHat/varnish. initrc
2) Change varnish. in initrc, the main execution parameter is
3) if the parameter is in the default configuration form, such as ETC/sysconfig/varnish, You can omit step 2nd.
4) varnish. initrc is renamed varnish
5) Copy varnish to/etc/rc. d/init. d/
6) grant varnish the execution permission chmod 700/etc/rc. d/init. d/varnish
7) chkconfig -- add varnish
3. view the varnish running status
PS-Aux | grep varnishd
the normal result is as follows


You can also view it through top | grep varnishd.
4. View varnish logs
1. Use the varnishlog command to view logs. You can add some parameters. For details, use-h to obtain parameter descriptions.
For example, run the varnishlog-I txurl command to check which URLs cause back-to-Source
2. Save the log to a file
Varnishncsa command is required, as shown in figure
Varnishncsa-W/var/lib/Varnish/varnish. Log save varnishlog in a file
The file format is text format, which is easy to view
V. View varnish cache Conditions
1. Use varnishstat to view current and cache hits since startup

Meaning of result data:

The first line shows how long varnish has been running since it was started, for example, 45 days, 0 hours, 11 minutes, and 27 seconds.
The second line shows the time when the command was started. The three numbers will eventually change to 10,100,100 0, indicating 10 seconds, 100 seconds, and 1000 seconds respectively.
The third line shows the hit rate, which is the time above the object, which is the hit rate within 10 seconds, the hit rate within 100 seconds, and the hit rate within 1000 seconds.
The data starting from the fourth row is divided into four columns.
The first column is the total value, the second column is the value per second, the third column is the average value since the start of the command (varnishstat), and the fourth column is the description

Several of them are important:

Client Connections accepted: indicates the total number of HTTP requests successfully sent from the client to the reverse proxy server.
Client requests received ed: indicates the cumulative number of HTTP requests sent by the browser to the reverse proxy server. Because persistent connections may be used, this value is generally greater than client connections accepted.
Cache-hit: indicates the number of cache hits
Miss-hit: indicates the number of missed attempts.
Worker threads: Number of current worker threads
Expired objects: indicates the number of expired objects.
LRU nuked objects: indicates the number of objects that can be used by the cache to go online and have to be removed.
LRU moved objects: number of objects moved by the LRU Policy
Total header Bytes: indicates the size of the cached request header object.
Total body Bytes: indicates the size of the cached Request body object.

Hit rate formula: cache-Hit/client requests received ed

2. If you need to log on to the server every time you view the current cache status of varnish, it is a little troublesome.
You can use PHP (other languages) to write a paragraphProgram, Remote view
CodeYou can refer to the following. For versions earlier than 3.0, you can connect to the varnish Management port through socket. You can use the STAT command to check whether the STAT command is available in Versions later than 3.0. You can only use the following method to solve this problem:

View code

<? PHP  $ OUTFILE = Shell_exec ("/Usr/bin/varnishstat-X" );  $ XML = Simplexml_load_string ( $ OUTFILE );  Echo   $ XML -> Getname (). "<br/>" ;  Foreach ( $ XML -> Children () As   $ Child  ){  //  $ Tmpname = "";        Foreach ( $ Child -> Children () As   $ Subchild ){  If ( $ Subchild -> Getname () = "name" ){  $ Tmpname = $ Subchild  ;}  Else    If ( $ Subchild -> Getname () = "value" ){  If ( $ Tmpname ! = ""){  $ Arys [" $ Tmpname "] = $ Subchild  ;  $ Tmpname = "" ;}}  Else  {  Continue  ;}}}  Function Bytereduce ( $ Bytes  ){ If ( $ Bytes > 1099511627776 ){  Return   Round ( $ Bytes /1099511627776). "TB" ;}  Else   If ( $ Bytes > 1073741824 ){  Return   Round ( $ Bytes /1073741824). "GB" ;}  Else   If ( $ Bytes > 1048576 ){  Return   Round ( $ Bytes /1048576). "MB" ;}  Else   If ( $ Bytes > 1024 ){  Return  Round ( $ Bytes /1024). "kb" ;}  Else  {  Return   $ Bytes . "B" ;}}  Echo "Client_conn :". $ Arys ["Client_conn"]. "<br/>" ;  Echo "Client_req :". $ Arys ["Client_req"]. "<br/>";  Echo "Cache_hit :". $ Arys ["Cache_hit"]. "<br/>" ;  Echo "Cache_miss :". $ Arys ["Cache_miss"]. "<br/>" ;  Echo "Cache hit rate :". Round (( $ Arys ["Cache_hit"]/ $ Arys ["Client_req"]) * 100). "% <br/>" ;  Echo "LRU nuked objects :". $ Arys [N_lru_nuked]. "<br/>" ;  Echo "". Bytereduce ( $ Arys ["S_bodybytes"] + $ Arys ["S_hdrbytes"]). "ACC content (". bytereduce ( $ Arys ["S_hdrbytes"]). "Header". bytereduce ( $ Arys ["S_bodybytes"]). "Body )" ; ?>

Page effects are shown in figure

Of course, in order to view the real-time situation, you can add an HTML on this monitoring page to regularly refresh

If you have deployed professional monitoring tools such as catic, you can view the varnish running status in the catic through configuration.

For Versions later than 3.0, you need a PHP page output result echo shell_exec ("/usr/bin/varnishstat-X"); then configure it in catic.

Note: This is my first blog :)

 

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.