我们有一个内部 JIRA 服务器正在运行。我正在使用提供的 REST api 开发一个 UI Web 应用程序。
我的 angularjs UI Web 应用程序运行在http://127.0.0.1:9000
哪个命中http://localhost/jira-api/rest/api/2/...
为了处理跨域问题,我使用以下配置设置了 ngnix。
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream jiraserver {
server myjira.com;
}
server {
listen localhost:80;
server_name localhost;
location /jira-api {
rewrite ^/jira-api/(.*) /$1 break;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
add_header 'Content-Length' 0;
add_header 'Content-Type' 'text/plain charset=UTF-8';
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
proxy_pass http://jiraserver;
}
}
}
但这给了我一个关于 OPTIONS 请求的错误:请求的资源上没有“Access-Control-Allow-Origin”标头。http://127.0.0.1:9000
因此不允许Origin访问。
OPTIONS 请求的响应标头实际上没有我上面设置的任何标头:
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 22 Sep 2014 12:35:39 GMT
Content-Type: text/html;charset=UTF-8
Content-Length: 0
Connection: keep-alive
X-AREQUESTID: 515x609530x4
X-Seraph-LoginReason: OK
X-ASESSIONID: 10iojgl
X-AUSERNAME: someone
是不是配置有问题??