【问题标题】:Django CORS, HTTPS and WSGI setupDjango CORS、HTTPS 和 WSGI 设置
【发布时间】:2017-01-22 03:24:27
【问题描述】:

我正在尝试使用 mod_wsgi 和 Django 设置 CORS + HTTPS,但我无法让它工作。

CORS 在没有 HTTPS/mod_wsgi 的情况下可以正常工作,但是当我尝试添加 HTTPS/mod_wsgi 时停止工作。

对于 CORS,我使用 Django CORS 中间件 (https://github.com/zestedesavoir/django-cors-middleware)

我的 Django 中间件类如下:(请参阅 CorsMiddleware 和 CorsPostCsrfMiddleware)

MIDDLEWARE_CLASSES = [
  'django.middleware.security.SecurityMiddleware',
  'django.contrib.sessions.middleware.SessionMiddleware',
  'corsheaders.middleware.CorsMiddleware',
  'django.middleware.common.CommonMiddleware',
  'django.middleware.csrf.CsrfViewMiddleware',
  'django.contrib.auth.middleware.AuthenticationMiddleware',
  'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
  'django.contrib.messages.middleware.MessageMiddleware',
  'django.middleware.clickjacking.XFrameOptionsMiddleware',
  'corsheaders.middleware.CorsPostCsrfMiddleware',
]

我还添加了如下配置:

CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True

我运行 Django/wsgi 服务器如下:

python manage.py runmodwsgi --host 0.0.0.0 --port 8001 --https-port 8000 --ssl-certificate-file ../utils/ssl_cert/local.crt --ssl-certificate-key-file ../utils/ssl_cert/local.key --processes 8 --server-name localhost --https-only --reload-on-changes

Successfully ran command.
Server URL         : http://localhost:8001/
Server URL (HTTPS) : https://localhost:8000/
Server Root        : /tmp/mod_wsgi-0.0.0.0:8001:1000
Server Conf        : /tmp/mod_wsgi-0.0.0.0:8001:1000/httpd.conf
Error Log File     : /tmp/mod_wsgi-0.0.0.0:8001:1000/error_log (warn)
Request Capacity   : 40 (8 processes * 5 threads)
Request Timeout    : 60 (seconds)
Startup Timeout    : 15 (seconds)
Queue Backlog      : 100 (connections)
Queue Timeout      : 45 (seconds)
Server Capacity    : 85 (event/worker), 70 (prefork)
Server Backlog     : 500 (connections)
Locale Setting     : en_US.UTF-8

如果我从命令行使用 httpie 查询 HTTPS 服务器,它会按预期工作(当然没有 CORS 问题)。

如果我从我的网络应用程序查询 HTTPS 服务器,我会遇到 CORS 问题:

OPTIONS https://127.0.0.1:8001/api/v1/auth-token/ 
XMLHttpRequest cannot load https://127.0.0.1:8001/api/v1/auth-token/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 403.

另外,为 mod_wsgi 服务器生成的配置似乎没有将 headers 设置为允许 CORS

Server Conf        : /tmp/mod_wsgi-0.0.0.0:8001:1000/httpd.conf

我是 Django 新手,我不确定应该在哪个级别配置 HTTPS 的 CORS。

我尝试在 /etc/apache2/sites-enabled/000-default.conf 中配置它失败

<VirtualHost *>
    Header set Access-Control-Allow-Origin "*"
</VirtualHost>

最好, 尼古拉斯

【问题讨论】:

    标签: django https cors mod-wsgi django-cors-headers


    【解决方案1】:

    设置/etc/apache2/sites-enabled/000-default.confmod_wsgi-express 没有任何作用。它们是完全独立的,mod_wsgi-express 忽略任何系统 Apache 安装配置文件。

    如果您在使用 mod_wsgi-express 时需要添加额外的 Apache httpd 配置指令,请将其添加到名为 extra.conf 的文件中:

    Header set Access-Control-Allow-Origin "*"
    

    然后将mod_wsgi-express 运行为:

    python manage.py runmodwsgi --host 0.0.0.0 --port 8001 --https-port 8000 --ssl-certificate-file ../utils/ssl_cert/local.crt --ssl-certificate-key-file ../utils/ssl_cert/local.key --processes 8 --server-name localhost --https-only --reload-on-changes --include-file extra.conf
    

    【讨论】:

    • 谢谢,我相信这解决了 CORS 问题,但现在运行 iunto 另一个问题。我试图访问一个不存在的文件。 [authz_core:error] AH01630:客户端被服务器配置拒绝:/tmp/mod_wsgi-0.0.0.0:8001:1000/htdocs/api。
    • /tmp/mod_wsgi-0.0.0.0:8001:1000/htdocs 目录中有什么?你有没有在任何时候玩过--mount-point 选项到mod_wsgi-express?如果该目录不为空,并且您没有在其中明确添加任何内容,请完全删除 /tmp/mod_wsgi-0.0.0.0:8001:1000 目录并重新开始。
    • 我没有动任何配置。看来/tmp/mod_wsgi-0.0.0.0:8001:1000/htdocs/tmp/mod_wsgi-0.0.0.0:8001:1000/httpd.conf中生成的DocumentRoot。似乎问题在于它试图转到其中的api 目录,而api 不存在。 api 在 url 中仅用于路由 REST API....感谢您的帮助
    • 这是不应该的。您使用的是什么版本的 Apache 和 mod_wsgi?你能把生成的httpd.conf 文件放在gist 的某个地方让我看到吗?也许把这个对话转移到 mod_wsgi 邮件列表。 modwsgi.readthedocs.io/en/develop/finding-help.html
    • 感谢您的建议。我没有联系 mowsgi 邮件列表,因为我不确定我是否错误配置了 mod_wsgi 或 Django。我还针对这个特定问题打开了另一个 SO 问题:stackoverflow.com/questions/39490720/…
    猜你喜欢
    • 1970-01-01
    • 2010-09-07
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    相关资源
    最近更新 更多