Django CORS、HTTPS 和 WSGI 设置

Django CORS, HTTPS and WSGI setup

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

CORS 在没有 HTTPS/mod_wsgi 的情况下工作正常,但当我尝试添加 HTTPS/mod_wsgi.

时停止工作

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

我的 Django middleware_classes 如下:(参见 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>

最好, 尼古拉斯

设置 /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