Nginx proxy_pass cookies


Nginx proxy_pass cookies

我们有什么:

  • 一堆阿帕奇·
  • 论坛小板。

通过 Apache 授权正常进行,但使用 nginx 作为函数 mkhash:

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

不返回正确的值,授权失败。验证如下:

    if ($cookie[ 1 ]! == mkhash($cookie[0], $user['password'], $cookie[2] ) {
     // Malformed cookies
     destroyCookies();
     mod_login();
     exit;
    }

为了成功登录,不应执行登录条件。

通过nginx返回的值示例(登录失败):

  • 饼干0: 管理员
  • 饼干1: Ib37H5U7hCi6Br9M09V
  • cookie2: Nn2wUxlnirvgzkn
  • mkhash: fN1jv3t9ccThde0Kp30h

通过 apache 返回的值示例(登录成功):

  • 饼干0: 管理员
  • 饼干1: SgaMoQ07upLoz9Q7Wdz6
  • 饼干2: Zp6BQ2b20Jsh 1R
  • mkhash: SgaMoQ07upLoz9Q7Wdz6

Apache 挂在端口 82 上(如果在该端口上正确处理,身份验证成功)。Nginx本身只接受静态文件,动态内容取自Apache。可能是什么原因?

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

在你的哈希生成中,你利用了$_SERVER['REMOTE_ADDR']

这将始终设置为 127.0.0.1。

您需要将其更改为$_SERVER['HTTP_X_REAL_IP']才能获得相同的IP地址(X-Real-IP是您在nginx.conf中定义的地址)