- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在同一服务器上的 Django Rest Framework 和 React 应用程序遇到 CORS 问题。我正在运行 Vagrant,安装了 Ubuntu 18 机器并安装了 NGINX(我假设这个问题将转化为 DigitalOcean),如果我提供了太多信息,我提前道歉。 DRF 使用 Supervisor,Gunicorn 使用端口 8000。我使用 create-react-app 创建了我的 React 应用程序。然后,我使用 npm run build
创建静态文件。
NGINX 设置:
react session
server {
listen 8080;
server_name sandbox.dev;
root /var/sites/sandbox/frontend/build;
index index.html;
client_max_body_size 4G;
location / {
try_files $uri $uri/ /index.html;
}
Django session
upstream sandbox_server {
server unix:/var/tmp/gunicorn_sanbox.sock fail_timeout=0;
}
server {
listen 8000;
server_name api.sandbox.dev;
...
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://sandbox_server;
break;
}
Django 设置:
INSTALLED_APPS = [
...
'rest_framework',
'corsheaders',
'myapp',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
...
]
我尝试了以下方法,但没有成功
CORS_ORIGIN_ALLOW_ALL = True
和
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = ('192.168.19.76:8080','localhost:8080',)
React App.js
...
fetch("http://localhost:8000/api/v1/token-auth/", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({"email":"test@user.com", "password":"testuser"}),
})
因此,声明明显的 CORS 是正确的,因为源是 localhost:8080,这是一个不同的端口,因此它将其视为跨源。我已经尝试了 cors origin allowed 的不同设置,但每次仍然是同样的问题。显然我做错了什么,但我看不到。
我的想法是
选项1
使用 django nginx conf 文件进行代理传递,并删除 React nginx conf 文件,但我不知道这可能会对生产造成什么影响,也不知道这是否是一个好主意。有更好的办法吗?
location /api {
proxy_set_header X-Forwarded_for $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://sandbox_server;
所以最后确定我的想法和问题。在尝试了 CORS 的不同 Django 选项后,我仍然收到 CORS 错误。为什么,是我的 NGINX conf 文件导致的还是其他原因?我希望在 DigitalOcean 中看到这个吗?
更新1
我忘记添加错误了。这是 CORS 错误
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/api/v1/token-auth/. (Reason: CORS request did not succeed).
对于那些想了解网络选项卡输出的人
Host localhost:8000
Origin http://192.168.19.76:8080
Pragma no-cache
Referer http://192.168.19.76:8080/
更新2我使用curl进行了测试,一切都按预期返回,所以我知道DRF正在正常工作。
curl --data "email=test@user.com&password=testuser" http://localhost:8000/api/v1/token-auth/
最终更新
感谢 paulsm4 的所有帮助和纯粹的敬畏。
所以,我确实放弃了 django-cors-headers 并推出了自己的。为了回答 paulsm4 的问题,我在 NGINX 文件中没有 add_header 'Access-Control-Allow-Origin' '*';
虽然我确实考虑过让 NGINX 处理 CORS 与 Django,但从未这样做过远的。 @paulsm4,这是我正在谈论的 proxy_pass 。关键是将这段代码添加到 NGINX 中,以便与我的中间件一起进行 react 部分。
location /api {
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://sandbox_server;
上面的代码本身有效,但它不允许我将任何传入 URL 列入白名单。创建我自己的中间件允许我将其列入白名单。我不知道为什么 django-cors-headers 甚至 django-cors-middleware 对我不起作用。奇怪的是,除了我询问的 CORS 错误之外, fetch 从未对这两个包进行足够远的处理以获取响应 header 和任何类型的错误。使用我编写的中间件,fetch 能够完全进行调用,并返回一些响应头,无论成功还是失败。
为了将来的引用,我可能会重新访问 NGINX 并允许它处理 CORS。这是一个很好的链接 CORS on NGINX
注意
澄清;除了 Django 已经包含的中间件之外,唯一安装的中间件是 cors 中间件。 Django 和 React 驻留在同一服务器上,但端口不同。
Django 设置.py
INSTALLED_APPS = [
...
# Third Party
'rest_framework',
'corsheaders',
# My Apps
'account',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
'django.middleware.csrf.CsrfViewMiddleware',
'corsheaders.middleware.CorsPostCsrfMiddleware',
...
]
CORS_ORIGIN_WHITELIST = (
'null',
'192.168.19.76:8080',
'localhost:8080',
'app.sandbox.com:8080'
)
React App.js
fetch("http://localhost:8000/api/v1/token-auth/", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"email": "test@user.com",
"password": "testuser"
}),
})
所以我在这里束手无策。要么是 django-cors-headers 不起作用,要么可能是 NGINX。
最佳答案
我们一直在交换意见;这是我目前的理解:
问题:您有一个 Django 后端 API(在端口 8080 上)和一个 React 前端(在端口 8000 上)。两者目前都在本地主机上运行,但最终将驻留在 DigitalOcean 上。 React 客户端正在获取 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/api/v1/token-auth/. (Reason: CORS request did not succeed).
您需要配置 CORS。
您捕获的 HTTP 流量中的请求 header 清楚地表明这种情况尚未发生。
一些相关链接包括:
建议的下一步:
如果您还没有安装并配置 django-cors-headers
关于django - Django 和 React 托管在同一服务器上的 CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54371815/
我尝试访问此 API click here for more info POST https://api.line.me/v2/oauth/accessToken 但总是得到错误: XMLHttpRe
我在掌握 CORS 概念时遇到问题... 我认为同源策略保护应用程序不会对“不受信任的域”进行 ajax 调用。所以, mydomain.com 向 发出 ajax 调用somedomain.com
一个技术性很强的问题,可能只有了解浏览器内部结构的人才能回答...... 浏览器缓存 CORS 预检响应的准确程度如何(假设在对 OPTIONS 预检请求的响应中返回了 Access-Control-
我一直在阅读 CORS以及它是如何工作的,但我发现很多事情令人困惑。例如,有很多关于事情的细节,比如 User Joe is using browser BrowserX to get data fr
在 OWASP site 上看到这个矛盾,我感到很困惑。 CORS 备忘单: 使用 Access-Control-Allow-Credentials: true 响应 header 时要特别小心。将允
我们无法在飞行前恢复使用 cors:任何帮助都非常感谢 ==========错误========================== About to connect() to localhost p
跨域请求字体文件时,您必须确保允许请求域使用 CORS header 访问字体文件: 访问控制允许来源 访问控制允许凭据 然而,这在请求图像时不是必需的,无论是对于 img元素或 background
CORS 规范没有说明服务器应如何响应无效的 CORS 请求。例如,如果请求 Origin 无效,则 CORS spec states :“终止这组步骤。请求超出了本规范的范围。”其他错误情况也有类似
我在理解同源策略和“解决”它的不同方法时遇到了一些麻烦。 很明显,同源策略是作为一种安全措施而存在的,因此来自服务器/域的一个脚本无法访问来自另一个服务器/域的数据。 也很明显,有时能够打破此规则很有
我正在尝试使用 cloudformation 在 API Gateway 中部署 API。这些方法需要启用 CORS,我遵循此处的模板 Enable CORS for API Gateway in C
我正在构建一个使用 CORS 的 REST 应用程序。每个 REST 调用都是不同的,我发现获取预检 OPTIONS 调用会产生很大的开销。有没有办法缓存并应用预检选项结果,以便对同一域的任何后续调用
我正在将我的 WebApi 升级到 MVC6。 在 WebApi 中,我可以拦截每个 HTTP 请求,如果是预检,我可以用浏览器可以接受的 header 进行响应。 我正在尝试找出如何在 MVC6 W
假设一个 CORS 预检请求进来了,但它为一个或多个 Access-Control-Request-* 指定了一个不受支持的值。标题。服务器应该如何将其传达回浏览器? 一些例子: 浏览器发送带有 Ac
问题中的一切。 附加信息: 使用 Win 10,GraphDB 免费,9.1.1 • RDF4J 3.0.1 • Connectors 12.0.2 我在控制台 => 设置中添加了 graphdb.w
我正在尝试通过 jQuery 调用 Sonar 网络服务之一。由于调用是跨域进行的,因此调用在 Chrome 上失败并出现以下错误: 请求的资源上不存在“Access-Control-Allow-Or
我想使用 NestJs api,但我在每个 fetch 中都收到相同的错误消息: Access to fetch at 'http://localhost:3000/articles' from or
我不确定这是否属于这里,但我在开发我的 svelte 应用程序时遇到了问题。 在开发过程中,它目前在独立服务器上运行(遵循使用 rollup 和 sirv 的指南)并针对不同端口上的后端 API。 稍
如果在服务器上正确设置 CORS 以仅允许某些来源访问服务器,这是否足以防止 XSRF 攻击? 最佳答案 更具体地说,很容易错误地认为如果 evil.com 由于 CORS 无法向 good.com
我在 Istio 入口上启用 CORS 时遇到问题。正如 Istio Ingress 文档所述,“ingresskubernetes.io”注释将被忽略。是否可以在 Istio 入口上启用 CORS?
我在 Istio 入口上启用 CORS 时遇到问题。正如 Istio Ingress 文档所述,“ingresskubernetes.io”注释将被忽略。是否可以在 Istio 入口上启用 CORS?
我是一名优秀的程序员,十分优秀!