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


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 *;
    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;


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


backend default {
    .host = "";
    .port = "9090";
sub vcl_recv {
    set req.backend = default;
    if (!(req.url ~ "wp-(login|admin)")) {
            unset req.http.cookie;
sub vcl_miss {
sub vcl_hit {
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;
sub vcl_deliver {



/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 42489 :8181
 11 Debug        c herding
 11 SessionClose c no request
 11 StatSess     c 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  *               LISTEN      20886/varnishd  
tcp        0      0*               LISTEN      20989/php5-cgi  
tcp        0      0*               LISTEN      20885/varnishd  
tcp        0      0    *               LISTEN      1478/nginx.conf 
tcp6       0      0 :::8181                 :::*                    LISTEN      20886/varnishd  

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.

  • Ok thanks. The things is that nginx was previously installed to server as reverse proxy (and serving static files) for other webapps (ruby based). Migrating our reverse proxy rules fro nginx to varnish sounds painful... Would it be foolish to add more rules in nginx for the following config : nginx - varnish - nginx(again) - php ? – Raphael Sep 25 '13 at 8:28
  • As you already know Nginx is able to act as a reverse proxy as well. I would recommend a construct Nginx (proxying) -> Nginx -> PHP to reduce the complexity. However in terms of reverse proxy solutions I prefer Varnish. – Jens Bradler Sep 25 '13 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.