【问题标题】:CORS error React native web with Nginx + DjangoCORS 错误 React Native Web with Nginx + Django
【发布时间】:2021-11-26 22:28:12
【问题描述】:

我在 EC2 Django + Nginx 服务器上遇到了 CORS 错误。不知道为什么它永远不起作用。

错误:

Access to fetch at 'https://serveraddress/api/v1/user' from origin 'http://localhost:19006' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

我是如何要求的

  fetch(`https://${env.apiUrl}/api/v1/user`, {
    method: "GET",
    mode: "cors",
    withCredentials: true,
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers":
        "Origin, X-Requested-With, Content-Type, Accept",
      "Access-Control-Allow-Methods":"GET,PUT,POST,DELETE,OPTIONS",
      "Access-Control-Allow-Credentials":'true',
      Authorization: await getToken(),
    },

还尝试使用授权标头或请求获取,不需要发布请求身份验证,但它们都不起作用。 另外,使用 axios、xmlhttprequest 进行了尝试,但都没有工作..

以下是 Nginx + Django 的设置

Nginx 设置

location / {
    proxy_hide_header Access-Control-Allow-Origin;
    add_header Access-Control-Allow-Origin '*';
    add_header Access-Control-Allow-Credentials true always;
    proxy_pass http://web/;
    proxy_redirect     off;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

Django 设置

包括 django-cors-headers 中间件在内的 cors 的所有设置,并将 corsheaders.middleware.CorsMiddleware 放在上面。

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
     ....
    ]
CORS_ORIGIN_ALLOW_ALL = True
ALLOWED_HOSTS = ['*']
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = [
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
]

CORS_ALLOW_HEADERS = [
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
]

CORS_ALLOWED_ORIGIN_REGEXES = [
    r"^https://\w+\.domain\.com$",
]
CSRF_TRUSTED_ORIGINS = ['*']
CORS_ALLOWED_ORIGINS = ['*']

还使用 curl 选项检查了预检消息并获得了

curl --verbose --request OPTIONS 'https://serveraddress/api/v1/user/profile' --header 'Origin: https://localhost:19006' --header 'Access-Control-Request-Headers: Origin, Accept, Content-Type' --header 'Access-Control-Request-Method: GET'

它可以在 Android 或 iOS 上找到,但它不能在网络上运行。

我发现 React 也有类似的问题,但是可以通过 http-proxy-middleware 或在 package.json 上添加代理来解决。 https://create-react-app.dev/docs/proxying-api-requests-in-development/ 但似乎我无法为 react native web 设置代理。

【问题讨论】:

  • 你能不能试试把这个加到nginxadd_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  • @yedpodtrzitko 感谢您的评论!我刚刚尝试过,但没有成功。

标签: django nginx


【解决方案1】:

终于成功了!这是Nginx的问题。 将以下内容添加到位置/成功了

add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET,PUT,POST,OPTIONS,HEAD,DELETE';
add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';
if ($request_method = 'OPTIONS') {
    return 204;
}

【讨论】:

    猜你喜欢
    • 2022-10-05
    • 2021-11-09
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多