30

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?

Devin Dixon
  • 11,553
  • 24
  • 86
  • 167

2 Answers2

9
location / {
  proxy_pass http://localhost:8000;
  proxy_set_header Host $host; # MAGIC
}
0

if your cookies work on like DOMAIN.com/wp-admin but when you go back to DOMAIN.com your cookies won't work this might help

for me on port 80 without ssl this solved my ploblem

add $uri in your Host proxy URL:

    location / {
        proxy_pass http://192.168.1.12;
        proxy_set_header Host $host$uri;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Scheme $scheme;
        

}

but in my case when i tried the same location for another server with ssl there was 400 error on and based on this answer : https://stackoverflow.com/a/64126257/19293776

i deleted $uri and added those lines:

            location / {
        proxy_pass http://192.168.2.12;
        proxy_set_header Host $host;
         ######
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
         ######
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Scheme $scheme;
       }
nitro
  • 11
  • 2