如何设置Access-Control-Allow-Origin标头,以便可以在主域中使用子域中的网络字体?
笔记:
您可以在HTML5BP服务器配置项目https://github.com/h5bp/server-configs中找到大多数HTTP服务器的此标头和其他标头的示例
如何设置Access-Control-Allow-Origin标头,以便可以在主域中使用子域中的网络字体?
笔记:
您可以在HTML5BP服务器配置项目https://github.com/h5bp/server-configs中找到大多数HTTP服务器的此标头和其他标头的示例
Answers:
Nginx必须使用http://wiki.nginx.org/NginxHttpHeadersModule进行编译(在Ubuntu和其他一些Linux发行版中是默认设置)。那你可以做
location ~* \.(eot|ttf|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
}
最新的答案:
#
# Wide-open CORS config for nginx
#
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
# Om nom nom cookies
#
add_header 'Access-Control-Allow-Credentials' 'true';
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,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# 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-Credentials' 'true';
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';
}
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-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
}
来源:https : //michielkalkman.com/snippets/nginx-cors-open-configuration.html
您可能还希望添加Access-Control-Expose-Headers
(以与Access-Control-Allow-Headers相同的格式),以便向ajax请求公开您的自定义和/或“非简单”标头。
Access-Control-Expose-Headers (optional) - The XMLHttpRequest 2 object has a
getResponseHeader() method that returns the value of a particular response
header. During a CORS request, the getResponseHeader() method can only access
simple response headers. Simple response headers are defined as follows:
Cache-Control
Content-Language
Content-Type
Expires
Last-Modified
Pragma
If you want clients to be able to access other headers, you have to use the
Access-Control-Expose-Headers header. The value of this header is a comma-
delimited list of response headers you want to expose to the client.
- http://www.html5rocks.com/en/tutorials/cors/
其他Web服务器的配置http://enable-cors.org/server.html
if
在Nginx中使用,即使官方手册也不建议使用。
always
选项添加到所有内容很有用,add_header
这样也可以为非200响应添加标头。从nginx 1.7.5开始:nginx.org/en/docs/http/ngx_http_headers_module.html
这是我写的文章,它避免了GET | POST的某些重复。它应该使您在Nginx中使用CORS。
这是帖子中的示例片段:
server {
listen 80;
server_name api.test.com;
location / {
# Simple requests
if ($request_method ~* "(GET|POST)") {
add_header "Access-Control-Allow-Origin" *;
}
# Preflighted requests
if ($request_method = OPTIONS ) {
add_header "Access-Control-Allow-Origin" *;
add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
return 200;
}
....
# Handle request
....
}
}
204 No content
因为它似乎更合适。
首先,让我说@hellvinz答案对我有用:
location ~* \.(eot|ttf|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
}
但是,我决定用一个单独的答案回答这个问题,因为在花了大约十多个小时的时间来寻找解决方案之后,我才设法使该解决方案生效。
默认情况下,Nginx似乎没有定义任何(正确的)字体MIME类型。通过遵循这个葬礼,我发现可以添加以下内容:
application/x-font-ttf ttc ttf;
application/x-font-otf otf;
application/font-woff woff;
application/font-woff2 woff2;
application/vnd.ms-fontobject eot;
到我的etc/nginx/mime.types
档案。如前所述,以上解决方案就可以了。
Nginx的传统add_header指令不适用于4xx响应。由于我们仍想向其添加自定义标头,因此我们需要安装ngx_headers_more模块才能使用more_set_headers指令,该指令也可用于4xx响应。
sudo apt-get install nginx-extras
然后在nginx.conf文件中使用more_set_headers,我将示例粘贴到下面
server {
listen 80;
server_name example-site.com;
root "/home/vagrant/projects/example-site/public";
index index.html index.htm index.php;
charset utf-8;
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE, HEAD';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept,Authorization';
location / {
if ($request_method = 'OPTIONS') {
more_set_headers 'Access-Control-Allow-Origin: $http_origin';
more_set_headers 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE, HEAD';
more_set_headers 'Access-Control-Max-Age: 1728000';
more_set_headers 'Access-Control-Allow-Credentials: true';
more_set_headers 'Access-Control-Allow-Headers: Origin,Content-Type,Accept,Authorization';
more_set_headers 'Content-Type: text/plain; charset=UTF-8';
more_set_headers 'Content-Length: 0';
return 204;
}
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/example-site.com-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
就我而言,使用Rails 5,唯一可行的解决方案是添加rack-cors
gem。像这样:
在/ Gemfile中
# Gemfile
gem 'rack-cors'
在config / initializers / cors.rb中
# config/initializers/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'localhost:4200'
resource '*',
headers: :any,
methods: %i(get post put patch delete options head)
end
end
来源:https : //til.hashrocket.com/posts/4d7f12b213-rails-5-api-and-cors