如何在Cloud Foundry静态文件Buildpack中配置CORS策略以添加缺少的“ Access-Control-Allow-Origin”标头 [英] How to configure CORS policy in Cloud Foundry Staticfile Buildpack to add missing 'Access-Control-Allow-Origin' header
问题描述
当我尝试使用静态文件Buildpack访问Cloud Foundry上托管的JavaScript文件时, / a>,我的浏览器拒绝加载它并在控制台中显示一条错误消息:
When I try to access a JavaScript file hosted on Cloud Foundry using the Staticfile Buildpack, my browser refuses to load it and displays an error message in the console:
从原始位置访问 ...处的脚本 ...已被CORS政策阻止:所请求的资源上没有 Access-Control-Allow-Origin标头
Access to script at ‘…’ from origin ‘…’ has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
如何在Cloud Foundry静态文件Buildpack中配置跨域资源共享(CORS)?
How do I configure cross-origin resource sharing (CORS) in the Cloud Foundry Staticfile Buildpack?
推荐答案
要使用静态文件构建包:
-
创建一个名为
Staticfile
的文件,其内容如下:
Create a file named
Staticfile
with the following contents:
root: public
location_include: includes/*.conf
确保您要提供应用程序的文件位于公开
创建文件 nginx / conf / includes / headers.conf
(
add_header 'Access-Control-Allow-Origin' '*';
就是这样!在您下一次 cf推送
之后,将设置CORS标头。
That's it! After your next cf push
, the CORS header will be set.
有关更多信息,请参见官方代码示例 include_headers_public
。
For more clarity, see also the official code sample include_headers_public
.
如果创建名为
Staticfile
并将其放在应用程序的构建目录中,当您推送应用程序时,Cloud Foundry会自动使用Staticfile buildpack。
If you create a file named
Staticfile
and locate it in the build directory of your app, Cloud Foundry automatically uses the Staticfile buildpack when you push your app.
(这意味着您无需在 manifest.yml
)
然后按照 自定义位置
。
自定义位置可让您
Custom Location allows you to specify custom location definitions with additional directives.
要自定义NGINX配置文件的位置块,请按照以下步骤操作
To customize the location block of the NGINX configuration file, follow the steps below.
- 设置替代项
root
目录。location_include
属性仅与备用root
结合使用。
- Set an alternative
root
directory. Thelocation_include
property only works in conjunction with an alternativeroot
.
- 使用位置范围内的NGINX指令创建文件。请参见以下示例,该示例使您网站的访问者收到
X-MySiteName
HTTP标头:
- 文件:
nginx / conf / includes / custom_header.conf
- 内容:
add_header X-MySiteName BestSiteEver;
- 文件:
- Create a file with location-scoped NGINX directives. See the following example, which causes visitors of your site to receive the
X-MySiteName
HTTP header:- File:
nginx/conf/includes/custom_header.conf
- Content:
add_header X-MySiteName BestSiteEver;
- File:
-
在 Staticfile 中将
location_include
变量设置为上一步中的文件。此路径相对于nginx / conf
。
Set the
location_include
variable in your Staticfile to the path of the file from the previous step. This path is relative tonginx/conf
.
示例:
root: public
location_include: includes/*.conf
这篇关于如何在Cloud Foundry静态文件Buildpack中配置CORS策略以添加缺少的“ Access-Control-Allow-Origin”标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!