Nginx不会将Cookie传递给代理

2022-08-30 16:46:06

我有一个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调用时,它看起来像这样:

enter image description here

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


答案 1
location / {
  proxy_pass http://localhost:8000;
  proxy_set_header Host $host; # MAGIC
}

答案 2

推荐