nginx - Nginx 从源获取已被 CORS 策略阻止
问题描述
我在后端 API 的前面有 Nginx,具有以下配置并暴露于api.dev.example.com
upstream api {
server api:8000;
}
server {
listen 80 default_server;
server_name localhost;
location / {
if ($request_method = OPTIONS) {
return 204;
}
proxy_pass http://api;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Max-Age 3600;
add_header Access-Control-Expose-Headers Content-Length;
add_header Access-Control-Allow-Headers Range;
}
}
我在 ( localhost:3000
) & 运行的本地前端开发环境中使用 API 端点,但是当 API 返回代码不是2xx
Access to fetch at 'https://api.dev.example.com/api/v1/users' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
这个问题的任何解决方案?
解决方案
推荐阅读
- asp.net - 在 cloudflare 中创建子域,添加托管在 IIS 上的新网站返回 HTTP 404 错误
- sql - Oracle Apex 交互式网格根据条件禁用 ROW
- qnamaker - 导出的 QnA maker 知识库上的 BF CLI 解析错误
- python - Django,Docker - 由于未安装 Pillow,因此无法使用 ImageField
- android - 如何为 Android 实现这个特定的“AnimatedBottomNavigationBar”?方法错误
- javascript - Three.js:为什么没有和 OrbitControls 3d 模型不显示?; 是否可以进行自动模型尺寸调整
- arrays - When (void *) p == (void *) *p - 标准对此有何评论?
- ruby-on-rails - RAILS:使用呈现形式的只读字段
- css - 使用剪辑路径 css 的倒曲线
- rabbitmq - TCP连接成功,Erlang分发失败