【发布时间】:2020-12-02 08:30:06
【问题描述】:
我在部署我的第一个 django 应用程序时迷路了。我面前的任务是将 http://www 重定向到裸 https://。以下已经在工作:
https://www » 裸 https://
裸 http:// » 裸 https://
但如果我只输入www.,我会登陆标准的 Ubuntu/Apache 网站。
middleware.py
from django.http import HttpResponsePermanentRedirect
class WwwRedirectMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
host = request.get_host().partition(':')[0]
if host == "www.example.com":
return HttpResponsePermanentRedirect(
"https://example.com" + request.path
)
else:
return self.get_response(request)
example.conf
1 <VirtualHost *:80>
10 ServerName example.com
11 ServerAdmin marcel@example.com
12 DocumentRoot /var/www/html
31 RewriteEngine on
32 RewriteCond %{SERVER_NAME} =example.com
33 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
34 </VirtualHost>
example-le-ssl.conf
1 <IfModule mod_ssl.c>
2 <VirtualHost *:443>
11 ServerName example.com
12 ServerAdmin marcel@example.com
13 DocumentRoot /var/www/html
60 SSLCertificateFile /etc/letsencrypt/live/example.com-0001/fullchain.pem
61 SSLCertificateKeyFile /etc/letsencrypt/live/example.com-0001/privkey.pem
62 Include /etc/letsencrypt/options-ssl-apache.conf
63 </VirtualHost>
64 </IfModule>
这就是我用 curl
得到的~$ curl -I http://example.com
HTTP/1.1 301 Moved Permanently
Date: Wed, 02 Dec 2020 08:03:02 GMT
Server: Apache/2.4.46 (Ubuntu)
Location: https://example.com/
Content-Type: text/html; charset=iso-8859-1
~$ curl -I http://www.example.com
HTTP/1.1 200 OK
Date: Wed, 02 Dec 2020 08:03:21 GMT
Server: Apache/2.4.46 (Ubuntu)
Last-Modified: Wed, 11 Nov 2020 22:26:44 GMT
ETag: "2aa6-5b3dc4a55bcd4"
Accept-Ranges: bytes
Content-Length: 10918
Vary: Accept-Encoding
Content-Type: text/html
~$ curl -I https://www.example.com
curl: (60) SSL: no alternative certificate subject name matches target host name 'www.example.com'
More details here: https://curl.haxx.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
但证书似乎是正确的:
~$ sudo certbot --apache -d example.com -d www.example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Cert not yet due for renewal
You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.
(ref: /etc/letsencrypt/renewal/example.com.conf)
这可能是两个问题,但我不知道如何解决它们,或者如果只有一个更改可以解决两个问题......
【问题讨论】: