在nginx上启用CORS

时间:2018-04-18 20:51:17

标签: nginx cors swagger-ui

我已按照this示例在我的API子域上启用CORS,以便我可以从SwaggerUI向它发送请求。这是我在该子域上运行OPTIONS得到的输出:

curl -i -X OPTIONS http://api.MYDOMAIN.com/v1/data/extraction

HTTP/1.1 204 No Content
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 18 Apr 2018 20:45:52 GMT
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-API-Key
Access-Control-Max-Age: 1728000
Content-Type: text/plain charset=UTF-8
Content-Length: 0

我坚持要去哪里找出为什么在我的docs子域(相同的DOMAIN.com服务器)上Chrome,CORS error仍然给了我这个。任何人都可以建议我应该去哪里看看吗?

4 个答案:

答案 0 :(得分:2)

我有一个类似的问题,无法让它发挥作用。这个设置终于对我有用了。 https://gist.github.com/Stanback/7145487这就是它的样子。我只是设置$cors 'true'来测试它是否有效。它对我有用。这都位于/ location {...}区域

set $cors '';
    if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)') {
            set $cors 'true';
    }

    if ($cors = 'true') {
            add_header 'Access-Control-Allow-Origin' "$http_origin" always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
            # required to be able to read Authorization header in frontend
            #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
    }

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
            add_header 'Access-Control-Allow-Credentials' 'true' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
            # required to be able to read Authorization header in frontend
            #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
            # Tell client that this pre-flight info is valid for 20 days
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
    }

答案 1 :(得分:1)

所有这一切都归结为NGINX使用了 more_set_headers 而不是 add_header (它可能'是NGINX启用了这个模块)之后使用上面的一个例子使它工作。

答案 2 :(得分:0)

您可能希望使用curl和CORS原始标头测试对api.MYDOMAIN.com/v1/data/extraction的调用。

curl -i -X POST -H "Origin: http://docs.whatever.com" \
 --verbose http://api.MYDOMAIN.com/v1/data/extraction

回复应该带有标题:

Access-Control-Allow-Origin:*

如果该标题没有带回响应,则chrome会抛出错误,就像你看到的那样。

P.S。在错误中提到nginx用404响应,这可能与它有关。

答案 3 :(得分:0)

问题可能是x[np.stack((s,s+d))] Access-Control-Allow-Origin: * 不兼容 - 如果您要传递凭据,则需要回复Access-Control-Allow-Credentials: true和{ {1}}。