【发布时间】:2023-04-05 20:39:01
【问题描述】:
我在本地玩过一个小 Django (v2.0.1)。现在我正在尝试在我的生产 Apache Web 服务器上实现一个测试用例。我正在运行 Ubuntu 14.04 DigitalOcean droplet(今年晚些时候将升级到 18.04)。
我让 Django 运行。
这里是:http://www.angeles4four.info:8000/
在我登录我的管理面板之前,我认为最好先设置 HTTPS。但是当我使用 https:// 访问该 URL 时,Chrome 会抛出此消息:
此网站无法提供安全连接 www.angeles4four.info 发送了无效响应。 ERR_SSL_PROTOCOL_ERROR
我服务器上的 shell 显示此消息:
[20/Jan/2018 23:54:39] "GET / HTTP/1.1" 200 16559 [21/Jan/2018 00:01:23] 代码 400,消息错误请求语法 ('\x16\x03\x01\x00Ì\x01\x00\x00È\x03\x03&6U\x10µ\x82\x97\x7f´8\ x1e«\x0e¿ÿ§\x89æ\x82\r¢G§\x01ç°P%\x80)ÕÃ\x00\x00\x1c * À+À/À,À0̨̩À\x13À\x14\x00\ x9c\x00\x9d\x00/\x005\x00') [21/Jan/2018 00:01:23] 您正在通过 HTTPS 访问开发服务器,但它仅支持 HTTP。
那是因为没有设置 SSL。我当前的 SSL 证书颁发机构是 Let's Encrypt。 SSL 对我的 public_html 内容运行正常,但不适用于我最近部署的 Django。
我在 SO 的其他地方找到了一些资源,用于使用 Django 设置 SSL。
在标题为“在 Apache 上为 Django 应用程序 (mod_wsgi) 配置 SSL 证书”的 SO 帖子中,Alexey Kuleshevich 高度赞成的回答建议为 Apache 虚拟主机提供 000-default.conf 和 default-ssl.conf 的模板。看这里:
Configure SSL Certificate on Apache for Django Application (mod_wsgi)
我已尽力更改建议的值和条目,以便它们引用我的特定配置。这是我的这两个虚拟主机配置文件现在的样子。
/etc/apache2/sites-available/angeles4four.info-le-ssl.conf:
<IfModule mod_ssl.c>
<VirtualHost *:443>
#ServerName www.example.com
ServerAdmin coffee.drinker.daniel@gmail.com
ServerName angeles4four.info
ServerAlias www.angeles4four.info
DocumentRoot /var/www/html/angeles4four.info/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# Django Application
Alias /static /var/www/html/angeles4four.info/public_html/Cel2FahConversion
<Directory /var/www/html/angeles4four.info/public_html/Cel2FahConversion>
Require all granted
</Directory>
<Directory /var/www/html/angeles4four.info/public_html/Cel2FahConversion>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WGIDaemonProcess cel python-path=/var/www/html/angeles4four.info/public_html/Cel2FahConversion/venv/bin/python3
WSGIProcessGroup cel
WSGIScriptAlias / /var/www/html/angeles4four.info/public_html/Cel2FahConversion/Cel2FahConversion/Cel2FahConversion/wsgi.py
SSLCertificateFile /etc/letsencrypt/live/angeles4four.info/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/angeles4four.info/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/angeles4four.info/chain.pem
</VirtualHost>
</IfModule>
angeles4four.info.conf:
<VirtualHost *:80>
#ServerName www.example.com
ServerAdmin coffee.drinker.daniel@gmail.com
ServerName angeles4four.info
ServerAlias www.angeles4four.info
DocumentRoot /var/www/html/angeles4four.info/public_html
<Directory "/var/www/html/angeles4four.info/public_html">
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =angeles4four.info [OR]
RewriteCond %{SERVER_NAME} =www.angeles4four.info
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
没有骰子。我仍然得到与我最初分享的相同的回溯。
我遇到的下一个 SO 帖子建议修改 settings.py。这里是:Error "You're accessing the development server over HTTPS, but it only supports HTTP"
YoYo 推荐的建议是修改会话 cookie 和安全 SSL 重定向。 YoYo 还建议管理不适用于我的基本、本地、生产设置。所以我尝试将这三行添加到我的 settings.py 中:
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
我的 python3 manage.py runserver shell traceback 仍然显示:“您正在通过 HTTPS 访问开发服务器,但它只支持 HTTP。”
有什么想法吗?我还能尝试什么?
感谢您的关注。
最后一点:这是第一个 SO 帖子,这就是为什么我的超链接被粘贴为原始文本的原因。一旦我得到一些支持,我一定会回到这里编辑这篇文章,并用锚标记润色我的链接。
【问题讨论】:
-
在使用 mod_wsgi 托管 Django 时,您甚至不应该运行
python3 manage.py runserver。你为什么还在运行它?当您不运行它并实际访问 Apache 的 host:port 时会发生什么? -
我之所以使用 'python3 manage.py runserver' 而不是 wsgi 来运行 Django,是因为我是部署 Django 的新手。使用 mod_wsgi 关键字,我找到了有用的 DigitalOcean 指南和官方 Django 文档。我会和他们一起玩,很快就会回到这里报告我的解决方案。
标签: python django ssl https mod-wsgi