在 Debian 8 上将 varnish 4.1 作为服务启动时,出现 Varnish 503 Backend Fetch 失败

在 Debian 8 上将 varnish 4.1 作为服务启动时,出现 Varnish 503 Backend Fetch 失败

我是 varnish 的新手。每当我尝试通过 varnish 访问我们的后端时,我都会收到以下错误:-

<!DOCTYPE html>
<html>
  <head>
    <title>503 Backend fetch failed</title>
  </head>
  <body>
    <h1>Error 503 Backend fetch failed</h1>
    <p>Backend fetch failed</p>
    <h3>Guru Meditation:</h3>
    <p>XID: 3</p>
    <hr>
    <p>Varnish cache server</p>
  </body>
</html>

我的varnish.vcl样子如下:-

vcl 4.0;
backend default {
    .host = "146.88.25.40";
    .port = "8080";
}

我也尝试设置.connect_timeout = 60sfirst_byte_timeout = 60s和,.between_bytes_timeout = 60s但没有运气。

我的/etc/default/varnish:-

START=yes
NFILES=131072
MEMLOCK=82000
DAEMON_OPTS="-a 0.0.0.0:6081 \
             -T localhost:6082 \
             -f /etc/varnish/varnish.vcl \
             -S /etc/varnish/secret \
             -s malloc,1G"

varnishlog内容:-

*   << BeReq    >> 3        
-   Begin          bereq 2 fetch
-   Timestamp      Start: 1451466805.152313 0.000000 0.000000
-   BereqMethod    GET
-   BereqURL       /rde_server/res/feea8d0b3d50/recomm/home/sid/jb55653bbd980baaf37484ef70bcfcef9b84d7c680ad1b487aed1fb21ac6779025?home_tsFilter=&cat_tsFilter=%28conc.item.brand+not+in+%2836%2C4187%2C4201%2C3721%2C4103%2C4101%2C4199%2C4075%29%29%28conc.item.param1+in+%28+a
-   BereqProtocol  HTTP/1.1
-   BereqHeader    User-Agent: curl/7.38.0
-   BereqHeader    Host: localhost:6081
-   BereqHeader    Accept: */*
-   BereqHeader    X-Forwarded-For: ::1
-   BereqHeader    Accept-Encoding: gzip
-   BereqHeader    X-Varnish: 3
-   VCL_call       BACKEND_FETCH
-   VCL_return     fetch
-   FetchError     no backend connection
-   Timestamp      Beresp: 1451466805.152502 0.000189 0.000189
-   Timestamp      Error: 1451466805.152511 0.000198 0.000009
-   BerespProtocol HTTP/1.1
-   BerespStatus   503
-   BerespReason   Service Unavailable
-   BerespReason   Backend fetch failed
-   BerespHeader   Date: Wed, 30 Dec 2015 09:13:25 GMT
-   BerespHeader   Server: Varnish
-   VCL_call       BACKEND_ERROR
-   BerespHeader   Content-Type: text/html; charset=utf-8
-   BerespHeader   Retry-After: 5
-   VCL_return     deliver
-   Storage        malloc Transient
-   ObjProtocol    HTTP/1.1
-   ObjStatus      503
-   ObjReason      Backend fetch failed
-   ObjHeader      Date: Wed, 30 Dec 2015 09:13:25 GMT
-   ObjHeader      Server: Varnish
-   ObjHeader      Content-Type: text/html; charset=utf-8
-   ObjHeader      Retry-After: 5
-   Length         278
-   BereqAcct      0 0 0 0 0 0
-   End           

*   << Request  >> 2        
-   Begin          req 1 rxreq
-   Timestamp      Start: 1451466805.152189 0.000000 0.000000
-   Timestamp      Req: 1451466805.152189 0.000000 0.000000
-   ReqStart       ::1 44193
-   ReqMethod      GET
-   ReqURL         /rde_server/res/feea8d0b3d50/recomm/home/sid/jb55653bbd980baaf37484ef70bcfcef9b84d7c680ad1b487aed1fb21ac6779025?home_tsFilter=&cat_tsFilter=%28conc.item.brand+not+in+%2836%2C4187%2C4201%2C3721%2C4103%2C4101%2C4199%2C4075%29%29%28conc.item.param1+in+%28+a
-   ReqProtocol    HTTP/1.1
-   ReqHeader      User-Agent: curl/7.38.0
-   ReqHeader      Host: localhost:6081
-   ReqHeader      Accept: */*
-   ReqHeader      X-Forwarded-For: ::1
-   VCL_call       RECV
-   VCL_return     hash
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 3 fetch
-   Timestamp      Fetch: 1451466805.152650 0.000461 0.000461
-   RespProtocol   HTTP/1.1
-   RespStatus     503
-   RespReason     Backend fetch failed
-   RespHeader     Date: Wed, 30 Dec 2015 09:13:25 GMT
-   RespHeader     Server: Varnish
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     Retry-After: 5
-   RespHeader     X-Varnish: 2
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish-v4
-   VCL_call       DELIVER
-   VCL_return     deliver
-   Timestamp      Process: 1451466805.152718 0.000529 0.000068
-   RespHeader     Content-Length: 278
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: keep-alive
-   Timestamp      Resp: 1451466805.152773 0.000584 0.000055
-   ReqAcct        386 0 386 235 278 513
-   End           

*   << Session  >> 1        
-   Begin          sess 0 HTTP/1
-   SessOpen       ::1 44193 :6081 ::1 6081 1451466805.152117 14
-   Link           req 2 rxreq
-   SessClose      REM_CLOSE 0.001
-   End            

如果我按如下方式启动 varnish,那么一切都会正常。我只有在通过以下方式启动 varnish 时才会遇到问题sudo service varnish start

sudo varnishd -n foo -f /etc/varnish/prudsys.vcl -s malloc,1G -T localhost:6082  -a 0.0.0.0:6081

backend如果我添加我的信息,一切都会正常工作/etc/varnish/default.vcl。似乎 varnish 没有从中选择更新的守护进程配置/etc/default/varnish

我的/etc/init.d/varnish样子如下:-

#! /bin/sh

### BEGIN INIT INFO
# Provides:          varnish
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start HTTP accelerator
# Description:       This script provides a server-side cache
#                    to be run in front of a httpd and should
#                    listen on port 80 on a properly configured
#                    system
### END INIT INFO

# Source function library
. /lib/lsb/init-functions

NAME=varnishd
DESC="HTTP accelerator"
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/varnishd
PIDFILE=/run/$NAME.pid

test -x $DAEMON || exit 0

# Include varnish defaults if available
if [ -f /etc/default/varnish ] ; then
        . /etc/default/varnish
fi

# Open files (usually 1024, which is way too small for varnish)
ulimit -n ${NFILES:-131072}

# Maxiumum locked memory size for shared memory log
ulimit -l ${MEMLOCK:-82000}

# If $DAEMON_OPTS is not set at all in /etc/default/varnish, use minimal useful
# defaults (Backend at localhost:8080, a common place to put a locally
# installed application server.)
DAEMON_OPTS=${DAEMON_OPTS:--b localhost}

# Ensure we have a PATH
export PATH="${PATH:+$PATH:}/usr/sbin:/usr/bin:/sbin:/bin"

start_varnishd() {
    log_daemon_msg "Starting $DESC" "$NAME"
    output=$(/bin/tempfile -s.varnish)
    if start-stop-daemon \
        --start --quiet --pidfile ${PIDFILE} --exec ${DAEMON} -- \
        -P ${PIDFILE} ${DAEMON_OPTS} > ${output} 2>&1; then
        log_end_msg 0
    else
        log_end_msg 1
        cat $output
        exit 1
    fi
    rm $output
}

disabled_varnishd() {
    log_daemon_msg "Not starting $DESC" "$NAME"
    log_progress_msg "disabled in /etc/default/varnish"
    log_end_msg 0
}

stop_varnishd() {
    log_daemon_msg "Stopping $DESC" "$NAME"
    if start-stop-daemon \
        --stop --quiet --pidfile $PIDFILE --retry 10 \
        --exec $DAEMON; then
        log_end_msg 0
    else
        log_end_msg 1
    fi

    if test -r $PIDFILE; then
        read -r PID < $PIDFILE
        if test ! -d /proc/$PID ; then
            # stale pidfile
            unset PID
            rm -f $PIDFILE
        fi
    fi
}

reload_varnishd() {
    log_daemon_msg "Reloading $DESC" "$NAME"
    if /usr/share/varnish/reload-vcl -q; then
        log_end_msg 0
    else
        log_end_msg 1
    fi
}

status_varnishd() {
    start-stop-daemon \
        --status --quiet --pidfile $PIDFILE \
        --exec $DAEMON
    exit $?
}

configtest() {
    $DAEMON ${DAEMON_OPTS} -C -n /tmp 2>/dev/null
    if [ "$?" != 0 ]; then
        $DAEMON ${DAEMON_OPTS} -C -n /tmp
        return $?
    fi
}


case "$1" in
    start)
        case "${START:-}" in
            [Yy]es|[Yy]|1|[Tt]|[Tt]rue)
                start_varnishd
                ;;
            *)
                disabled_varnishd
                ;;
        esac
        ;;
    stop)
        stop_varnishd
        ;;
    reload)
        reload_varnishd
        ;;
    status)
        status_varnishd
        ;;
    restart|force-reload)
    if status_of_proc -p "${PIDFILE}" "${DAEMON}" "${NAME}" 1>/dev/null; then
        if ! configtest; then
        log_failure_msg "Syntax check failed, not restarting"
        exit 1
        fi
    fi
        $0 stop
        $0 start
        ;;
    configtest)
        configtest && log_success_msg "Syntax ok"
        ;;
    *)
        log_success_msg "Usage: $0 {start|stop|restart|reload|force-reload|configtest}"
        exit 1
        ;;
esac

有人能告诉我出了什么问题吗?我在 Debian 8 上使用 varnish 4.1。

答案1

这是 Debian 8 和 Varnish 的问题。我按照以下步骤解决了这个问题。

覆盖 varnish 的 systemd 初始化脚本

# cp /lib/systemd/system/varnish.service /etc/systemd/system/
# vim /etc/systemd/system/varnish.service

直接编辑/etc/systemd/system/varnish.service,如下所示:-

[Unit]
Description=Varnish Cache, a high-performance HTTP accelerator

[Service]
Type=forking
LimitNOFILE=131072
LimitMEMLOCK=82000
ExecStartPre=/usr/sbin/varnishd -C -f /etc/varnish/prudsys.vcl
ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/prudsys.vcl -S /etc/varnish/secret -s malloc,1G
ExecReload=/usr/share/varnish/reload-vcl

[Install]
WantedBy=multi-user.target

重新启动服务:-

systemctl daemon-reload
systemctl restart varnish.service

请记住在所有三个地方进行更改:-

  1. /etc/default/varnish
  2. /lib/systemd/system/varnish.service
  3. /etc/systemd/system/varnish.service

相关内容