【发布时间】:2015-03-19 01:57:41
【问题描述】:
我遇到了很多话题,但我找不到我的问题的问题。
我正在开发一个应用程序:http://example.com:8081/app/
我有一个 Angular 应用程序:http://example.com:8081/app/angular/ 和http://example.com:8081/app/rest/上的休息api
我已经制作了一个 nginx 服务器来代理这个域,例如:
http://angular.example.com => 代理 Angular 应用程序和 http://rest.example.com => 代理其余的 api。
Booth 正在工作,并且正在通信,但我无法存储由 rest.exemple.com 发送的 cookie
登录服务后有响应头:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin: "http://angular.example.com"
Connection:keep-alive
Content-Type:application/json
Date:Tue, 20 Jan 2015 17:52:19 GMT
Server:nginx/1.2.1
Set-Cookie:JSESSIONID=0F69455E4475CDFBF7D1C01BC4C1D121; Path=/app/; HttpOnly
Transfer-Encoding:chunked
Vary:Origin
那么 Angular 就不要使用这个 JSESSIONID 来打别人的电话了……
我设置了$httpProvider.defaults.withCredentials = true;,并没有改变任何东西...
有什么想法吗?
编辑:当我输入网址时工作:http://example.com:8080/app/rest/ 但不是http://rest.example.com/
有我的 nginx 配置文件:
server {
listen 80;
server_name rest.example.com;
location / {
proxy_pass http://localhost:8081/app/rest/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
@sergiocruz : document.cookie 什么都不返回,并且还带有http://example.com:8080/app/rest/
EDIT2:在这篇文章中使用 apache: How to properly set JSESSIONID cookie path behind reverse proxy
如果有人必须用 nginx 解决:)
【问题讨论】:
-
如果您在控制台上输入
document.cookie,是否可以看到您要查找的cookie? -
@sergiocruz 刚刚编辑过 ;)
-
您是否使用通配符域名设置cookie?这有很大的不同......除非您设置通配符选项,否则子域被认为是完全不同的域。您是在服务器端生成这些 cookie 吗?确保您的服务器端应用设置了该选项。
标签: angularjs rest tomcat nginx