【问题标题】:Nginx Does Not Pass Cookies To ProxyNginx 不会将 Cookie 传递给代理
【发布时间】:2014-04-06 04:01:48
【问题描述】:

我有一个 cookie 集适用于所有子域 .example.com 。我有 nginx ajax 调用通过 proxy_pass 但 cookie 没有保留。我的配置如下:

server {
    listen   80;
    server_name  www.example.com;

    location / {
        root   /data/sites/www.example.com/widgets/public_html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?rt=$uri&$args;
    }

    location ~ .php$ {
      root          /data/sites/www.example.com/site/public_html;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param REQUEST_URI    $uri;
      fastcgi_index  index.php;
      include        fastcgi_params;
      fastcgi_param ENV staging;
    }


    location /api {
        proxy_pass_header  Set-Cookie;
        proxy_cookie_domain $host example.com;
        proxy_pass_header  P3P;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Fowarded-Host $host;
        proxy_pass http://api.example.com/;
        proxy_connect_timeout 1;
    }

}

但是当我检查 ajax 调用时,它看起来像这样:

上图显示正在发送的 cookie 的域为 N/A,而它应该是 .example.com 。它适用于我的 Apache/PHP 配置,但不适用于我的 nginx/php 配置。我做错了什么?

【问题讨论】:

  • 请求 cookie 总是 N/A,所以没有任何意义。
  • 你用 AJAX 请求什么路径?
  • @devindixon 我也面临同样的问题。你找到解决办法了吗?
  • samw 问题你用这个做了什么?

标签: php ajax apache cookies nginx


【解决方案1】:
location / {
  proxy_pass http://localhost:8000;
  proxy_set_header Host $host; # MAGIC
}

【讨论】:

  • 请您解释一下您的解决方案是如何工作的,以便人们从中学习:)
  • # MAGIC 是我们需要知道的一切 :)
  • Here 是正确的解释。
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 2019-03-10
  • 2021-09-09
  • 2011-06-20
  • 1970-01-01
相关资源
最近更新 更多