gpt4 book ai didi

cors - 如何在 Cloud Foundry Staticfile Buildpack 中配置 CORS 策略以添加缺少的 'Access-Control-Allow-Origin' header

转载 作者:行者123 更新时间:2023-12-04 16:42:10 26 4
gpt4 key购买 nike

当我尝试使用 Staticfile Buildpack 访问托管在 Cloud Foundry 上的 JavaScript 文件时,我的浏览器拒绝加载它并在控制台中显示一条错误消息:

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 Staticfile Buildpack 中配置跨源资源共享 (CORS)?

最佳答案

使用静态文件构建包设置 CORS:

  1. 创建一个名为Staticfile的文件,内容如下:

     root: public
    location_include: includes/*.conf
  2. 确保您希望应用提供的文件位于 public 文件夹中。

  3. 使用以下内容创建文件 nginx/conf/includes/headers.conf(及其父文件夹):

      add_header 'Access-Control-Allow-Origin' '*';

就是这样!在您的下一次 cf push 之后,将设置 CORS header 。

为了更清楚,另见官方代码示例include_headers_public .

说明

截至 Staticfile Buildpack documentation :

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 中指定 staticfile_buildpack)

然后按照 Custom Location 下的说明进行操作.

Custom Location allows you to specify custom location definitions with additional directives.

To customize the location block of the NGINX configuration file, follow the steps below.

  1. Set an alternative root directory. The location_include property only works in conjunction with an alternative root.
  1. 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;
  1. Set the location_include variable in your Staticfile to the path of the file from the previous step. This path is relative to nginx/conf.

    Example:

     root: public
    location_include: includes/*.conf

关于cors - 如何在 Cloud Foundry Staticfile Buildpack 中配置 CORS 策略以添加缺少的 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58408784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com