title: How can I fix HTTP ERROR 500 on nginx + varnish server
body: <p>I am running 2 wordpress websites on digitalocean droplet. One website is working absolutely fine. </p>
  
  <p>But on another website When I am logged in to wordpress admin then it works fine. But when I am not logged in, then <code>HTTP ERROR 500</code> occurs. </p>
  
  <p>both websites nginx and varnish configuration is same. </p>
  
  <p>Here is NGINX virtual host configuration:</p>
  
  <pre><code>server {
  
      listen 8080;
  
      listen 443 ssl;
  
      root /home/user/example.com/public_html;
      index index.html index.htm index.php;
  
      server_name example.com www.example.com;
      include hhvm.conf;
  
  
      ssl_certificate /etc/nginx/ssl/nginx.crt;
          ssl_certificate_key /etc/nginx/ssl/nginx.key;
  
      access_log        off;  
  
      location / {
          #try_files $uri $uri/ =404;
          try_files $uri $uri/ /index.php?q=$uri&amp;$args;
  
  
      }
  
      location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|woff|eot|ttf|svg|mp4|webm)$ {
              access_log        off;
              log_not_found     off;
          expires 1M;
          add_header Pragma public;
          add_header Cache-Control public;
          add_header Vary Accept-Encoding;
          }
  
  }
  </code></pre>
  
  <p>Here is <code>/etc/varnish/default.vcl</code></p>
  
  <pre><code># Redirect to Nginx Backend if not in cache
  backend default {    
      .host = "127.0.0.1";    
      .port = "8080";
  }
  
  import std;
  
  include "lib/xforward.vcl";
  include "lib/cloudflare.vcl";
  include "lib/purge.vcl";
  include "lib/bigfiles.vcl";        # Varnish 3.0.3+
  #include "lib/bigfiles_pipe.vcl";  # Varnish 3.0.2
  include "lib/static.vcl";
  
  acl cloudflare {
      # set this ip to your Railgun IP (if applicable)
      # "1.2.3.4";
  }
  acl purge {
      "127.0.0.1";
  }
  
  # vcl_recv is called whenever a request is received 
  sub vcl_recv {
      if (req.restarts == 0) {        
          if (req.http.x-forwarded-for) {            
              set req.http.X-Forwarded-For =  req.http.X-Forwarded-For + ", " + client.ip;      
          } else {            
              set req.http.X-Forwarded-For = client.ip;        
          }    
      }     
      if (req.http.X-Real-IP) {         
          set req.http.X-Forwarded-For = req.http.X-Real-IP;    
      } else {        
          set req.http.X-Forwarded-For = client.ip;    
      } 
      # Serve objects up to 2 minutes past their expiry if the backend
      # is slow to respond.
          set req.grace = 120s;
  
          set req.backend = default;
  
          if (!req.http.X-Forwarded-Proto) {        
              set req.http.X-Forwarded-Proto = "http";        
              set req.http.X-Forwarded-Port = "80";        
              set req.http.X-Forwarded-Host = req.http.host;    
          } 
  
      # This uses the ACL action called "purge". Basically if a request to
      # PURGE the cache comes from anywhere other than localhost, ignore it.
          if (req.request == "PURGE") 
          {if (!client.ip ~ purge)
              {error 405 "Not allowed.";}
              return(lookup);}
  
      # Pass any requests that Varnish does not understand straight to the backend.
          if (req.request != "GET" &amp;&amp; req.request != "HEAD" &amp;&amp;
                  req.request != "PUT" &amp;&amp; req.request != "POST" &amp;&amp;
                  req.request != "TRACE" &amp;&amp; req.request != "OPTIONS" &amp;&amp;
                  req.request != "DELETE") 
          {return(pipe);}     /* Non-RFC2616 or CONNECT which is weird. */
  
      # Pass anything other than GET and HEAD directly.
          if (req.request != "GET" &amp;&amp; req.request != "HEAD") {
              return(pass);
          }      /* We only deal with GET and HEAD by default */
  
      # Pass requests from logged-in users directly.
          if (req.http.Authorization || req.http.Cookie) {
              return(pass);
          }      /* Not cacheable by default */
  
      # Pass any requests with the "If-None-Match" header directly.
          if (req.http.If-None-Match) {
              return(pass);
          }
  
      # Force lookup if the request is a no-cache request from the client.
          if (req.http.Cache-Control ~ "no-cache") {
              ban_url(req.url);
          }
          return(lookup);
  }
  
  sub vcl_pipe {
  # This is otherwise not necessary if you do not do any request rewriting.
      set req.http.connection = "close";
  }
  
  # Called if the cache has a copy of the page.
  sub vcl_hit {
      if (req.request == "PURGE") {
          ban_url(req.url);
          error 200 "Purged";
      }
  
      if (!obj.ttl &gt; 0s) {
          return(pass);
      }
  }
  
  # Called if the cache does not have a copy of the page.
  sub vcl_miss {
      if (req.request == "PURGE") {
          error 200 "Not in cache";
      }
  }
  
  # Called after a document has been successfully retrieved from the backend.
  sub vcl_fetch {
      # make sure grace is at least 2 minutes
      if (beresp.grace &lt; 2m) {
          set beresp.grace = 2m;
      }
  
      # catch obvious reasons we can't cache
      if (beresp.http.Set-Cookie) {
          set beresp.ttl = 0s;
      }
  
      # Varnish determined the object was not cacheable
      if (beresp.ttl &lt;= 0s) {
          set beresp.http.X-Cacheable = "NO:Not Cacheable";
          return(hit_for_pass);
  
      # You don't wish to cache content for logged in users
      } else if (req.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
          set beresp.http.X-Cacheable = "NO:Got Session";
          return(hit_for_pass);
  
      # You are respecting the Cache-Control=private header from the backend
      } else if (beresp.http.Cache-Control ~ "private") {
          set beresp.http.X-Cacheable = "NO:Cache-Control=private";
          return(hit_for_pass);
  
      # You are extending the lifetime of the object artificially
      } else if (beresp.ttl &lt; 300s) {
          set beresp.ttl   = 300s;
          set beresp.grace = 300s;
          set beresp.http.X-Cacheable = "YES:Forced";
  
      # Varnish determined the object was cacheable
      } else {
          set beresp.http.X-Cacheable = "YES";
      }
  
      # Avoid caching error responses
      if (beresp.status == 404 || beresp.status &gt;= 500) {
          set beresp.ttl   = 0s;
          set beresp.grace = 15s;
      }
  
  
  
  
      # Deliver the content
      return(deliver);
  }
  
  sub vcl_pass {
      return (pass);
  }
  
  sub vcl_hash {
      hash_data(req.url);
      if (req.http.host) {
          hash_data(req.http.host);
      } else {
          hash_data(server.ip);
      }
      return (hash);
  }
  
  sub vcl_deliver {
      # Debug    
      remove resp.http.Via;    
      remove resp.http.X-Varnish;    
  
      # Add a header to indicate a cache HIT/MISS    
      #if (obj.hits &gt; 0) {        
      #   set resp.http.X-Cache = "HIT";        
      #   set resp.http.X-Cache-Hits = obj.hits;        
      #   set resp.http.X-Age = resp.http.Age;        
      #   remove resp.http.Age;    
      #} else {        
      #   set resp.http.X-Cache = "MISS";    
      #}    
  
      # Remove some headers: PHP version
      unset resp.http.X-Powered-By;
  
      # Remove some headers: Apache version &amp; OS
      unset resp.http.Server;
  
      # Remove some heanders: Varnish
      unset resp.http.Via;
      unset resp.http.X-Varnish;
  
  
      return (deliver);
  }
  
  sub vcl_error {
      set obj.http.Content-Type = "text/html; charset=utf-8";
      set obj.http.Retry-After = "5";
      synthetic {"
          &lt;?xml version="1.0" encoding="utf-8"?&gt;
              &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
              &lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;"} + obj.status + " " + obj.response + {"&lt;/title&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;h1&gt;Error "} + obj.status + " " + obj.response + {"&lt;/h1&gt;
              &lt;p&gt;"} + obj.response + {"&lt;/p&gt;
              &lt;h3&gt;Guru Meditation:&lt;/h3&gt;
              &lt;p&gt;XID: "} + req.xid + {"&lt;/p&gt;
              &lt;hr&gt;
              &lt;p&gt;Varnish cache server&lt;/p&gt;
              &lt;/body&gt;
              &lt;/html&gt;
              "};
      return (deliver);
  }
  
  sub vcl_init {
      return (ok);
  }
  
  sub vcl_fini {
      return (ok);
  }
  </code></pre>
  
  <p>I did not understand what is causing 500 error. Please guide me how can I fix this issue. 
  Thanks</p>