JIRA api的nginx CORS配置无法正常工作

时间:2014-09-22 10:29:42

标签: angularjs nginx jira-rest-api

我们还没有运行内部JIRA服务器。我正在开发一个提供REST api的UI Web应用程序。

我的angularjs UI网络应用程序在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请求错误:No' Access-Control-Allow-Origin'标头出现在请求的资源上。因此,不允许原点http://127.0.0.1:9000访问。

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

配置有问题??

1 个答案:

答案 0 :(得分:2)

尝试移动

rewrite ^/jira-api/(.*) /$1 break;

到底部

rewrite ^/jira-api/(.*) /$1 break; proxy_pass http://jiraserver;