Nginx不会将Cookie传递给代理

pinkon5k  于 2023-03-29  发布在  Nginx
关注(0)|答案(2)|浏览(251)

我有一个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配置一起使用。我做错了什么?

moiiocjp

moiiocjp1#

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

4bbkushb2#

如果您的cookie在www.example.com上工作DOMAIN.com/wp-admin,但当您返回DOMAIN.com时,您的cookie将无法工作,这可能会有所帮助
对于我在端口80没有ssl这解决了我的问题
在主机代理URL中添加$uri:

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;
        

}

但在我的情况下,当我尝试相同的位置为另一个服务器与ssl有400错误,并根据这个答案:https://stackoverflow.com/a/64126257/19293776
我删除了$uri并添加了以下行:

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

相关问题