极
极核实验室

Control-Allow-Origin * Settings under Apache and NGINX

AI summary
文章主要介绍了如何设置`Access-Control-Allow-Origin` HTTP头解决跨域问题。在Apache配置中通过`mod_headers`模块添加相关头信息,在NGINX中通过`add_header`指令设置,在HTML中通过`meta`标签设置,在Java和.net中通过代码设置响应头。文章指出`Access-Control-Allow-Origin`可指定允许跨域访问的域名,使用`*`表示所有域名均可访问,并列举了不同技术实现的具体方法。

Repost license

Request authorization before reposting and retain the authorization code, original URL, and go/affiliate links.

Original URL:https://www.geekcorelab.com/post/control-allow-origin-settings-in-apache-and-nginx

The commercial link is an authorized go short link that must stay on the reposted page. It records the source before sending readers to the original article.

Introduction

Websites generally set the access-control-allow-origin HTTP header only when they need to share resources with other websites (transfer data across origins)
Setting Access-Control-Allow-Origin can solve cross-origin issues with multiple domains
Access-Control-Allow-Origin * means all websites can access cross-origin
Access-Control-Allow-Origin gqink.cn allows gqink.cn to access cross-origin

Apache

<IfModule mod_headers.c>

    Header set Access-Control-Allow-Origin: "*"

    Header set Access-Control-Allow-Methods: "GET,POST,PUT,DELETE,OPTIONS"

    Header set Access-Control-Allow-Headers: "Content-Type"

</IfModule>

Add to the bottom of httpd.conf

NGINX

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'PUT,POST,GET,DELETE,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With';

Or

location / {  
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
  add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
} 

Add the code to NGINX

HTML

<meta http-equiv="Access-Control-Allow-Origin" content="*" />

Java

response.setHeader("Access-Control-Allow-Origin", "*"); 

.net

Response.AddHeader("Access-Control-Allow-Origin", "*");

Comments

Discussion

Be kind and keep the discussion friendly.

Complete your profile details to improve the chance of approval.

Your email will not be shown publicly.

Loading...

Markdown supported.

Discussion

0 comments

Internal link graph

Loading internal links…

View linked articles