[英]Using varnish to cache heroku app
我很早以前就安裝了清漆,並且我將后端之一設置為host.herokuapp.com,效果很好。 一段時間以來,我能夠更改設置並使用基本service varnish reload
命令重新加載varnish配置。
現在,當我嘗試重新加載它時,我得到:
* Reloading HTTP accelerator varnishd Command failed with error code 106
Message from VCC-compiler:
Backend host "myapp.herokuapp.com": resolves to multiple IPv4 addresses.
Only one address is allowed.
Please specify which exact address you want to use, we found these:
154.129.225.36
13.21.108.188
50.10.185.176
50.13.98.193
54.125.177.29
54.213.81.135
107.25.192.112
174.139.35.141
('/etc/varnish/backends.vcl' Line 39 Pos 27)
backend mobile { .host = "myapp.herokuapp.com"; .port = "80"; }
--------------------------#####################-----------------
In backend specification starting at:
('/etc/varnish/backends.vcl' Line 39 Pos 1)
backend mobile { .host = "myapp.herokuapp.com"; .port = "80"; }
#######---------------------------------------------------------------
Running VCC-compiler failed, exit 1
VCL compilation failed
Error: vcl.load 7ba71b44-c6b9-40e9-b0be-18f02bb5e9be /etc/varnish/default.vcl failed
由於heroku使用動態IP為其動態特性,因此IP列表會不斷變化,因此將IP設置為后端毫無意義。 有什么辦法可以解決這個問題嗎?
我對acquia托管的服務器幾乎有同樣的問題。
解決該問題的一種方法是:-通過在單獨的vcl中默認獲取來自主機服務器的后端IP-構建一個可以定期更新vcl的croned腳本(如果后端發生了更改)-重新啟動清漆以將新的后端投入生產
#!/usr/bin/python2.7
import socket
import subprocess
import re
#
# Do a nslookup and return the list of the IPs
#
def _nslookup(host):
ips = ""
ips = socket.getaddrinfo(host ,0,0,0,0)
ip_list = []
for result in ips:
ip_list.append(result[-1][0])
ip_list = list(set(ip_list))
return ip_list
#
# Compare current backends with the list returned by nslookup
#
def _compare_backends_vcl(host_name, group_name):
current_ips = []
current_ips = _nslookup(host_name)
# Get current backends
current_backends = []
list = subprocess.Popen("/usr/bin/varnishadm backend.list | grep " + group_name + " | awk '{print $1}'", shell=True, stdout=subprocess.PIPE)
backend = ""
for backend in list.stdout:
current_backends.append(re.sub(r'^.*\((.*),.*,.*$\n', r'\1', backend))
# Due to varnish bug that is not removing backends (old ones are still declared in backend.list
# we are forced to add backends
# So the nslookup should be part of the current set of backends
# if set(current_ips).symmetric_difference(current_backends):
if set(current_ips).difference(current_backends):
# List is present so difference exist
print "_compare: We have to update " + group_name
return True
else:
return False
#
# Write the corresponding file
#
def _write_backends_vcl(host_name, group_name):
TEMPLATE_NODE = '''backend %s {
\t.host = "%s";
\t.port = "80";
\t.probe = %s;
}'''
vcl_file = open("/etc/varnish/" + group_name + "_backends.vcl", 'w')
host_num = 1
hosts = _nslookup(host_name)
for host in hosts:
vcl_file.write(TEMPLATE_NODE % (group_name + "_" + str(host_num), host, group_name + "_probe"))
vcl_file.write("\n\n")
host_num +=1
vcl_file.write("director " + group_name + "_default round-robin {\n")
for i in range(len(hosts)):
node = group_name + "_" + str(i+1)
vcl_file.write("\t{ .backend = %s; }\n" % node)
vcl_file.write("}\n")
vcl_file.close()
# Main
do_reload = ""
if _compare_backends_vcl("myhost.prod.acquia-sites.com", "MYHOST_CONFIG"):
do_reload = True
_write_backends_vcl("myhost.prod.acquia-sites.com", "MYHOST_CONFIG")
if do_reload:
print "Reloading varnish"
subprocess.Popen(['sudo', '/etc/init.d/varnish', 'reload'])
exit(1)
else:
# print "Everything is ok"
exit(0)
然后,相應的vcl如下所示:
backend MYHOST_CONFIG_1 {
.host = "XX.XX.XX.XX";
.port = "80";
.probe = MYHOST_CONFIG_probe;
}
backend MYHOST_CONFIG_2 {
.host = "XX.XX.XX.XX";
.port = "80";
.probe = MYHOST_CONFIG_probe;
}
director MYHOST_CONFIG_default round-robin {
{ .backend = MYHOST_CONFIG_1; }
{ .backend = MYHOST_CONFIG_2; }
}
您必須設置MYHOST_CONFIG_probe探針,並將MYHOST_CONFIG_default設置為配置的導向器。 注意清漆存儲所有后端,因此您必須定期重新啟動它以清除有缺陷的服務器
我今天有同樣的問題。
因此,我在端口3000上安裝了Nginx服務器,並在myapp.herokuapp.com上設置了proxy_pass服務器。
然后,將host="myapp.herokuapp.com"
和port="80"
為host="127.0.0.1"
和port="3000"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.