3

I want to put nginx in front of Varnish, which at the same time will go back to nginx to serve some PHP (Drupal).

But I am only getting blank pages (from Varnish) with 200 response, but length 0. Only the first access after restarting varnish works, but then blank pages all the time.

This is the configuration for Nginx:

server{
  listen 80; 
  server_name myserver

  access_log  /var/log/nginx/ssl_access.log main;
  error_log   /var/log/nginx/ssl_error.log warn;

  location / { 
    proxy_pass http://127.0.0.1:6081;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
  } 

Then the varnish configuration is just only the default, trying to debug:

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

And then again the nginx config for 8080 is:

server{
  listen       8080; 
  access_log  /var/log/nginx/fromvarnish.log main;
  error_log   /var/log/nginx/fromvarnisherror.log warn;

  server_name myserver

  location / { 
    try_files $uri /index.php?$query_string; 
  }
  location ~ '\.php$|^/update.php' {
     include fastcgi_params;
     fastcgi_pass 127.0.0.1:9001;
     fastcgi_index index.php;
  }
}

Visiting my webpage only returns empty html but 200 response. No PHP error or anything. Access logs on Nginx are just access, no errors.

If I access directly from Varnish (either I access via port mywbpage.com:6081) or if I set it to port 80 it works.

If I set the php executing backend to Apache+php-fpm (instead of Nginx: nginx->varnish->apache) it also works properly (I have the same issue though if the php executer is HHVM but that might be a different problem).

EDIT: sorry that was wrong, if I use apache it works only if I uncheck drupal's default cache (cache pages for unauthenticated users). With nginx this doesn't matter, it never works regardless of this checkbox.

Any of you know something that can guide me?

  • 1
    This is the same thing I'm trying to setup and it's working except I'm getting blank responses on POST requests only. Have you figured it out yet? – bradt Jan 1 '16 at 22:01
  • Have you tried bypassing Varnish to see if it works? i.e. update proxy_pass http://127.0.0.1:6081; in your frontend Nginx conf to proxy_pass http://127.0.0.1:8080; – bradt Jan 3 '16 at 23:21
  • didn't find out why yet. Bypassing nginx and going directly to varnish (varnish ->nginx) or bypassing varnish like you say (nginx->nginx) works alright... – Cesc Jan 4 '16 at 0:42
  • @bradt But I think I've found an error in varnishlog ("junk after gzip data") which was hard to see coz it only shows once and then I guess varnish caches the blank page and it doesn't show the error anymore – Cesc Jan 4 '16 at 1:03
  • @bradt this varnish-cache.org/trac/ticket/234 might give you info for your case, looks a POST request can't be pass through varnish you have to pipe it directly – Cesc Jan 4 '16 at 6:09
3

So I hope somebody gives an answer, but in the meantime I made it work so I'll explain how:

nginx config in the server part port 80. The one that the user will access first, add this:

proxy_http_version 1.1;

Taking a look at the varnish logs, one of the little differences between a request coming from nginx -blank page-:

ReqProtocol HTTP/1.0

and one coming directly from the webbrowser visiting the varnish port (ex: mypage:6081) -working properly- was that:

ReqProtocol HTTP/1.1

So I forced nginx to use 1.1 and now it works.

  • 1
    Woohoo, that fixed my issue as well! I'm not exactly sure why, but I believe it has something to do with Transfer-Encoding: chunked that I read about. It's not supported by HTTP/1.0. – bradt Jan 4 '16 at 12:37
  • I ran into this issue running nginx -> varnish -> apache + mod_php in building a developer setup inside a vagrant machine, and @bradt is spot it… it's the transfer-encoding support that causes it. Still not sure why the proxy_pass defaults to HTTP/1.0. Before I found the setting, some stuff would work, and other pages would have 4 random chars (hexadecimal) printed above the body of the page! – davidalger Feb 11 '16 at 1:00

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.