如何将会话/cookie 从主域传递到 Nginx 中的子域?

How to pass session / cookie from main domain to subdomains in Nginx?

所以我的主域中有一个子域的 iframed 页面,这个子域页面需要用户登录并拥有会员资格才能访问。

基本上我需要将会话变量和 cookie 传递到子域以便加载 iframe。

我如何在 Nginx 中实现这一点?

Cookie 有一个 domain 属性,该属性指定它们将从客户端发送到哪些域。例如,在 PHP 的 setcookie 函数中,第 5 个参数接受要在 cookie 中设置的 $domain 字符串。默认情况下,它留空,这意味着它将使用客户端收到请求时来自的域。

The domain that the cookie is available to. Setting the domain to 'www.example.com' will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as 'example.com' will be available to higher subdomains, such as 'www.example.com'. Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.

因此,如果您将 cookie 设置到您的主域,客户端 UA 将不会有问题使其可用于您的子域。

不过,现在,iframe 有点棘手了。例如,Internet Explorer 可以根据其不同的隐私政策规则区别对待 iframe,并阻止来自 iframe 的所有 cookie。有关详细信息,请参阅 this question。然而,Nginx 在这一切中真的应该扮演一个被动的角色。