標籤:odi recv web服務 ref varnish管理 module sts 查看 uri
Varnish,Nginx搭建快取服務器
一. varnish1.安裝pcre庫,相容Regex# tar -zxvf pcre-8.10.tar.gz# cd pcre-8.10# ./configure --prefix=/usr/local/pcre# make && make install2.配置安裝varnish# tar -zxvf varnish-3.0.2.tar.gz# cd varnish-3.0.2# export PKG_CONFIG_PATH=/usr/local/pcre/lib/pkgconfig/# ./configure --prefix=/usr/local/varnish# make && make install3.修改varnish設定檔/usr/local/varnish/etc/varnish/default.vcl# mv default.vcl default.vcl.bak# vi cq.vclbackend cqserver {.host = "127.0.0.1";.port = "8087";.connect_timeout = 20s;}acl purge {"localhost";"127.0.0.1";"192.168.1.0"/24;}sub vcl_recv {if (req.request == "PURGE") {if (!client.ip ~ purge) {error 405 "Not allowed.";}return (lookup);}if (req.http.host ~ "^www.baidu.com") {set req.backend = cqserver;if (req.request != "GET" && req.request != "HEAD") {return (pipe);}else{return (lookup);}}else {error 404 "caoqing Cache Server";return (lookup);}}sub vcl_hit {if (req.request == "PURGE") {set obj.ttl = 0s;error 200 "Purged.";}}sub vcl_miss {if (req.request == "PURGE") {error 404 "Not in cache.";}}(1)Varnish通過反向 Proxy請求後端IP為127.0.0.1,連接埠為8087的web伺服器,即nginx伺服器監聽連接埠;(2)Varnish允許localhost、127.0.0.1、192.168.1.*三個來源IP通過PURGE方法清除緩衝;(3)Varnish對網域名稱為www.baidu.com的請求進行處理,非www.baidu.com網域名稱的請求則返回"caoqing Cache Server";(4)Varnish對HTTP協議中的GET、HEAD請求進行緩衝,對POST請求透過,讓其直接存取後端Web伺服器。4.啟動varnish# cd /usr/local/varnish/sbin/# ./varnishd -f /usr/local/varnish/etc/varnish/cq.vcl -s file,/var/varnish_cache,1G -T 127.0.0.1:2000 -a 0.0.0.0:80二. nginx1.安裝nginx# rpm -ivh zlib-devel-1.2.3-4.el5.i386.rpm# tar zxvf nginx-1.4.1.tar.gz# cd nginx-1.4.1# ./configure --prefix=/usr/local/nginx --with-openssl=/usr/lib --with-pcre=/root/tool/pcre-8.10 --with-http_stub_status_module# make && make install2.啟動nginx# cd /usr/local/nginx/sbin# ./nginx3.修改ngnix設定檔測試組態檔案/usr/local/nginx/sbin/./nginx -t# cd /usr/local/nginx/conf# vi nginx.conf#使用的使用者和組user root root;#制定工作繁衍的處理序數(一般為CPU總核心數兩倍)worker_processes 8;#制定檔案描述符數量worker_rlimit_nofile 51200;#指定錯誤記錄檔存放路徑error_log logs/error.log#指定pid存放路徑pid logs/nginx.pid;event{ #使用網路I/O模型 use epoll;#允許的串連數 worker_connections 65535;}http {#訪問日誌存放路徑 access_log logs/access.log;#開啟gzip壓縮 gzip on; gzip_min_length 1000; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/rss+xml application/xhtml+xml application/atom_xml; gzip_disable "MSIE [1-6].(?!.*SV1)";#限定PHP-CGI的串連、發送和讀取的時間為300s fastcgi_connect_timeout 300s; fastcgi_send_timeout 300s; fastcgi_read_timeout 300s;#虛擬機器主機配置server {#監聽連接埠 listen 8087;#主機名稱 server_name 127.0.0.1;#google提供的DNS服務 resolver 8.8.8.8 location / {#nginx作為HTTPProxy 伺服器 proxy_pass http://$http_host$request_uri; proxy_set_header Accept-Encoding ‘‘; proxy_redirect off; }}}三. 排錯最佳化1)修改環境變數vi ~/.bashrcPATH=$PATH:/usr/local/nginx/sbin:/usr/local/varnish/sbin:/usr/local/varnish/binexport PATH2)nginx啟動與關閉參數:-v:查看版本-V:查看配置模組-t:查看設定檔是否正確-c:制定其他設定檔pkill -9 nginx3)varnish啟動與關閉參數: -u:以什麼使用者運行-g:以什麼組運行-f:varnish設定檔 -a:綁定IP和連接埠-s:varnish緩衝位置和大小-w:最小,最大線程和逾時時間-T:varnish管理連接埠,主要用來清除緩衝pkill varnishdvarnish服務動態載入設定檔 #varnishadm -T 127.0.0.1:2000vcl.load vcl-name_vcl "設定檔路徑" # vcl-name 這裡可以任意取名 vcl.use vcl-namevcl.show vcl-name #顯示vcl-name設定檔內容 # varnishncsa -w /usr/local/varnish/logs/varnish.log將輸出日誌寫入到制定記錄檔。4)修改windows用戶端C:\Windows\System32\drivers\etc\hosts192.168.1.202 www.baidu.com
來源:https://my.oschina.net/u/1449160/blog/198650
Varnish,Nginx搭建快取服務器