免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4886 | 回复: 4
打印 上一主题 下一主题

Varnish反向代理+NginX出现奇怪现象 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-02 16:42 |只看该作者 |倒序浏览
Varnish版本2.0.4  Nginx版本0.7.30
Varnish和NginX装在同一个虚拟机ip eth0:192.168.2.215 eth1:192.168.0.105,本机192.168.2.60 Varnish监听80端口,Nginx监听8080端口
hosts文件中已经设置192.168.2.215   cache.example.com
Nginx中listen       192.168.2.215:8080;
           server_name  cache.example.com;
在IE中输入http://cache.example.com:8080中可正常访问
然后加入Varnish,只对默认的配置文件做了简单的修改,启动Vanish,并对日志做tail -f输出观察
问题出来了:在IE中输入http://cache.example.com需要刷新一次才能进入首页,点击链接也需要点击两次才能正常进入页面,查看Varnish日志有如下输出
127.0.0.1 - - [02/Apr/2009:08:28:36 +0000] "GET http://cache.example.com/RELEASE-NOTES-U1-bn.html HTTP/1.1" 200 - "http://cache.example.com/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; CIBA)"
192.168.2.60 - - [02/Apr/2009:16:28:36 +0800] "GET http://cache.example.com/RELEASE-NOTES-U1-bn.html HTTP/1.1" 200 36209 "http://cache.example.com/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; CIBA)"
查看Nginx日志有如下输出:
192.168.2.215 - - [02/Apr/2009:16:28:36 +0800] "GET /RELEASE-NOTES-U1-bn.html HTTP/1.1" 200 36222 "http://cache.example.com/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; CIBA)" 192.168.2.60

不知如何解决
附上Nginx的配置文件
user  www www;
worker_processes  8;
error_log   logs/error.log info;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        /opt/nginx/nginx.pid;
worker_rlimit_nofile 51200;
events {
    use epoll;
    worker_connections 51200;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '"$status" $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  /var/log/nginx/access.log  main;
   
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;

    sendfile        on;
    tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  60;
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    server {
        listen       192.168.2.215:8080;
        server_name  cache.example.com;
        log_format example '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
        access_log /var/log/nginx/cache1.log example;
        location / {
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
            root   /opt/nginx/html;
        }
    }
}

Varnish的配置文件
backend cache1 {
  .host = "cache.example.com";
  .port = "8080";
  }
sub vcl_recv {
      remove req.http.X-Forwarded-For;
      set req.http.X-Forwarded-For= client.ip;
      set req.grace=30s;
     if (req.request != "GET" &&
         req.request != "HEAD" &&
         req.request != "PUT" &&
         req.request != "POST" &&
         req.request != "TRACE" &&
         req.request != "OPTIONS" &&
         req.request != "DELETE") {
         return (pipe);
     }
     if (req.request != "GET" && req.request != "HEAD") {
        return (pass);
     }
     if (req.http.Authorization || req.http.Cookie) {
         return (pass);
     }
     if (req.http.host ~ "^cache.example.com") {
               set req.backend = cache1;
               if (req.request != "GET" && req.request != "HEAD") {
                      return(pipe);
               }
               else {
                      return(lookup);
               }
       }
     else {
               error 404 "Example.com's Cache Server";
               return(lookup);
       }
     return (lookup);
     
}
sub vcl_pipe {
    return (pipe);
}
sub vcl_pass {
    return (pass);
}
sub vcl_hash {
    set req.hash += req.url;
    if (req.http.host) {
        set req.hash += req.http.host;
    } else {
        set req.hash += server.ip;
    }
    return (hash);
}
sub vcl_hit {
    if (!obj.cacheable) {
        return (pass);
    }
    return (deliver);
}
sub vcl_miss {
    return (fetch);
}
sub vcl_fetch {
    if (!obj.cacheable) {
        return (pass);
    }
    if (obj.http.Set-Cookie) {
        return (pass);
    }
    set obj.prefetch =  -30s;
    return (deliver);
}
sub vcl_deliver {
    return (deliver);
}
sub vcl_discard {
    return (discard);
}
sub vcl_prefetch {
    return (fetch);
}
sub vcl_timeout {
        return (discard);
}
sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    synthetic {"
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title>"} obj.status " " obj.response {"</title>
      </head>
      <body>
        <h1>Error "} obj.status " " obj.response {"</h1>
        <p>"} obj.response {"</p>
        <h3>Guru Meditation:</h3>
        <p>XID: "} req.xid {"</p>
        <address>
           <a href="http://www.varnish-cache.org/">Varnish</a>
        </address>
      </body>
    </html>
    "};
    return (deliver);
}

论坛徽章:
0
2 [报告]
发表于 2009-04-02 17:25 |只看该作者
召唤高手来解决一下,明天用低一些的版本再试试看

论坛徽章:
0
3 [报告]
发表于 2009-04-02 23:27 |只看该作者

回复 #1 CyaLiven 的帖子

更换本版尝试一下

论坛徽章:
0
4 [报告]
发表于 2009-06-08 17:53 |只看该作者

不用更换版本 我用的就是2.0.4 一直就没有出过问题

你有两个lookup  
你要去掉一个
if (req.request != "GET" && req.request != "HEAD") {
                      return(pipe);
               }
               else {
                      return(lookup);
               }
       }
这个里面的
else {
                      return(lookup);
               }
删掉

论坛徽章:
0
5 [报告]
发表于 2009-06-08 17:56 |只看该作者

你最好整理成下面这样会比较清楚

if(req.http.host ~"www.xxx.com.cn$") {
                set req.backend = xxx;
        }else{
                error 200 "Domain is not exist";
        }

        if (req.request != "GET" && req.request != "HEAD")
        {
                return(pipe);
        }
        elseif (req.url ~ "\.(php|cgi)($|\?)")
        {
                return(pass);
        }
        elseif (req.http.Authenticate || req.http.Authorization) {
                return(pass);
        }
        return(lookup);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP