以前建站的时候遇到过一些网站访问量大导致后台处理程序响应超时的问题。当时采用的架构是nginx+php-fastcgi,同事想到了用nginx-proxycache做页面缓存,效果还不错。接下来介绍一下varnish的使用技巧。严格来说varnish是一个可以作为代理服务器的软件,直接将http请求转发给php-cgi,然后交给php处理。Varnish会获取PHP处理后的数据。***返回浏览器。不过,现在php-fastcgi已经被淘汰了,也就是说我们一般不用php-fastcgi,所以我们不能直接把varnish和php结合起来,因为php-fpm的交互方式是socket,不是然后监听机器的9000端口,所以必须要找一个媒介来连接varnish和php-fpm,nginx可以充当这个媒介,如下图:那么问题来了,据研究,varnish不像像nginx一样高效地处理http请求。所以如果我们让nginx来充当转发,效率会更高。那么我们需要如何实现这个目标呢?下面梳理一下流程,实现图3的架构。需要提前准备好nginx、varnish、php-fpm、php软件。操作系统是ubuntu。所有软件都可以使用apt-getinstall安装。如果不知道包的全名,可以先apt-getupdate,更新源,然后使用apt-cachesearchxxx查找包名安装varnish后,可以使用servicevarnish回车查看可操作的选项*用法:/etc/init.d/varnish{start|stop|restart|reload|force-reload|configtest},一般安装完成后系统会自动启动varnish,nginx就是同理,配置就不细说了。安装完需要的软件后,需要配置这些软件来实现这个架构。/nginx.confhttp{##proxyglobalsettingproxy_connect_timeout5;proxy_read_timeout60;proxy_send_timeout5;proxy_buffer_size16k;proxy_buffers464k;proxy_busy_buffers_size128k;##END##cacheproxypassupstreamcache{server127.0.0.1:6081;}##END##phpproxypassupstream0.0.1:6081;}##END##phpproxypassupstream0.80;server127:}##END#BasicSettingssendfileon;tcp_nopushon;tcp_nodelayon;keepalive_timeout65;types_hash_max_size2048;server_tokensoff;#dependonnginx-extras需要安装nginx-extras才能定义Servermore_set_headers'Server:Bird-shark';#server_names_hash_bucket_size64;#server_name_in_redirectoff;include/etc/nginx/mime.types;default_typeapplication/octet-stream;###SSLSettings##ssl_protocolsTLSv1TLSv1.1TLSv1.2;#DroppingSSLv3,ref:POODLEssl_prefer_server_cipherson;###LoggingSettings##access_log/var/log/nginx/access.log;error_log/var/log/nginx/error.log;###GzipSettings##gzipon;gzip_disable"msie6";gzip_varyon;gzip_proxiedany;gzip_comp_level6;gzip_buffers168k;gzip_http_version1.1;gzip_typestext/plaintext/cssapplication/jsonapplication/javascripttext/xmlapplication/xmlapplication/xml+rsstext/javascript;###VirtualHostConfigs##include/etc/nginx/conf.d/*.conf;include/etc/nginx/sites-enabled/*;}varnish部分vi/etc/varnish/default.vclserver{listen80default_server;listen[::]:80default_server;indexindex.htmlindex.htmindex.php;ser??ver_namelocalhost;location~.*\.(gif|jpg|png|css|js|flv|ico|swf|html)${proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_passhttp://cache;}#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000#location/{proxy_通过http://php;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_pass_headerServer;}}server{listen8080;root/var/www/html;index.htmlindex.htmindex.php;location/{if(!-e$request_filename){rewrite^(.*)$/index.php?s=$1last;break;}try_files$uri$uri/=404;}location~^(.+\.php)(.*)${fastcgi_passunix:/var/run/php5-fpm.sock;fastcgi_intercept_errorson;fastcgi_buffers8128k;fastcgi_indexindex.php;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;includefastcgi_params;}}模块测试分析不使用Incasevi/etc/nginx/sites-available/default#location~.*\.(gif|jpg|png|css|js|flv|ico|swf|html)${#proxy_set_headerHost$host;#proxy_set_headerX-Real-IP$remote_addr;#proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;#proxy_passhttp://cache;#}首先使用chrome浏览器访问查看请求头,然后使用curl,在服务器上执行如下命令curl-k-v'http://192.168.99.1/Public/Home/images/t_navigation_logo.png'-H'Pragma:no-cache'-H'Accept-En编码:gzip,deflate,sdch'-H'接受语言:zh,en;q=0.8,zh-CN;q=0.6'-H'升级不安全请求:1'-H'用户代理:Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/52.0.2743.116Safari/537.36'-H'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'-H'Connection:keep-alive'--压缩后发现有输出内容然后,取消勾选禁用缓存并执行以下命令curl-k-v'http://192.168.99.1/Public/Home/images/t_navigation_logo.png'-H'If-None-Match:"57c6b733-1962"在服务器'-H'Accept-Encoding:gzip,deflate,sdch'-H'Accept-Language:zh,en;q=0.8,zh-CN;q=0.6'-H'Upgrade-Insecure-Requests:1'-H'User-Agent:Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/52.0.2743.116Safari/537.36'-H'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'-H'Connection:keep-alive'-H'If-Modified-Since:Wed,31Aug201610:53:39GMT'--压缩found只返回头部信息,没有返回内容。然后我们比较两个命令,发现区别在于-H'Pragma:no-cache'和-H'If-Modified-Since:Wed,31Aug201610:53:39GMT'-H'If-None-match:"57c6b733-1962"'57c6b733-1962这串字符对应服务器响应给浏览器的ETag部分的内容,然后我们修改部分内容curl-k-v'http://192.168.99.1/Public/Home/images/t_navigation_logo.png'-H'If-None-Match:"57c6b733-1234"'-H'Accept-Encoding:gzip,deflate,sdch'-H'Accept-Language:zh,en;q=0.8,zh-CN;q=0.6'-H'升级-Insecure-Requests:1'-H'User-Agent:Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/52.0.2743.116Safari/537.36'-H'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'-H'Connection:keep-alive'-H'If-Modified-Since:Wed,31Aug201610:53:39GMT'--compressed在服务器端执行,发现有内容要返回,所以这个ETag相当于token,不是nginx随机生成的,应该和请求链接一一对应,用来识别缓存,当服务器返回状态为304时,此时我们的浏览器会直接去本地缓存数据中查找。2、在使用缓存模块的情况下,vi/etc/nginx/sites-available/defaultlocation~.*\.(gif|jpg|png|css|js|flv|ico|swf|html)${proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_passhttp://cache;}用浏览器查看响应头,发现X-Cache:MISSfrom192.168.99.1。这说明缓存没有安装,然后我们从192.168.99.1刷新X-Cache:HIT,发现已经安装了。对于已经安装好的资源文件,如果我们删除它们会怎么样呢?答案是除非重启或者清除缓存,否则它们仍然可以访问,但是PURGE显然是不对外开放的。下面是服务端curl清除varnish缓存的命令是curl-v-k-XPURGEhttp://localhost/Public/Home/css/t_navigation.css结论varnish是内存型缓存软件,不是物理缓存类似nginx扩展proxy_cache的类软件,访问速度比较快,但也有缺点。重启后,所有的缓存都要重写。无论如何,任何适用于任何货架的场景都需要进行彻底的研究才能满足业务需求,我才意识到我的想法。毕竟资源和精力有限,随便玩玩,都是看客。看看就好,别太当真,知道是怎么回事就知道了。
