nginx - 如何解决 Strapi 生产构建中的 CORS 策略问题?

标签 nginx strapi

现在有人如何修复 Strapi 后端的 CORS 错误吗?我收到如下类似的消息。我用 example.com 替换了我的域:

访问“https://blog.strapi.io/ghost/api/v0.1/posts/?client_id=ghost-frontend&client_secret=1f230799b4ec&limit=2”获取'来自原产地' http://backend.example.com:1337 ' 已被 CORS 策略阻止:对 ' http://backend.example.com:1337/admin 的响应´ 预检请求未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin” header 。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。

我尝试将来源添加到 config/environments/production/security.json

“原点”:“*”

“来源”:“backend.example.com”

他们都没有工作。我仍然收到错误消息。

我会为此提供一些帮助。

最佳答案

对我来说,以下配置有效:

// file: root_project/config/middleware.js:

module.exports = {
  //...
  settings: {
    cors: {
      enabled: true, 
      // headers: '*', 
      origin: ["http://localhost", 'https://foo.example'],
    },
  },
};
设置headers: '*'是访问控制协议(protocol)最简单的用法。服务器发回带有 Access-Control-Allow-Origin: * 的 Access-Control-Allow-Origin header ,这意味着资源可以被 访问任意 起源。
如果您希望限制对资源的访问,仅限来自 https://foo.example 的请求,(这样除了https://foo.example以外的域都不能跨站访问资源),使用上面的设置,这样你的服务器就会发送:Access-Control-Allow-Origin: https://foo.example 备注 : 响应凭证请求请求时,服务器 必须Access-Control-Allow-Origin 的值中指定原点 header ,而不是指定“*”通配符。

关于nginx - 如何解决 Strapi 生产构建中的 CORS 策略问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009731/

相关文章:

nginx - 同一主机上的两个容器之间的通信

nginx - nginx ingress 中的 body 大小到底是多少?

authentication - Strapi中的电话号码身份验证

vue.js - Strapi graphql 突变语法错误 : Unterminated string

javascript - Strapi 自定义身份验证 Controller

node.js - 上传文件时 Nginx 502 Bad Gateway

nginx - 为 E2E 测试配置 nginx-ingress

用于重定向域的 NGinx 配置

api - Strapi v4 插件的扩展服务器 API 不起作用

reactjs - 有什么方法可以使用模板组件/页面使用 Gatsby 创建动态 slugs?