Php5-cgi gives 502 with varnish but is ok without

fastcgiphp5varnish

problem summary:

php5-fastcgi works with nginx but doesn't work with nginx+varnish (502 Bad Gateway)

does anyone have a suggestion of what I could change to get varnish working ?

problem details:

This is my configuration:

(listening 80) nginx (requesting 8181) ==> (listening 8181) varnish ==> (listening 9090) php5-fastcgi

/etc/nginx/conf.d/mydomain.com

server {
  listen 80;
  server_name mydomain.com;
  index index.html index.htm index.php;
  keepalive_timeout  30;
  root /var/www/assets;
  location = /favicon.ico {
    log_not_found off;
    access_log off;
  }
  location ~\.php$ {
    root /var/www/api;
    add_header Access-Control-Allow-Origin *;
    fastcgi_pass 127.0.0.1:8181;
    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
  location ~ ^/(api|wp) {
    root /var/www/api;
    try_files $uri $uri/ /index.php?q=$uri&$args;
  }
}

/etc/default/varnish

START=yes
NFILES=131072
MEMLOCK=82000
DAEMON_OPTS="-a :8181 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256M"

/etc/varnish/default.vlc

backend default {
    .host = "127.0.0.1";
    .port = "9090";
}
sub vcl_recv {
    set req.backend = default;
    if (!(req.url ~ "wp-(login|admin)")) {
            unset req.http.cookie;
    }
    return(lookup);
}
sub vcl_miss {
    return(fetch);
}
sub vcl_hit {
    return(deliver);
}
sub vcl_fetch {
    if (!(req.url ~ "wp-(login|admin)")) {
            unset beresp.http.set-cookie;
    }
    set beresp.ttl = 24h;
    set beresp.http.X-Cacheable = "YES";
    unset beresp.http.Vary;
    return(deliver);
}
sub vcl_deliver {
    return(deliver);
}

/usr/bin/php5-fastcgi-p

#!/bin/bash
FASTCGI_USER=user
FASTCGI_GROUP=www-data
PORT=9090
ADDRESS=127.0.0.1
PIDFILE=/var/run/php5-fastcgi.pid
CHILDREN=4
PHP5=/usr/bin/php5-cgi

/usr/bin/spawn-fcgi -a $ADDRESS -p $PORT -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5

(on ubuntu 10.04 lucid lynx)

This setup doesn't currently work (502 Bad Gateway).

I also get a couple messages in varnishlog when I'm making my requests

 0 CLI          - Wr 200 19 PONG 1380029665 1.0
 11 SessionOpen  c 127.0.0.1 42489 :8181
 11 Debug        c herding
 11 SessionClose c no request
 11 StatSess     c 127.0.0.1 42489 0 1 0 0 0 0 0 0
 0 CLI          - Rd ping
 0 CLI          - Wr 200 19 PONG 1380029668 1.0
 0 CLI          - Rd ping

so I tried another config where nginx goes around varnish, and It works like a charms.

(listen on 80 )nginx ==> (listen on 9090) php5-fastcgi

Usually people suggest it's spawn-fcgi 's fault but I think it's rather in varnish's config

(I'm working on Ubuntu 10.04 so the option of using php-fpm is not available.)

here is what I get with netstat-nlp

tcp        0      0 0.0.0.0:8181            0.0.0.0:*               LISTEN      20886/varnishd  
tcp        0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      20989/php5-cgi  
tcp        0      0 127.0.0.1:6082          0.0.0.0:*               LISTEN      20885/varnishd  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1478/nginx.conf 
tcp6       0      0 :::8181                 :::*                    LISTEN      20886/varnishd  

Best Answer

Varnish understands HTTP but to get connected with your PHP fast CGI daemon it has to know the CGI standard. You cannot connect Varnish with your PHP daemon. There must be a web server Varnish can speak to and which connects to PHP. Maybe a set-up Varnish-Nginx-Php makes more sense.

If you are looking for a Php acceleration use caching in Php itself and combine it with RAM based key-value-stores like Memcached or Redis or other No-SQL databases.

Related Topic