码迷,mamicode.com
首页 > 其他好文 > 详细

nginx配置允许跨域

时间:2021-06-06 18:45:56      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:char   ext   type   oca   ESS   ons   eth   请求方式   charset   

location / {

    add_header Access-Control-Allow-Origin *;

    add_header Access-Control-Allow-Methods ‘GET,POST‘;

    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‘) {
    add_header ‘Access-Control-Allow-Origin‘ ‘*‘;
    add_header ‘Access-Control-Allow-Methods‘ ‘GET, POST, OPTIONS‘;
    add_header ‘Access-Control-Allow-Headers‘ ‘DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range‘;
    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,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range‘;
    add_header ‘Access-Control-Expose-Headers‘ ‘DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,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,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range‘;
    add_header ‘Access-Control-Expose-Headers‘ ‘DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range‘;
}

nginx配置允许跨域

标签:char   ext   type   oca   ESS   ons   eth   请求方式   charset   

原文地址:https://www.cnblogs.com/fyiyy/p/14853406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!