【问题标题】:Making AJAX requests to a CORS-enabled NGINX Server向支持 CORS 的 NGINX 服务器发出 AJAX 请求
【发布时间】:2020-04-09 16:46:15
【问题描述】:

我正在尝试连接到 ReSTful API,为此我需要让服务器知道我有权这样做。我自己托管这个 ReST API,我使用 NGINX 作为反向代理。我已将 NGINX 配置为允许 CORS 请求(通过enable-cors)。为了便于使用,我决定使用 jQuery 来处理 AJAX 请求,并使用选项来发送我的凭据。

URL = "<url>"
$.ajax({
    url: URL,
    type: "GET",
    xhrFields: {
        withCredentials: true
    },
}).done(function(e) {
    console.log(e)
})

长话短说:我收到以下错误

Access to XMLHttpRequest at '<url>' from origin 'http://localhost:8000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

我的NGINX配置如下:

add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    #
    # Custom headers and headers various browsers *should* be OK with but aren't
    #
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    #
    # 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;
}
if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}

我知道我必须找到使用此通配符的解决方案,但是,在我工作的网络上,IP 地址经常出现。那么,来回答我的问题:做什么?

【问题讨论】:

  • 您可以使用add_header 'Access-Control-Allow-Origin' $http_origin,但允许来自任意来源的凭据请求并不是一个好主意。相反,您实际上想要做的是,检查$http_origin 值是否与您要允许的来源的某些模式匹配,如果匹配,则仅执行add_header 'Access-Control-Allow-Origin' $http_origin,否则不要在全部。见stackoverflow.com/a/44600395/441757等。
  • @sideshowbarker 感谢您的快速回复。为了消除任何疑虑,此服务器只能从本地网络内部访问。我已经采纳了您的建议,但它给了我以下错误:Access to XMLHttpRequest at '&lt;url&gt;' from origin 'http://localhost:8000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains the invalid value '&lt;server_ip&gt;'.

标签: jquery ajax nginx cors reverse-proxy


【解决方案1】:

试试always;

add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range' always;

【讨论】:

  • 感谢您的快速回复。你能详细说明一下吗?我应该用那个代替什么吗?我应该将其附加到 NGINX 配置中吗?
  • 在您的主机中使用 always; 否则删除您的代码并添加此
猜你喜欢
  • 2020-01-24
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 2015-09-01
  • 2015-09-29
  • 2014-12-31
相关资源
最近更新 更多