title: Nginx proxy_pass cookies
body: <p>What we have:</p>
  
  <ul>
  <li>A bunch of apache nginx.</li>
  <li>Forum Tinyboard.</li>
  </ul>
  
  <p>Through Apache authorization is proceeding normally, but with nginx as a function mkhash:</p>
  
  <pre><code>    function mkhash($username, $password, $salt = false) {
      global $config;
  
      if (! $salt) {
      // Create some sort of salt for the hash
       $salt = substr(base64_encode(sha1(rand(). time(), true). $config['cookies']['salt']), 0 , 15) ;
  
       $generated_salt = true;
      }
  
       // Generate hash (method is not important as long as it's strong)
       $hash = substr(base64_encode(md5($username . $config['cookies']['salt']. sha1($username . $password . $salt . ($config['mod']['lock_ip']? $_SERVER['REMOTE_ADDR']:''), true), true)), 0 , 20);
  
      if (isset($generated_salt))
       return array($hash, $salt);
      else
       return $hash;
      }
  </code></pre>
  
  <p>does not return the correct value and the authorization fails. 
  Verification is as follows:</p>
  
  <pre><code>    if ($cookie[ 1 ]! == mkhash($cookie[0], $user['password'], $cookie[2] ) {
       // Malformed cookies
       destroyCookies();
       mod_login();
       exit;
      }
  </code></pre>
  
  <p>In order to be successful login condition should not be executed.</p>
  
  <p>Example returned values ​​via nginx (login fails):</p>
  
  <ul>
  <li>cookie0: admin </li>
  <li>cookie1: Ib37H5U7hCi6Br9M09V </li>
  <li>cookie2: Nn2wUxlnirvgzkn</li>
  <li>mkhash: fN1jv3t9ccThde0Kp30h</li>
  </ul>
  
  <p>Example returned values ​​​​via apache (login success):</p>
  
  <ul>
  <li>cookie0: admin</li>
  <li>cookie1: SgaMoQ07upLoz9Q7Wdz6</li>
  <li>cookie2: Zp6BQ2b20Jsh 1R</li>
  <li>mkhash: SgaMoQ07upLoz9Q7Wdz6</li>
  </ul>
  
  <p>Apache hanging on port 82 (if handled properly on that port authentication is successful). Nginx itself takes only static files, dynamic content is taken from Apache. In what could be the reason?</p>
  
  <pre><code>    location / {
          proxy_pass http://backend;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_connect_timeout 120;
          proxy_send_timeout 120;
          proxy_read_timeout 180;
      }
  </code></pre>