Nginx Does Not Pass Cookies To Proxy

2019-06-17 04:01发布

问题:

I have a cookie set will work for all subdomains, .example.com . I have nginx ajax calls go through a proxy_pass but the cookie does not remain. My configuration looks like this:

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

}

But when I check the ajax call, it looks like this:

The picture above shows the cookie being sent has a domain of N/A when it should be .example.com . It works with my Apache/PHP configuration but does not with my nginx/php configuration. What am I doing wrong?

回答1:

I think you should replace

server_name  www.example.com;

with:

server_name  www.example.com example.com *.example.com;