Schema varnish+nginx+php (FastCGI) +mysql5+mencache+mencachedb (iii)

Source: Internet
Author: User
Tags chmod varnish

Iv. optimization of Linux kernel parameters


Add the following at the end:

Net.ipv4.tcp_fin_timeout = 30
Net.ipv4.tcp_keepalive_time = 300
Net.ipv4.tcp_syncookies = 1
Net.ipv4.tcp_tw_reuse = 1
Net.ipv4.tcp_tw_recycle = 1
Net.ipv4.ip_local_port_range = 5000 65000

Make the configuration effective immediately:


--------------------------------------------------------------------------------------------------------------- --------

Attached: Recently often engaged in lighttpd+fastcgi+php or nginx+fastcgi+php, often by PHP "No input file specified." Let's get depressed, tell me about the situation I met.

First php.ini in the configuration

Doc_root once I set a path, the result of PHP old hint "No input file specified.", only one virtual machine works. It's normal when you get rid of it.

The configuration in Nginx is a bit troublesome

Fastcgi_index index.php;
Fastcgi_param Script_filename/var/www/html$fastcgi_script_name;

Each virtual machine according to their own virtual machines can not set the directory, to ensure that the path is correct.
Fastcgi_param Script_filename/var/www/html$fastcgi_script_name; not in front of Fastcgi_pass;

Remember to change the php.ini to restart the fastcgi service.

In fact, because of carelessness caused, it is very simple, write to give yourself to mention a wake up

--------------------------------------------------------------------------------------------------------------- ------------

Four.) Installing varnish

This article on varnish, written today, is a detailed solution that can completely replace squid as a website cache accelerator. There is little information about varnish on the internet, the Chinese information is very small, I hope this article can attract more people to study and use varnish.

In my opinion, there are three reasons for using varnish instead of squid:
1, Varnish uses the "Visual page Cache" technology, in memory utilization, varnish than squid has advantages, it avoids squid frequently in memory, disk Exchange files, performance than squid high.
2, varnish stability is also good, by the way, varnish performance of the key lies in Varnish configuration document optimization.
3, through the varnish management port, you can use regular expressions to quickly and batch clear part of the cache, which squid can not have
4. There is also a point, it should be varnish shortcomings, that is, the varnish cache is basically in memory, if the varnish process stops and starts again, Varnish will revisit the backend Web server, Cache again. Although squid performance is not varnish high, but it stops, restart, you can directly read the cache data from the disk.

Varnish is a high-performance open-source HTTP accelerator, and Norway's largest online newspaper, Verdens Gang (, uses 3 varnish instead of the original 12 squid for better performance than ever before.

Varnish's author, Poul-henning Kamp, is one of the core developers of FreeBSD, who believes that computers today are much more complex than the 1975. In the 1975, there were only two storage media: Memory and hard disk. But now the computer system's memory in addition to main storage, but also includes the CPU L1, L2, and even L3 cache. The hard drive also has its own quick-access device, so squid cache self-processing object replacement architecture is not possible to know these conditions to be optimized, but the operating system can be aware of these conditions, so this part of the work should be given to the operating system, this is the Varnish cache design architecture.

--------------------------------------------------------------------------------------------------------------- ---

1. Compile and install varnish:

Download Source Package Link: wget

Attached 1:varnish official website:

[[email protected]] #wget
[[email protected]] #tar ZXVF varnish-1.1.2.tar.gz
[[email protected]] #cd varnish-1.1.2
[Email protected]]#./configure--prefix=/usr/local/varnish--enable-debugging-symbols--enable-developer-warnings --enable-dependency-tracking
[[email protected]] #make && make install

2. Create WWW users and groups, and varnish cache file storage directory (/var/vcache):

[Email Protected]]#/usr/sbin/groupadd www
[Email protected]]#/usr/sbin/useradd-g www www
[[email protected]] #mkdir-P/usr/local/varnish/var/varnish/
[[email protected]] #chmod +w/usr/local/varnish/var/varnish/
[[email protected]] #chown-R www:www/usr/local/varnish/var/varnish/

3. Create the Varnish log directory (/usr/local/varnish/logs):

[[email protected]] #mkdir-P/usr/local/varnish/logs
[[email protected]] #chmod +w/usr/local/varnish/logs
[[email protected]] #chown-R www:www/usr/local/varnish/logs
[[email protected]] #touch/usr/local/varnish/logs/varnish.log

4. Create the varnish configuration file:

[[email protected]] #vi/usr/local/varnish/vcl.conf

Backend Myblogserver {
Set = "";
Set backend.port = "80";

ACL Purge {

Sub Vcl_recv {
if (req.request = = "PURGE") {
if (!client.ip ~ purge) {
Error 405 "not allowed.";

if ( ~ "^") {
Set req.backend = Myblogserver;
if (req.request! = "GET" && req.request! = "HEAD") {
else {
else {
Error 404 "Zhang Quan Sheng Cache Server";

Sub Vcl_hit {
if (req.request = = "PURGE") {
set obj.ttl = 0s;
Error "purged.";

Sub Vcl_miss {
if (req.request = = "PURGE") {
Error 404 "not in cache.";

Sub Vcl_fetch {
if (req.request = = "GET" && req.url ~ "\. ( TXT|JS) ($ ") {
Set obj.ttl = 3600s;
else {
Set obj.ttl = 30d;

Here, I explain to this configuration file:
(1), varnish through the reverse proxy request back-end IP is, the port is 80 Web server;
(2), varnish allow localhost,, 192.168.0.*** three source IP through the Purge method to clear the cache;
(3), varnish the domain name for the request processing, domain name request returns "Zhang Quan Sheng Cache Server";
(4), varnish to the HTTP protocol request cache, the POST request through, let it directly access the back-end Web server. This configuration, because the post request is generally sent to the server, the server needs to receive, processing, so do not cache;
(5), varnish the URL cache time at the end of. txt and. JS is set for 1 hours, and the other URL cache time is set to 30 days.

--------------------------------------------------------------------------------------------------------------- ----------

5. Start varnish

Ulimit-shn 51200l
/usr/local/varnish/sbin/varnishd-n/var/vcache-f/usr/local/varnish/vcl.conf-a file,/usr/local/ Varnish/var/varnish/,1g-g www-u www-w 30000,51200,10-t client_http11=on

6. Start Varnishncsa to write the varnish access log to the log file:

/usr/local/varnish/bin/varnishncsa-n/usr/local/varnish/var/varnish/-w/usr/local/varnish/var/varnish/ Varnish.log &

7. Configure boot auto-start varnish


Ulimit-shn 51200l
/usr/local/varnish/sbin/varnishd-n/var/vcache-f/usr/local/varnish/vcl.conf-a file,/usr/local/ Varnish/var/varnish/,1g-g www-u www-w 30000,51200,10-t client_http11=on
/usr/local/varnish/bin/varnishncsa-n/usr/local/varnish/var/varnish/-w/usr/local/varnish/var/varnish/ Varnish.log &

8. Optimizing Linux Kernel Parameters

Net.ipv4.tcp_fin_timeout = 30
Net.ipv4.tcp_keepalive_time = 300
Net.ipv4.tcp_syncookies = 1
Net.ipv4.tcp_tw_reuse = 1
Net.ipv4.tcp_tw_recycle = 1
Net.ipv4.ip_local_port_range = 5000 65000

--------------------------------------------------------------------------------------------------------------- ------------

Then look at how to manage varnish:
1. View the number of varnish server connections and hit ratio:


2. Manage through the varnish management port:
Use Help to see which varnish commands you can use:

/usr/local/varnish/bin/varnishadm-t Help

Available commands:
ping [timestamp]
Vcl.list [-l] []
Help [command]

3. Use regular expressions to clear the cache in batches using the varnish management port:
(1), example: Clear the URL address similar to

/usr/local/varnish/bin/varnishadm-t url.purge/a/

(2), example: Clear the URL address similar to Http://

/usr/local/varnish/bin/varnishadm-t Url.purge w*$

(3), example: Clear all caches:

/usr/local/varnish/bin/varnishadm-t Url.purge *$

4, a clear squid cache php function (clear varnish cache can also use this function, without any modification, very convenient):

function Purge ($IP, $url)
$errstr = ";
$errno = ";
$fp = Fsockopen ($ip, $errno, $ERRSTR, 2);
if (! $fp)
return false;
$out = "PURGE $url http/1.1\r\n";
$out. = "\r\n";
$out. = "connection:close\r\n\r\n";
Fputs ($fp, $out);
$out = Fgets ($fp, 4096);
Fclose ($FP);
return true;

Purge ("", "/index.php");

Attached 2: December 10, 2007, I wrote a daily 0-point run, cut varnish logs by day, generate a compressed file, and delete the script (/usr/local/varnish/var/varnish/ of the last month's log:
The contents of the/usr/local/varnish/var/varnish/ file are as follows:

# This file is run at 00:00
date=$ (date-d "Yesterday" + "%y-%m-%d")
/usr/local/varnish/bin/varnishncsa-n/usr/local/varnish/var/varnish/-w/usr/local/varnish/var/varnish/ Youvideo.log &
Gzip-c/usr/local/varnish/var/varnish/${date}.log >/usr/local/varnish/var/varnish/${date}.log.gz
Rm-f/usr/local/varnish/var/varnish/youvideo/$ (date-d "-1 month" + "%y-%m*"). log.gz

The settings are scheduled to be executed 00:00 every day:
Enter the following:
0 0 * * */bin/sh/usr/local/varnish/var/varnish/

--------------------------------------------------------------------------------------------------------------- -----------

Attached 3. The number of TCP connections varnish is less than squid because the varnish TCP connection is released faster than squid.

But the number of requests processed at the same time is varnish higher than squid, a varnish, another squid, the connection to them
The number of requests in real-time processing is 1 time times more than squid, and the average number of requests processed by varnish is 100 more than squid:

70979868 580.97 356.55 Client requests received
70897998 580.97 356.14 Cache Hits

/usr/local/squid/bin/squidclient-p Mgr:5min
Client_http.requests = 248.425264/sec
Client_http.hits = 245.135282/sec

If normal, Vcache only one file of size 1G in this directory:

--------------------------------------------------------------------------------------------------------------- ------------

Five.) Memcachedb

Memcachedb: Compiling and installing

Description: Memcachedb like memcache, the network socket data processing relies on libevent, so you need to download three installation packages, namely Libevent, Berkeley db, and memcachedb before installing.

Libevent download page:, download the latest stable version on the line.

Berkeley db download page: need to install version 4.6

Memcachedb download page: latest Version 0.1.0 version

In sequential installations, libevent and Berkeley DB are installed as usual, with the Linux environment as the standard.


[[email protected]] #tar-ZXVF libevent-1.3e.tar.gz
[[email protected]] #cd libevent-1.3e
[[Email Protected]]#./configure
[[email protected]] #make && make install

[======berkeley db=====]

[[email protected]] #tar-ZXVF db-4.6.19.tar.gz

#需要进入特定操作系统编译环境, there are some differences in the compilation of more conventional software

[[email protected]] #cd db-4.6.19/build_unix/


[Email protected]]#. /dist/configure

[[email protected]] #make && make install

#如果没有指定特殊安装路径, compile, the path to the Berkeley DB Runtime needs to be added to the system configuration

[[email protected]] #echo "/usr/local/berkeleydb.4.6/lib/" >>/etc/


[[email protected]] #ldconfig


[[email protected]] #tar-ZXVF memcachedb-0.1.0.tar.gz
[[email protected]] #cd memcachedb-0.1.0

#如果您要存储的内容的键值的长度和单笔内容的长度有变化, you need to change a file memcachedb.h

[[email protected]] #vi memcachedb.h


#define END_LEN----> This is the maximum length of key+value
#define END_LEN_STR----> This is the maximum length of the key


#默认key键值只预留了31个字节的长度, the maximum amount of data for a single pen is 32 bytes.

#例如您如果需要用这个玩艺来存储文本内容数据, suppose your key value is a 32-byte MD5, and the single data allows for a maximum of 10K, then you can set


#define End_len 102400----> This is the maximum length of key+value
#define END_LEN_STR----> This is the maximum length of the key


#修改完之后, just make it.

[[email protected]] #make

#编译完之后, a copy of the executable memcachedb will be generated to/usr/sbin/

[[email protected]] #cp-F memcachedb/usr/sbin/

#启动 Memcachedb

#第一种, read and write merge mode, that is, do not do the secondary copy, that is, read and written, do not do redundant backup

[[email protected]] #memcachedb-p21211-d-r-u root-h/data/mdb_11211-n

#第二种, read/write separation/Backup mode

#主服务器 Read-write listening 11211, synchronization via 31211 port, do not occupy the busy main service port

[[email protected]] #memcachedb-p21211-d-r-u root-h/data/mdb_11211_m-n-R

# secondary server read-only listening 21212, synchronizing data from the 31211 port of, while opening a 31212 port to provide data synchronization service externally

[[email protected]] #memcachedb-p21212-d-r-u root-h/data/mdb_11211_from_16801_s-o 31212-s

--------------------------------------------------------------------------------------------------------------- ---------


Schema varnish+nginx+php (FastCGI) +mysql5+mencache+mencachedb (iii)

Related Article

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: 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.