ITeye博客

nginx中配置跨域支持功能

tkggft · 2015-10-14 10:49 · nginx中配置跨域支持功能 · 企业架构 · Markdown

在nginx.conf中配置

http {
  ......
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers X-Requested-With;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
  ......
}

这样就可以实现GET,POST,OPTIONS的跨域请求的支持
也可以 add_header Access-Control-Allow-Origin http://test.51testing.com; --指定允许的url;

配置项详细解释:w3c-Cross-Origin Resource Sharing

 

 

 

 

 

 

 

在Nginx location 里加上如下代码可以解决js 请求跨域问题

 

 

if ($request_method = 'OPTIONS') { 

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'; 

return 200; 

}

 

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';

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。