【问题标题】:ngnix CORS configuration for JIRA api not workingJIRA api的nginx CORS配置不起作用
【发布时间】:2014-09-22 23:18:21
【问题描述】:

我们有一个内部 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”标头。因此,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

配置有问题吗??

【问题讨论】:

    标签: angularjs nginx jira-rest-api


    【解决方案1】:

    尝试移动

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

    到底部

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

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      • 2019-08-16
      • 2020-06-23
      • 1970-01-01
      • 2017-03-05
      • 2019-07-15
      相关资源
      最近更新 更多