Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Does Not Pass Cookies To Proxy

Tags:

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:

enter image description here

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?

like image 953
Devin Dixon Avatar asked Mar 04 '14 14:03

Devin Dixon


1 Answers

location / {
  proxy_pass http://localhost:8000;
  proxy_set_header Host $host; # MAGIC
}
like image 187
Алексей Соснин Avatar answered Sep 24 '22 13:09

Алексей Соснин