title: Nginx error 502 Bad Gateway caused because of many 'Set-Cookie'
body: <p>I am having problems to deploy an application using Varnish and Nginx while doing a <em>POST</em> request.</p>
  
  <p>The log below was got from the <em>varnishlog</em> command:</p>
  
  <p><a href="https://i.stack.imgur.com/QCSPC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QCSPC.png" alt="enter image description here"></a></p>
  
  <p>As you guys can see, looks like Nginx is trying to create the same cookie many times. I checked my application and we are not setting the same cookie value twice.</p>
  
  <p>I tried to apply the follow settings on Nginx, but did not change the result:</p>
  
  <pre><code>proxy_buffer_size 32k;
  proxy_buffers 4 32k;
  proxy_busy_buffers_size 32k;
  </code></pre>
  
  <p>And I am always getting:</p>
  
  <p><a href="https://i.stack.imgur.com/Bm1QH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bm1QH.png" alt="enter image description here"></a></p>
  
  <p>Varnish settings:</p>
  
  <pre><code>vcl 4.0;
  
  backend default {
      .host = "127.0.0.1";
      .port = "8080";
  }
  
  sub vcl_recv {
  
  if (req.method != "GET" &amp;&amp; req.method != "HEAD" &amp;&amp;
      req.method != "PUT" &amp;&amp; req.method != "POST" &amp;&amp;
      req.method != "TRACE" &amp;&amp; req.method != "OPTIONS" &amp;&amp;
      req.method != "DELETE") {
          return(pipe);
  } /* Non-RFC2616 or CONNECT which is weird. */
  
  if(req.method == "POST"){
           return (pass);
  }
  
  if (req.url ~ "61646D696E2D646576") {
          return (pass);
  }
  
  if (req.url ~ "payzen") {
          return (pass);
  }
  
  if ((req.url ~ "REFRESH")) {
          set req.url = regsub(req.url, "REFRESH", "");
          return (purge);
  }
  
  if (req.http.Cache-Control ~ "no-cache")
  {
          return(pass);
  }
  
  return(hash);
  
  }
  
  sub vcl_hash {
      hash_data(req.http.X-Forwarded-Proto);
  }
  
  sub vcl_backend_response {
  
  if(beresp.status == 500) {
          set beresp.uncacheable = true;
          set beresp.ttl = 120s;
          return (deliver);
  }
  
  if(beresp.status == 301 || beresp.status == 302) {
          set beresp.uncacheable = true;
          set beresp.ttl = 120s;
          return (deliver);
  }
  
  if (beresp.status &gt;= 200 &amp;&amp; beresp.status &lt; 400 &amp;&amp; (bereq.method
  == "PUT" || bereq.method == "POST" || bereq.method == "DELETE")) {
              ban("obj.http.X-Invalidated-By ~ " + bereq.http.X-Invalidates);
      }
  
  if (!beresp.http.Cache-Control ~ "no-cache") {
         unset beresp.http.set-cookie;
         unset beresp.http.cookie;
  }
  
  if (beresp.http.Cache-Control ~ "no-cache") {
          set beresp.uncacheable = true;
  }
  
  # IF IT'S SYMFONY DEBUG (DEV) MODE
  if (beresp.http.X-Debug-Token) {
          set beresp.uncacheable = true;
  }
  
      return(deliver);
  
  }
  
  sub vcl_deliver {
  
  if (obj.hits &gt; 0){
      set resp.http.X-Varnish-Cache = "HIT";
  } else {
      set resp.http.X-Varnish-Cache = "MISS";
  }
  
  }
  </code></pre>
  
  <p>Nginx config:</p>
  
  <pre><code>worker_processes  1;
  
  events {
      worker_connections  1024;
  }
  
  http {
      include       mime.types;
      default_type  application/octet-stream;
  
      gzip on;
      gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  
      keepalive_timeout  65;
  
      server_tokens off;
  
      client_max_body_size 50M;
  
      server {
          listen       localhost:8080;
          include /etc/nginx/redirects.nginx;
  
          location / {
  
          if ($http_x_forwarded_proto = "http") {
             return 301 https://$http_host$request_uri;
          }
  
  
           resolver 172.31.0.2 valid=10s;
           set $backend_upstream "http://my-server:80";
           proxy_pass $backend_upstream;
           proxy_set_header Host $http_host;
         }
      }
  
  }
  </code></pre>
  
  <p>Any idea where that problem comes from?</p>
  
  <p>Thanks! :)</p>