因此我使用 Ubuntu 12.04、nginx、php5-fpm 和 varnish 设置了一个基于云的新实例。
在我安装和配置 Varnish 之前,网站运行良好,虚拟主机也运行正常。设置 Varnish 后,我现在收到错误 503 服务不可用。
我的 nginx 配置如下:
server {
listen xxx.xxx.xxx.xxx:8080;
server_name example.com ;
root /var/www/example.com/public_html;
if ($http_host != "example.com") {
rewrite ^ http://www.example.com$request_uri permanent;
}
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
#Roots theme clean URL rewrites
location ~ ^/assets/(img|js|css)/(.*)$ {
try_files $uri $uri/ /wp-content/themes/sitename/assets/$1/$2;
}
location ~ ^/plugins/(.*)$ {
try_files $uri $uri/ /wp-content/plugins/$1;
}
}
/etc/default/varnish 看起来像这样:
# Configuration file for varnish
#
# /etc/init.d/varnish expects the variables $DAEMON_OPTS, $NFILES and $MEMLOCK
# to be set from this shell script fragment.
#
# Note: If systemd is installed, this file is obsolete and ignored. You will
# need to copy /lib/systemd/system/varnish.service to /etc/systemd/system/ and
# edit that file.
# Should we start varnishd at boot? Set to "no" to disable.
START=yes
# Maximum number of open files (for ulimit -n)
NFILES=131072
# Maximum locked memory size (for ulimit -l)
# Used for locking the shared memory log in memory. If you increase log size,
# you need to increase this number as well
MEMLOCK=82000
# Default varnish instance name is the local nodename. Can be overridden with
# the -n switch, to have more instances on a single server.
# INSTANCE=$(uname -n)
# This file contains 4 alternatives, please use only one.
## Alternative 1, Minimal configuration, no VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# content server on localhost:8080. Use a 1GB fixed-size cache file.
#
# DAEMON_OPTS="-a :6081 \
# -T localhost:6082 \
# -b localhost:8080 \
# -u varnish -g varnish \
# -S /etc/varnish/secret \
# -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G"
## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request. Use a 1GB
# fixed-size cache file.
#
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
/etc/varnish/default.vcl 如下所示:
backend default {
.host = "xxx.xxx.xxx.xxx";
.port = "8080"; }
检查 varnishlog 我看到这个:
11 SessionOpen c xx.xxx.xxx.xxx 4712 :80
11 ReqStart c xx.xxx.xxx.xxx 4712 1475226459
11 RxRequest c GET
11 RxURL c /
11 RxProtocol c HTTP/1.1
11 RxHeader c Host: example.com
11 RxHeader c Connection: keep-alive
11 RxHeader c Cache-Control: max-age=0
11 RxHeader c Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
11 RxHeader c User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.155 Safari/537.22
11 RxHeader c Accept-Encoding: gzip,deflate,sdch
11 RxHeader c Accept-Language: en-US,en;q=0.8
11 RxHeader c Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
11 RxHeader c Cookie: __utma=148547044.766489551.1362139914.1362151355.1362156101.3; __utmz=148547044.1362139914.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __cfduid=d92c50421d4bba63041d6d12ba960d3151362551863; wordpress_test_cookie=WP+Cookie+check; wordpress_lo
11 VCL_call c recv lookup
11 VCL_call c hash
11 Hash c /
11 Hash c example.com
11 VCL_return c hash
11 VCL_call c miss fetch
11 FetchError c no backend connection
11 VCL_call c error deliver
11 VCL_call c deliver deliver
11 TxProtocol c HTTP/1.1
11 TxStatus c 503
11 TxResponse c Service Unavailable
11 TxHeader c Server: Varnish
11 TxHeader c Content-Type: text/html; charset=utf-8
11 TxHeader c Retry-After: 5
11 TxHeader c Content-Length: 419
11 TxHeader c Accept-Ranges: bytes
11 TxHeader c Date: Wed, 06 Mar 2013 20:14:13 GMT
11 TxHeader c X-Varnish: 1475226459
11 TxHeader c Age: 0
11 TxHeader c Via: 1.1 varnish
11 TxHeader c Connection: close
11 Length c 419
11 ReqEnd c 1475226459 1362600853.513662100 1362600853.513980627 0.000180006 0.000249863 0.000068665
11 SessionClose c error
11 StatSess c 70.194.150.230 4712 0 1 1 0 0 0 257 419
11 SessionOpen c 70.194.150.230 4713 :80
11 ReqStart c 70.194.150.230 4713 1475226460
11 RxRequest c GET
11 RxURL c /favicon.ico
11 RxProtocol c HTTP/1.1
11 RxHeader c Host: example.com
11 RxHeader c Connection: keep-alive
11 RxHeader c Accept: */*
11 RxHeader c User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.155 Safari/537.22
11 RxHeader c Accept-Encoding: gzip,deflate,sdch
11 RxHeader c Accept-Language: en-US,en;q=0.8
11 RxHeader c Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
11 VCL_call c recv lookup
11 VCL_call c hash
11 Hash c /favicon.ico
11 Hash c example.com
11 VCL_return c hash
11 VCL_call c miss fetch
11 FetchError c no backend connection
11 VCL_call c error deliver
11 VCL_call c deliver deliver
11 TxProtocol c HTTP/1.1
11 TxStatus c 503
11 TxResponse c Service Unavailable
11 TxHeader c Server: Varnish
11 TxHeader c Content-Type: text/html; charset=utf-8
11 TxHeader c Retry-After: 5
11 TxHeader c Content-Length: 419
11 TxHeader c Accept-Ranges: bytes
11 TxHeader c Date: Wed, 06 Mar 2013 20:14:13 GMT
11 TxHeader c X-Varnish: 1475226460
11 TxHeader c Age: 0
11 TxHeader c Via: 1.1 varnish
11 TxHeader c Connection: close
11 Length c 419
11 ReqEnd c 1475226460 1362600853.751207590 1362600853.751491070 0.000154257 0.000221491 0.000061989
11 SessionClose c error
11 StatSess c 70.194.150.230 4713 0 1 1 0 0 0 257 419
答案1
看起来 Varnish 无法连接到你的 nginx 后端 - 你能通过访问来连接到它吗?http://xxx.xxx.xxx.xxx:8080?