我设置了 nginx 作为前端反向代理 + varnish 缓存 + Web 应用程序后端,请求通过 nginx -> varnish -> 后端,我可以看到各个方面的日志,但它返回一个空白的 200 响应所有请求。
我的/etc/default/varnish.vcl
vcl 4.0;
backend jira {
.host = "127.0.0.1";
.port = "27988";
}
sub vcl_recv {
set req.backend_hint = jira;
if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
unset req.http.Cookie;
return (pipe);
} else {
return (pass);
}
}
sub vcl_backend_response {
if (bereq.url ~ "\.(css|js|jpg|jpeg|gif|png|ico)$") {
unset beresp.http.set-cookie;
set beresp.ttl = 30m;
#Set Grace Time to one hour
set beresp.grace = 1h;
}
}
我的 nginx 配置
server {
listen 80;
server_name jira.lan.mysite.com;
location / {
proxy_pass http://127.0.0.1:6081/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
80 - nginx port
6081 - varnish port
27988 - backend server(jira) port
对 jira.lan.fongwell.com
的请求首先转到 nginx,然后转到 varnish,如果不是静态资源,则转到后端,但都返回空的 200 响应。
如果我绕过 nginx 并转到 varnish,例如。 192.168.0.119:6081
,其中6081
是varnish端口,那么一切正常!
浏览器返回的http响应头
Accept-Ranges bytes
Age 0
Cache-Control no-cache, no-store, must-revalidate
Connection keep-alive
Content-Encoding gzip
Content-Length 0
Content-Type text/html;charset=UTF-8
Date Fri, 11 Dec 2015 01:50:57 GMT
Expires Thu, 01 Jan 1970 00:00:00 GMT
Pragma no-cache
Server nginx/1.4.6 (Ubuntu)
Set-Cookie JSESSIONID=C49D71942379289A803041B4257E6328; Path=/; HttpOnly
Vary User-Agent
Via 1.1 varnish-v4
X-AREQUESTID 110x16852x1
X-ASEN SEN-L4572887
X-AUSERNAME anonymous
X-Varnish 32779
x-content-type-options nosniff
我的设置有什么问题吗?谢谢
最佳答案
你试过这个吗?
server {
listen 80;
server_name jira.lan.mysite.com;
...
location / {
...
proxy_http_version 1.1;
...
}
}
我遇到了类似的问题,问题是 nginx 使用 http/1.0 协议(protocol)作为反向代理,看起来这无法处理编码良好的 block 内容。
关于tomcat - Nginx + Varnish 4 对所有请求返回 200 空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34196637/