导读 ​ 在 HTML 中,<a>, <form>, <img>, <script>, <iframe>, <link> 等标签以及 Ajax 都可以指向一个资源地址,而所谓的跨域请求就是指:当前发起请求的域与该请求指向的资源所在的域不一样。这里的域指的是这样的一个概念:我们认为若协议 + 域名 + 端口号均相同,那么就是同域。

那NGINX如何配置跨域请求,跨域请求失败时报错: 
403 No 'Access-Control-Allow-Origin' header is present on the requested resource

nginx 配置

比如请求的接口是: http://www.test.com/exchangeApi/xxxx

server {
    listen 80;
    server_name test.com www.test.com;
    root /data/web/homepage;
    index index.html;

    location / {
    
    }

    location ~ /exchangeApi/ {
        add_header Access-Control-Allow-Origin *;
        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,Authorization';

        if ($request_method = 'OPTIONS') {
            return 204;
        }

        ....
    }
}

这样即可配置完成对于 /exchangeApi/ 这个资源请求的跨域请求.

对于跨域头部的解释

Access-Control-Allow-Origin

服务器默认是不被允许跨域的。给Nginx服务器配置Access-Control-Allow-Origin *后,表示服务器可以接受所有的请求源(Origin),即接受所有跨域的请求。

Access-Control-Allow-Headers

是为了防止出现以下错误:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

这个错误表示当前请求Content-Type的值不被支持。其实是我们发起了"application/json"的类型请求导致的。

Access-Control-Allow-Methods

是为了防止出现以下错误:

Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

return 204

给OPTIONS 添加 204的返回,是为了处理在发送POST请求时Nginx依然拒绝访问的错误. 发送"预检请求"时,需要用到方法 OPTIONS ,所以服务器需要允许该方法。

预检请求( preflight request)

CROS (Cross-origin resource sharing): 全称是跨域资源共享, 可以解决跨域请求的问题.

CORS 标准新增了一组 HTTP 首部字段,允许服务器声明哪些源站有权限访问哪些资源。

规范要求,对那些可能对服务器数据产生副作用的HTTP 请求方法, 特别是 GET 以外的 HTTP 请求,或者搭配某些 MIME 类型的 POST 请求,浏览器必须首先使用 OPTIONS 方法发起一个预检请求(preflight request),从而获知服务端是否允许该跨域请求。服务器确认允许之后,才发起实际的 HTTP 请求。在预检请求的返回中,服务器端也可以通知客户端,是否需要携带身份凭证(包括 Cookies 和 HTTP 认证相关数据).

Content-Type 字段的类型为 application/json 的请求就是上面所说的搭配某些 MIME 类型的 POST 请求. CORS规定,Content-Type不属于以下MIME类型的,都属于预检请求:

application/x-www-form-urlencoded
multipart/form-data
text/plain

所以 application/json 的请求会在正式通信之前,增加一次"预检"请求,这次"预检"请求会带上头部信息 Access-Control-Request-Headers: Content-Type :

OPTIONS /api/test HTTP/1.1
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type
...

服务器回应时,返回的头部信息如果不包含Access-Control-Request-Headers: Content-Type则表示不接受非默认的的Content-Type。即出现以下错误:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

原文来自:https://www.cnblogs.com/tiantiandas/p/nginx_config_CORS.html

本文地址:https://www.linuxprobe.com/nginx-kua-yurepest.html编辑:问题终结者,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/