0

目前,我们为 magento 配置了使用松节油扩展的清漆。

我们的配置是magento + nginx + varnish + php5-fpm + redis。

Varnish 似乎对我们的后端 nginx 表现得很奇怪。任何人都可以帮助追踪我们有问题的原因吗?

我们尝试了很多配置更改,但没有任何效果。

查找 varnishstat :

0+00:00:16 命中率:0 0 0 平均命中率:0.0000 0.0000 0.0000

    5029         4.99       314.31 backend_busy - Backend conn. too many
    3999         2.00       249.94 backend_reuse - Backend conn. reuses
       5         0.00         0.31 backend_toolate - Backend conn. was closed
      31         0.00         1.94 backend_recycle - Backend conn. recycles
    1985         2.00       124.06 backend_retry - Backend conn. retry
     133         0.00         8.31 fetch_head - Fetch head
    3867         2.00       241.69 fetch_bad - Fetch had bad headers
     113         0.00         7.06 fetch_close - Fetch wanted close
    3983         2.00       248.94 fetch_oldhttp - Fetch pre HTTP/1.1 closed
       3         0.00         0.19 fetch_failed - Fetch failed
    1254         0.00        78.38 fetch_1xx - Fetch no body (1xx)
    2671         2.00       166.94 fetch_204 - Fetch no body (204)
      66          .            .   n_vbc - N struct vbc
      60          .            .   n_wrk - N worker threads
      26         3.99         1.62 n_wrk_create - N worker threads created
     616         0.00        38.50 n_wrk_failed - N worker threads not created
     616         0.00        38.50 n_wrk_lqueue - work request queue length
     911         2.00        56.94 n_wrk_queued - N queued work requests
      16         0.00         1.00 n_wrk_drop - N dropped work requests
       9          .            .   n_backend - N backends
      16          .            .   n_expired - N expired objects
      18          .            .   n_lru_nuked - N LRU nuked objects
       0         0.00         0.00 n_objsendfile - Objects sent with sendfile
      83         0.00         5.19 n_objwrite - Objects sent with write
       1         0.00         0.06 s_sess - Total Sessions
    1369         2.00        85.56 s_req - Total Requests
       5         0.00         0.31 s_pass - Total pass
    3918         2.00       244.88 s_bodybytes - Total body bytes
    5027         6.99       314.19 sess_pipeline - Session Pipeline
    3999         2.00       249.94 sess_readahead - Session Read Ahead
    2009         0.00       125.56 sess_herd - Session herd
    3994         2.00       249.62 shm_records - SHM records
 2301828      1574.75    143864.25 shm_writes - SHM writes
87280788     32612.36   5455049.25 shm_flushes - SHM flushes due to overflow
     205         0.00        12.81 shm_cont - SHM MTX contention
    3999          .            .   sms_nobj - SMS outstanding allocations
    6728          .            .   sms_nbytes - SMS outstanding bytes
  404240          .            .   sms_balloc - SMS bytes allocated
   35798          .            .   sms_bfree - SMS bytes freed
     297         0.00        18.56 n_vcl - N vcl total

Varnishlog如下:

22 TxHeader     - Accept-Language: en-US
22 TxHeader     - User-Agent: Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)
22 TxHeader     - X-Forwarded-Port: 80
22 TxHeader     - X-Forwarded-Proto: http
22 TxHeader     - X-Forwarded-For: 180.76.6.21, 10.130.235.245
22 TxHeader     - X-Varnish: 2058179311
22 TxHeader     - Accept-Encoding: gzip
23 RxProtocol   - HTTP/1.1
23 RxStatus     - 200
23 RxResponse   - OK
23 RxHeader     - Server: nginx
23 RxHeader     - Date: Sun, 28 Jul 2013 14:29:47 GMT
23 RxHeader     - Content-Type: text/html; charset=UTF-8
23 RxHeader     - Transfer-Encoding: chunked
23 RxHeader     - Connection: keep-alive
23 RxHeader     - Vary: Accept-Encoding
23 RxHeader     - X-Powered-By: PHP/5.3.10-1ubuntu3.7
23 RxHeader     - Set-Cookie: frontend=cru3ecd106vb38as19bjl9c2o5; expires=Sun, 28-Jul-2013 15:29:45 GMT; path=/
23 RxHeader     - Expires: Sun, 28 Jul 2013 14:29:46 GMT
23 RxHeader     - Last-Modified: Sun, 28 Jul 2013 14:29:46 GMT
23 RxHeader     - Cache-Control: no-cache, must-revalidate
23 RxHeader     - Pragma: no-cache
23 RxHeader     - Set-Cookie: nitrogento_cache_fullpage=0; expires=Sun, 28-Jul-2013 15:29:47 GMT; path=/; domain=www.yuvastyle.com
23 RxHeader     - Set-Cookie: nitrogento_last_store=default; expires=Sun, 28-Jul-2013  15:29:47 GMT; path=/; domain=www.yuvastyle.com
23 RxHeader     - Content-Encoding: gzip
21 TTL          - 2058179305 RFC 0 -1 -1 1375021788 0 1375021787 1375021786 0
21 VCL_call     - fetch
21 TTL          - 2058179305 VCL 123 -1 -1 1375021785 -3
21 VCL_return   - hit_for_pass
21 ObjProtocol  - HTTP/1.1
21 ObjResponse  - OK
21 ObjHeader    - Server: nginx
21 ObjHeader    - Date: Sun, 28 Jul 2013 14:29:47 GMT
21 ObjHeader    - Content-Type: text/html; charset=UTF-8
21 ObjHeader    - Vary: Accept-Encoding
21 ObjHeader    - X-Powered-By: PHP/5.3.10-1ubuntu3.7
21 ObjHeader    - Set-Cookie: frontend=cru3ecd106vb38as19bjl9c2o5; expires=Sun, 28-Jul-2013 15:29:45 GMT; path=/
21 ObjHeader    - Expires: Sun, 28 Jul 2013 14:29:46 GMT
21 ObjHeader    - Last-Modified: Sun, 28 Jul 2013 14:29:46 GMT
21 ObjHeader    - Cache-Control: no-cache, must-revalidate
21 ObjHeader    - Pragma: no-cache

请找到我们由 magento 的 turpentine 扩展生成的 VCL 文件:

C{
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
static pthread_mutex_t lrand_mutex = PTHREAD_MUTEX_INITIALIZER;
void generate_uuid(char* buf) {
pthread_mutex_lock(&lrand_mutex);
long a = lrand48();
long b = lrand48();
long c = lrand48();
long d = lrand48();
pthread_mutex_unlock(&lrand_mutex);
sprintf(buf, "frontend=%08lx-%04lx-%04lx-%04lx-%04lx%08lx",
a,
b & 0xffff,
(b & ((long)0x0fff0000) >> 16) | 0x4000,
(c & 0x0fff) | 0x8000,
(c & (long)0xffff0000) >> 16,
d
);
return;
}
}C
import std;
backend default {
.host = "127.0.0.1";
.port = "8080";
.first_byte_timeout = 300s;
.between_bytes_timeout = 300s;
.connect_timeout = 5s; 
.saintmode_threshold = 20;
.first_byte_timeout = 20s;
.max_connections = 50;
}
backend admin {
.host = "127.0.0.1";
.port = "8080";
.first_byte_timeout = 21600s;
.between_bytes_timeout = 21600s;
}
acl crawler_acl {
"127.0.0.1";
}
acl debug_acl {
}
sub remove_cache_headers {
unset beresp.http.Cache-Control;
unset beresp.http.Expires;
unset beresp.http.Pragma;
unset beresp.http.Cache;
unset beresp.http.Age;
}
sub remove_double_slashes {
set req.url = regsub(req.url, "(.*)//+(.*)", "\1/\2");
}
sub generate_session {
if (req.url ~ ".*[&?]SID=([^&]+).*") {
set req.http.X-Varnish-Faked-Session = regsub(
req.url, ".*[&?]SID=([^&]+).*", "frontend=\1");
} else {
C{
char uuid_buf [50];
generate_uuid(uuid_buf);
VRT_SetHdr(sp, HDR_REQ,
"\030X-Varnish-Faked-Session:",
uuid_buf,
vrt_magic_string_end
);
}C
}
if (req.http.Cookie) {
std.collect(req.http.Cookie);
set req.http.Cookie = req.http.X-Varnish-Faked-Session +
"; " + req.http.Cookie;
} else {
set req.http.Cookie = req.http.X-Varnish-Faked-Session;
}
}
sub generate_session_expires {
C{
time_t now = time(NULL);
struct tm now_tm = *gmtime(&now);
now_tm.tm_sec += 3600;
mktime(&now_tm);
char date_buf [50];
strftime(date_buf, sizeof(date_buf)-1, "%a, %d-%b-%Y %H:%M:%S %Z", &now_tm);
VRT_SetHdr(sp, HDR_RESP,
"\031X-Varnish-Cookie-Expires:",
date_buf,
vrt_magic_string_end
);
}C
}
sub vcl_recv {
if (req.restarts == 0) {
if (req.http.X-Forwarded-For) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
if (!true || req.http.Authorization ||
req.request !~ "^(GET|HEAD)$" ||
req.http.Cookie ~ "varnish_bypass=1") {
return (pipe);
}
call remove_double_slashes;
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} else if (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}
if (req.url ~ "^(/)(?:(?:index|litespeed)\.php/)?") {
set req.http.X-Turpentine-Secret-Handshake = "1";
if (req.url ~ "^(/)(?:(?:index|litespeed)\.php/)?yuvadmin") {
set req.backend = admin;
return (pipe);
}
if (req.http.Cookie ~ "\bcurrency=") {
set req.http.X-Varnish-Currency = regsub(
req.http.Cookie, ".*\bcurrency=([^;]*).*", "\1");
}
if (req.http.Cookie ~ "\bstore=") {
set req.http.X-Varnish-Store = regsub(
req.http.Cookie, ".*\bstore=([^;]*).*", "\1");
}
if (req.url ~ "/turpentine/esi/getBlock/") {
set req.http.X-Varnish-Esi-Method = regsub(
req.url, ".*/method/(\w+)/.*", "\1");
set req.http.X-Varnish-Esi-Access = regsub(
req.url, ".*/access/(\w+)/.*", "\1");
if (req.http.X-Varnish-Esi-Method == "esi" && req.esi_level == 0 &&
!(true || client.ip ~ debug_acl)) {
error 403 "External ESI requests are not allowed";
}
}
if (req.http.Cookie !~ "frontend=") {
if (client.ip ~ crawler_acl ||
req.http.User-Agent ~ "^(?:ApacheBench/.*|.*Googlebot.*|JoeDog/.*Siege.*|magespeedtest\.com|Nexcessnet_Turpentine/.*)$") {
set req.http.Cookie = "frontend=crawler-session";
} else {
call generate_session;
}
}
if (true &&
req.url ~ ".*\.(?:css|js|jpe?g|png|gif|ico|swf)(?=\?|&|$)") {
unset req.http.Cookie;
unset req.http.X-Varnish-Faked-Session;
return (lookup);
}
if (req.url ~ "^(/)(?:(?:index|litespeed)\.php/)?(?:yuvadmin|api|cron\.php)" ||
req.url ~ "\?.*__from_store=") {
return (pipe);
}
if (true &&
req.url ~ "(?:[?&](?:__SID|XDEBUG_PROFILE)(?=[&=]|$))") {
return (pass);
}
if (req.url ~ "[?&](utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=") {
set req.url = regsuball(req.url, "(?:(\?)?|&)(?:utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl)=[^&]+", "\1");
set req.url = regsuball(req.url, "(?:(\?)&|\?$)", "\1");
}
return (lookup);
}
}
sub vcl_pipe {
unset bereq.http.X-Turpentine-Secret-Handshake;
set bereq.http.Connection = "close";
}
sub vcl_hash {
hash_data(req.url);
if (req.http.Host) {
hash_data(req.http.Host);
} else {
hash_data(server.ip);
}
hash_data(req.http.Ssl-Offloaded);
if (req.http.X-Normalized-User-Agent) {
hash_data(req.http.X-Normalized-User-Agent);
}
if (req.http.Accept-Encoding) {
hash_data(req.http.Accept-Encoding);
}
if (req.http.X-Varnish-Store || req.http.X-Varnish-Currency) {
hash_data("s=" + req.http.X-Varnish-Store + "&c=" + req.http.X-Varnish-Currency);
}
if (req.http.X-Varnish-Esi-Access == "private" &&
req.http.Cookie ~ "frontend=") {
hash_data(regsub(req.http.Cookie, "^.*?frontend=([^;]*);*.*$", "\1"));
}
return (hash);
}
sub vcl_hit {
}
sub vcl_fetch {
set req.grace = 15s;
if (req.url ~ "^(/)(?:(?:index|litespeed)\.php/)?") {
unset beresp.http.Vary;
set beresp.do_gzip = true;
if (beresp.status != 200 && beresp.status != 404) {
set beresp.ttl = 5d;
return (hit_for_pass);
} else {
if (beresp.http.Set-Cookie) {
set beresp.http.X-Varnish-Set-Cookie = beresp.http.Set-Cookie;
unset beresp.http.Set-Cookie;
}
call remove_cache_headers;
if (beresp.http.X-Turpentine-Esi == "1") {
set beresp.do_esi = true;
}
if (beresp.http.X-Turpentine-Cache == "0") {
set beresp.ttl = 5d;
return (hit_for_pass);
} else {
if (true &&
bereq.url ~ ".*\.(?:css|js|jpe?g|png|gif|ico|swf)(?=\?|&|$)") {
set beresp.ttl = 128800s;
set beresp.http.Cache-Control = "max-age=128800";
} elseif (req.http.X-Varnish-Esi-Method) {
if (req.http.X-Varnish-Esi-Access == "private" &&
req.http.Cookie ~ "frontend=") {
set beresp.http.X-Varnish-Session = regsub(req.http.Cookie,
"^.*?frontend=([^;]*);*.*$", "\1");
}
if (req.http.X-Varnish-Esi-Method == "ajax" &&
req.http.X-Varnish-Esi-Access == "public") {
set beresp.http.Cache-Control = "max-age=" + regsub(
req.url, ".*/ttl/(\d+)/.*", "\1");
}
set beresp.ttl = std.duration(
regsub(
req.url, ".*/ttl/(\d+)/.*", "\1s"),
300s);
if (beresp.ttl == 0s) {
set beresp.ttl = 15s;
return (hit_for_pass);
}
} else {
set beresp.ttl = 3600s;
}
}
}
return (deliver);
}
}
sub vcl_deliver {
if (req.http.X-Varnish-Faked-Session) {
call generate_session_expires;
set resp.http.Set-Cookie = req.http.X-Varnish-Faked-Session +
"; expires=" + resp.http.X-Varnish-Cookie-Expires + "; path=/";
if (req.http.Host) {
set resp.http.Set-Cookie = resp.http.Set-Cookie +
"; domain=" + regsub(req.http.Host, ":\d+$", "");
}
set resp.http.Set-Cookie = resp.http.Set-Cookie + "; httponly";
unset resp.http.X-Varnish-Cookie-Expires;
}
if (true || client.ip ~ debug_acl) {
set resp.http.X-Varnish-Hits = obj.hits;
set resp.http.X-Varnish-Esi-Method = req.http.X-Varnish-Esi-Method;
set resp.http.X-Varnish-Esi-Access = req.http.X-Varnish-Esi-Access;
set resp.http.X-Varnish-Currency = req.http.X-Varnish-Currency;
set resp.http.X-Varnish-Store = req.http.X-Varnish-Store;
} else {
unset resp.http.X-Varnish;
unset resp.http.Via;
unset resp.http.X-Powered-By;
unset resp.http.Server;
unset resp.http.X-Turpentine-Cache;
unset resp.http.X-Turpentine-Esi;
unset resp.http.X-Turpentine-Flush-Events;
unset resp.http.X-Turpentine-Block;
unset resp.http.X-Varnish-Session;
unset resp.http.X-Varnish-Set-Cookie;
}
}
4

2 回答 2

1

我对源代码进行了一些研究,如下所示:

    // this is checking the maximum connection which is defined in backend
    if (vs->vrt->max_connections > 0 &&
    bp->n_conn >= vs->vrt->max_connections) {
    VSC_C_main->backend_busy++;
    return (NULL);
}

在 .rst 文件的声明部分:

后端声明

后端声明创建并初始化一个命名的后端对象:::

后端 www { .host = "www.example.com"; .port = "http"; }

稍后可以使用后端对象在请求时选择后端:::

if (req.http.host ~ "(?i)^(www.)?example.com$") { 设置 req.backend = www; }

为避免后端服务器过载,可以设置 .max_connections 来限制最大并发后端连接数。

可以在后端声明中覆盖超时参数。超时参数是 .connect_timeout 用于等待后端连接的时间,.first_byte_timeout 用于等待来自后端的第一个字节的时间,以及 .between_bytes_timeout 用于在每个接收到的字节之间等待的时间。

这是示例设置:

backend default { 
  .host = "localhost";  
  .port = "80";  
  .connect_timeout = 0.5s; 
  .between_bytes_timeout = 5s;  
  .saintmode_threshold = 20;
  .first_byte_timeout = 20s;
  .max_connections = 50;
}
于 2013-07-28T14:55:34.293 回答
0

Try updating your libvarnishapi lib — this helped me (ubuntu):

sudo app-install libvarnishapi1

and reboot.

If you are using a wrong version (mine was libvarnishapi-dev), you don't get varnishstat working properly.

于 2014-06-30T09:14:05.410 回答