【问题标题】:How to redirect http://www.example.com to https://example.com with middleware如何使用中间件将 http://www.example.com 重定向到 https://example.com
【发布时间】: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)

这可能是两个问题,但我不知道如何解决它们,或者如果只有一个更改可以解决两个问题......

【问题讨论】:

    标签: django apache ssl


    【解决方案1】:

    这是 apache 在 django 应用程序需要之前会做的事情,不需要添加 django 中间件。

     <VirtualHost *:80>
          ServerName  example.com
          ServerAlias www.example.com
          ServerAdmin  marcel@example.com
          DocumentRoot /var/www/html
    
     RewriteEngine on
     RewriteCond %{HTTP_HOST} ^www\.
     RewriteRule ^ https://example.com%{REQUEST_URI} [END,NE,R=permanent]
     </VirtualHost>
    

    作为第二层,在settings.py中设置SECURE_SSL_REDIRECT = True

    还有./manage.py check --deploy 管理命令来检查和显示所有内置的安全设置。

    *编辑以显示删除了域名中的www

    【讨论】:

    • 不幸的是,它不适用于添加的 www。无论第二个条件是高于还是低于第一个条件。你还有什么想法吗?
    • 我更新以显示删除 www. 并添加 ServerAlias 以捕获 www.example.comexample.com。 django 设置仍应有助于满足 SSL 要求。
    • 完美!有用。非常感谢,真的!
    • 即使你把我带到了正确的轨道上,我仍然需要一些修改才能让重定向按我想要的方式工作。这是我的 apache2.conf 现在:RewriteEngine on RewriteCond %{HTTP_HOST} ^www\. RewriteRule ^ https://example.com%{REQUEST_URI} [END,NE,R=permanent] RewriteCond %{SERVER_NAME} =example.com RewriteRule ^ https://kryptokram.com%{REQUEST_URI} [END,NE,R=permanent] 但如果有人在 https://www.example.com (SSL_ERROR_BAD_CERT_DOMAIN) 中键入,而 http://example.com 会按照应有的方式进行重定向,则会出现问题。
    • 很高兴它正在工作。在最后一个问题上,我通常将 2 个域添加到letsencrypt,并允许letsencrypt 添加重定向。 www.example.comexample.com 都可以使用 certbot 工具一步完成。这超出了这个问题的范围:)
    猜你喜欢
    • 2020-09-24
    • 2011-07-06
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2016-01-16
    • 2011-05-18
    相关资源
    最近更新 更多