【问题标题】:Tornado + nginx CORS cookiesTornado + nginx CORS cookie
【发布时间】:2014-09-12 20:00:54
【问题描述】:

我有 Tornado 应用程序,作为 API 服务器工作,它处理来自其他几个站点的请求。我也打算用 nginx 作为负载均衡器。

问题在于 CORS cookie。例如,当有人访问合作伙伴站点 partner.com 时,那里的 JS 脚本会向我的服务器发送多个请求。第一个请求旨在设置几个 cookie。但它没有。

我有什么:

  1. tornado 上没有标题设置
  2. nginx 上的如下配置

    access_log /var/log/nginx/api_access.log;
    error_log /var/log/nginx/api_error.log;
    
    add_header Access-Control-Allow-Origin http://salesmarine.ru;
    add_header Access-Control-Allow-Credentials 'true';
    add_header Set-Cookie test=111;
    
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://api;
        proxy_next_upstream error;
    }
    
  3. 来自 JS 的所有请求都使用.withCredentials=true; 选项发送 以下是请求数据:

    Accept:*/*
    Accept-Encoding:gzip,deflate,sdch
    Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
    Cache-Control:max-age=0
    Connection:keep-alive
    Content-Type:application/x-www-form-urlencoded
    Cookie:t_id=d9ed7601a2074e78b5ba38ab58ad7043; t_id_session=0c7910f9b3194e899d233e68380df59f;                 test=111
    Host:server.com
    If-None-Match:"bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f"
    Origin:http://example.com
    Referer:http://example.com/test
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko)         Chrome/37.0.2062.120 Safari/537.36
    Query String Parametersview sourceview URL encoded
    id:54129700102392342dff6dbe
    apiKey:2a8bb0a06bca4ad59c4d4079dce30a30
    client_id:54129700102392342dff6dbe
    apikey:2a8bb0a06bca4ad59c4d4079dce30a30
    user_id:null
    Response Headersview source
    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Origin:http://example.com
    Connection:keep-alive
    Date:Fri, 12 Sep 2014 19:40:17 GMT
    Etag:"bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f"
    Mime-Type:text/javascript
    Server:TornadoServer/4.0.2
    Set-Cookie:test=111
    

并且不发送任何 cookie。可能是什么问题?

还有什么更好 - 来自 tornado 或 nginx 的控制标头?

【问题讨论】:

    标签: javascript cookies nginx cors tornado


    【解决方案1】:

    嗯,我想通了。问题出在 CORS 本身——我无法为另一个域设置 cookie。 Access-Control-Allow-Credentials 允许这样做,但该域上的 JS 无法使用 cookie。

    有很多指向 this 资源的链接,但它对于理解 CORS 非常有用。

    我最初问题的第二部分是对我保持模棱两可。一个疯狂的猜测——如果你使用 nginx + tornado,nginx 首先接收传入的请求。所以它应该返回正确的标题。

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 2018-12-24
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 2021-02-08
      • 2019-02-27
      相关资源
      最近更新 更多