Varnish和Nginx重定向8080 - > 80

时间:2012-02-03 09:48:57

标签: performance redirect proxy nginx varnish

我正在尝试使用Varnish作为我在Ubuntu 10.10上的nginx安装的反向缓存代理,我在端口8080上设置了Varnish用于测试,nginx在端口80上正常运行。

我的nginx.conf:

user {user} {group};
worker_processes  16;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    multi_accept on;
}

http {
    include       /etc/nginx/mime.types;
    client_max_body_size 4M;
    client_body_buffer_size 128k;
    access_log  /var/log/nginx/access.log;

    gzip        on;
    gzip_proxied any;
    gzip_comp_level 2;
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml$

    sendfile        on;
    tcp_nopush     off;

    keepalive_timeout  30;
    tcp_nodelay        on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

我的网站的.conf文件:

server {
     listen  80;
     server_name www.mylesgray.com dev.mylesgray.com;

     access_log /var/www/mylesgray.com/logs/access.log;
     error_log /var/www/mylesgray.com/logs/error.log;

     location / {
        root /var/www/mylesgray.com/public;
        index index index.php;
        try_files $uri/ $uri index.php?q=$uri&$args;
        port_in_redirect off;
     }

     location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
        access_log        off;
        expires           30d;
        root /var/www/mylesgray.com/public;
     }

     location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(.*)$;
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/mylesgray.com/public/$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_intercept_errors        on;
        fastcgi_ignore_client_abort     off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }

    location ~ /.ht {
        deny  all;
    }

    location ~ /.git {
        deny  all;
    }

#    include /var/www/mylesgray.com/public/nginx.conf;
}

upstream backend {
    server 127.0.0.1:9000;
}

我的清漆conf:

backend default {
     .host = "localhost";
     .port = "80";
}

acl purge {
        "localhost";
}

sub vcl_recv {
        if (req.request == "PURGE") {
                if (!client.ip ~ purge) {
                        error 405 "Not allowed.";
                }
                return(lookup);
        }

        if (req.url ~ "^/$") {
               unset req.http.cookie;
         }
}

sub vcl_hit {
        if (req.request == "PURGE") {
                set obj.ttl = 0s;
                error 200 "Purged.";
        }
}

sub vcl_miss {
        if (req.request == "PURGE") {
                error 404 "Not in cache.";
        }

        if (!(req.url ~ "wp-(login|admin)")) {
                        unset req.http.cookie;
        }

        if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?$
                unset req.http.cookie;
                set req.url = regsub(req.url, "\?.$", "");
        }

        if (req.url ~ "^/$") {
                unset req.http.cookie;
        }
}

sub vcl_fetch {
        if (req.url ~ "^/$") {
                unset beresp.http.set-cookie;
        }

        if (!(req.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }
}

所以,当你在端口8080上转到我的网站时,http://www.mylesgray.com:8080它只是重定向到普通的旧版本:http://www.mylesgray.com如果我访问端口,它应该做的是(如它所示) 80我应该只获得nginx服务文件,如果我访问8080,我应该有nginx + varnish服务文件。

我这样做是为了测试清漆对普通nginx的性能优势。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

我最好的猜测是某种SEO插件正在考虑“哦,端口8080上的请求?不应该,我会善意并重写到端口80”:

$ curl -I "http://www.mylesgray.com:8080/"
HTTP/1.1 301 Moved Permanently
Server: nginx/0.7.65
Location: http://www.mylesgray.com/

您可能希望通过剥离端口号来规范化主机标头:

set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");

如果需要,请查看这些configuration examples for Varnish

的更多示例

答案 1 :(得分:1)

一个很好的技巧是让两个服务器(nginx和varnish)在同一个端口上,但在不同的IP中,比如说

nginx:localhost:80 清漆:1.2.3.4:80

检查一下:http://danielmiessler.com/blog/handling-redirects-with-varnish-and-nginx

答案 2 :(得分:0)

您需要进入WP常规设置并将yourdoma.in更改为yourdoma.in:8080。我遇到了同样的问题。

否则,WordPress只会永远在端口80上侦听。

这是explanation by davide73 on the wp forum

  

WordPress有一个错误;你不能将它用于非80 TCP端口。

     

那是因为他们使用$ _SERVER ['HTTP_HOST']代替   $ _SERVER [ 'HTTP_HOST']。 “:”。$ _ SERVER [ 'HTTP_PORT']