清漆“ FetchError无后端连接”错误


11

Varnishlog:

    0 CLI          - Rd ping
    0 CLI          - Wr 200 19 PONG 1340829925 1.0
   12 SessionOpen  c 79.124.74.11 3063 :80
   12 SessionClose c EOF
   12 StatSess     c 79.124.74.11 3063 0 1 0 0 0 0 0 0
    0 CLI          - Rd ping
    0 CLI          - Wr 200 19 PONG 1340829928 1.0
    0 CLI          - Rd ping
    0 CLI          - Wr 200 19 PONG 1340829931 1.0
   12 SessionOpen  c 108.62.115.226 46211 :80
   12 ReqStart     c 108.62.115.226 46211 467185881
   12 RxRequest    c GET
   12 RxURL        c /
   12 RxProtocol   c HTTP/1.0
   12 RxHeader     c User-Agent: Pingdom.com_bot_version_1.4_(http://www.pingdom.com/)
   12 RxHeader     c Host: www.mysite.com
   12 VCL_call     c recv lookup
   12 VCL_call     c hash
   12 Hash         c /
   12 Hash         c www.mysite.com
   12 VCL_return   c hash
   12 VCL_call     c miss fetch
   12 FetchError   c no backend connection
   12 VCL_call     c error deliver
   12 VCL_call     c deliver deliver
   12 TxProtocol   c HTTP/1.1
   12 TxStatus     c 503
   12 TxResponse   c Service Unavailable
   12 TxHeader     c Server: Varnish
   12 TxHeader     c Content-Type: text/html; charset=utf-8
   12 TxHeader     c Retry-After: 5
   12 TxHeader     c Content-Length: 418
   12 TxHeader     c Accept-Ranges: bytes
   12 TxHeader     c Date: Wed, 27 Jun 2012 20:45:31 GMT
   12 TxHeader     c X-Varnish: 467185881
   12 TxHeader     c Age: 1
   12 TxHeader     c Via: 1.1 varnish
   12 TxHeader     c Connection: close
   12 Length       c 418
   12 ReqEnd       c 467185881 1340829931.192433119 1340829931.891024113 0.000051022 0.698516846 0.000074035
   12 SessionClose c error
   12 StatSess     c 108.62.115.226 46211 1 1 1 0 0 0 256 418
    0 CLI          - Rd ping
    0 CLI          - Wr 200 19 PONG 1340829934 1.0
    0 CLI          - Rd ping
    0 CLI          - Wr 200 19 PONG 1340829937 1.0

netstat -tlnp
 Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      3086/nginx      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1915/varnishd   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1279/sshd       
tcp        0      0 127.0.0.2:25            0.0.0.0:*               LISTEN      3195/sendmail: MTA:
tcp        0      0 127.0.0.2:6082          0.0.0.0:*               LISTEN      1914/varnishd   
tcp        0      0 127.0.0.2:9000          0.0.0.0:*               LISTEN      1317/php-fpm.conf)
tcp        0      0 127.0.0.2:3306          0.0.0.0:*               LISTEN      1192/mysqld     
tcp        0      0 127.0.0.2:587           0.0.0.0:*               LISTEN      3195/sendmail: MTA:
tcp        0      0 127.0.0.2:11211         0.0.0.0:*               LISTEN      3072/memcached  
tcp6       0      0 :::8080                 :::*                    LISTEN      3086/nginx      
tcp6       0      0 :::80                   :::*                    LISTEN      1915/varnishd   
tcp6       0      0 :::22                   :::*                    LISTEN      1279/sshd  

/ etc / nginx / site-enabled / default

server {
    listen   8080; ## listen for ipv4; this line is default and implied
    listen   [::]:8080 default ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www;
    index index.html index.htm index.php;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to index.html
        try_files $uri $uri/ /index.html;
    }

    location /doc {
        root /usr/share;
        autoindex on;
        allow 127.0.0.2;
        deny all;
    }

    location /images {
        root /usr/share;
        autoindex off;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #   root /usr/share/nginx/www;
    #}

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #   proxy_pass http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass 127.0.0.2:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

/etc/nginx/sites-enabled/www.mysite.com.vhost

server {
       listen 8080;
       server_name www.mysite.com mysite.com.net;
       root /var/www/www.mysite.com/web;
       if ($http_host != "www.mysite.com") {
                 rewrite ^ http://www.mysite.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 127.0.0.2:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       }

include /var/www/www.mysite.com/web/nginx.conf;
       location ~ /nginx.conf {
                deny all;
                access_log off;
                log_not_found off;
       }

}

/etc/varnish/default.vcl

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
# 
# Default backend definition.  Set this to point to your content
# server.
# 
backend default {
    .host = "127.0.0.2";
    .port = "8080";
    # .connect_timeout = 600s;
    #.first_byte_timeout = 600s;
    # .between_bytes_timeout = 600s;
    # .max_connections = 800;

注意:在default.vcl中取消注释最后四个选项没有影响。


猫/ 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.
#

# Should we start varnishd at boot?  Set to "yes" to enable.
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 127.0.0.2:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G"

如果您需要其他任何信息,请告诉我。我对所有问题一无所知。


curl header给我这个:

curl -v -I -H "Testing: Test header so you see this works" http://www.mysite.com:8080
* About to connect() to www.mysite.com port 8080 (#0)
*   Trying 176.31.158.78... connected
* Connected to www.mysite.com (176.31.158.78) port 8080 (#0)
> HEAD / HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: www.mysite.com:8080
> Accept: */*
> Testing: Test header so you see this works
> 
< HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently
< Server: nginx/1.0.5
Server: nginx/1.0.5
< Date: Thu, 28 Jun 2012 11:01:23 GMT
Date: Thu, 28 Jun 2012 11:01:23 GMT
< Content-Type: text/html
Content-Type: text/html
< Content-Length: 184
Content-Length: 184
< Connection: keep-alive
Connection: keep-alive
< Location: http://www.mysite.com/
Location: http://www.mysite.com/

< 
* Connection #0 to host www.mysite.com left intact
* Closing connection #0

2
尝试启用后端轮询。varnish-cache.org/trac/wiki/BackendPolling 如果您使用curl或wget直接在端口8080上命中了内容,nginx是否会返回您的内容?
Pax 2012年

上次看到此问题时,问题出在SELinux。作为测试,请尝试将其关闭:echo 0 >/selinux/enforce。如果确认是问题所在,则可以使用设置例外audit2allow
cyberx86 2012年

nginx error_log上有东西吗?也许是503错误?并在您的mysite.com.vhost中尝试删除第5、6和7行,然后再次尝试访问。
Skamasle

它可以使用malloc代替文件吗?您可能还需要在vcl文件中使用}。
2013年

即使您的帖子很旧,我也发布了答案,因为我遇到了同样的错误,并且搜索了一段时间,然后才意识到这是由非www到www重定向引起的
Kojo

Answers:


1

当尝试在本地使用不同的后端测试Varnish时,我遇到了类似的问题。使用127.0.0.1:8080效果很好,但是将端口更改8081为503,即使该后端在Varnish之外对我来说也非常适用。

该问题是由SELinux不允许连接引起的。我发现这是通过拖尾审核日志并从Varnish引起503:

$ sudo tail -f /var/log/audit/audit.log type=AVC msg=audit(1539253067.438:1379): avc: denied { name_connect } for pid=10154 comm="varnishd" dest=8081 scontext=system_u:system_r:varnishd_t:s0 tcontext=system_u:object_r:transproxy_port_t:s0 tclass=tcp_socket

要查看Varnish SELinux允许的端口,可以使用以下命令:

$ sudo semanage port -l | grep http_cache_port_t http_cache_port_t tcp 8080, 8118, 8123, 10001-10010

要解决此问题,您可以使用其他端口,例如,8118或要求SELinux允许8081从Varnish 进行连接。

添加端口的命令是- -a标志是添加端口的标志:

semanage port -a -t http_cache_port_t -p tcp 8081

如果您收到一条消息,告诉您该端口已经定义,那么您需要修改而不是添加端口:

ValueError: Port tcp/8081 already defined

该命令将-a标志交换为-m-'modify':

semanage port -m -t http_cache_port_t -p tcp 8081


0

您应该尝试使用清漆用户登录,并使用curl或wget测试到Nginx后端(127.0.0.2:8080)的连接,以确保其按预期工作。

正如cyberx86在评论中所说,不允许在服务之间建立连接的SELinux(或其他任何安全模块,例如apparmor,grsec等)可能是个问题,您还应该检查审核日志以确保它未在服务器中发生。

如果您的后端工作不正常(执行Nginx时出错),您将遇到此类问题,请尝试直接访问后端并检查日志以确保一切都按预期运行(Pax在评论中也告诉您) 。


0

Varnish正在接收数据,并且应该监听端口80。来自Varnish的数据是Nginx的代理,后者监听8080端口。

Nginx配置

server {
    listen 800;
    server_name www.example.com
}

清漆配置 -sudo nano / etc / default / varnish

DAEMON_OPTS="-a :80 \               #note on port 80 here
         -T localhost:6082 \
         -f /etc/varnish/default.vcl \
         -S /etc/varnish/secret \
         -s malloc,256m"

然后这是端口8080(Nginx)的Varnish代理配置-sudo nano /etc/varnish/default.vcl

backend default {
.host = "127.0.0.1";
.port = "8080";
}

0

我在多个站点配置中遇到了相同的错误,非www到www域重定向是问题所在。这就是我配置它的方式(相关部分)。

在您的中.vclsub vcl_recv

vcl 4.0;

sub vcl_recv {
    #THIS IS THE IMPORTANT POINT
    #redirect non www to www domains ip no subdomain defined
    if (req.http.host ~ "^([-0-9a-zA-Z]+)\.([a-zA-Z]+)$") {
       return (synth (750, ""));
    }

    # Normalize the header, remove the port
    set req.http.host = regsub(req.http.host, ":[0-9]+", "");

    #OFTEN SHIPS WITH THIS OTHER ONE, COMMENT IT OTHERWISE IT WILL FAIL
    #set req.http.Host = regsub(req.http.Host, "^www\.", "");

    # For the sake of example, a couple of custom backends, 
    # note www is specified in the req.http.host
    if (req.http.host ~ "foo.example.tld") {
         set req.backend_hint = example;
    }
     elseif (req.http.host ~ "www.other.com") {
         set req.backend_hint = other;
    }

然后,在其中sub vcl_synth添加:

sub vcl_synth {
    if (resp.status == 750) {
        #redirect non www to www
        set resp.status = 301;
        set resp.http.Location = "http://www." + req.http.host + req.url;
        return(deliver);
    }

这样重定向将由Varnish处理,然后再请求后端,避免循环或没有后端连接。

最后,在您的网站虚拟主机中,不再需要两个服务器块,您可以注释或删除非www块。

#server {
#        listen 1.2.3.4:8080;
#        server_name example.tld;
#        return 301 http://www.example.tld;
#}

server {
        listen 1.2.3.4:8080;
        server_name www.example.tld;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.