【问题标题】:Make https the Default when Page Loading页面加载时将 https 设为默认值
【发布时间】:2018-07-21 18:48:49
【问题描述】:

我正在从运行在 AWS EC2 上的 Ubuntu 16 (LTS) 服务器运行一个网站。我在 AWS 上为我的安全组设置了以下输入规则

Type    Protocol    Port Range    Source
HTTP    TCP            80        0.0.0.0/0
SSH     TCP            22        72.81.131.89/32
HTTPS   TCP            443       0.0.0.0/0 

在我的 Ubuntu 服务器上,我已经按照here 的描述设置了 SSL。我还将 /etc/apache2/ports.conf 编辑为以下内容。

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80
Listen 8080

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    NameVirtualHost *:443
    Listen 443
</IfModule>

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

我没有文件,/etc/apache2/sites-enabled/000-default.conf。

我创建了一个文件,/etc/apache2/httpd.conf,内容如下

<VirtualHost _default_:443>
ServerName example.com:443
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/example.com.key
SSLCertificateFile /etc/apache2/ssl/example.com.cert
ServerAdmin MYWEBGUY@localhost
DocumentRoot /var/www/html
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/errorSSL.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/accessSSL.log combined

</VirtualHost>

我已经去了/etc/apache2/ssl并进入了

sudo openssl req -new -x509 -nodes -out example.com.crt -keyout example.com.key

该过程没有错误。

当我进入时

sudo a2enmod ssl

我明白了

Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Module socache_shmcb already enabled
Module ssl already enabled

当我打开浏览器并输入时

https://example.com

我的网页以绿色锁打开,表明该网站是安全的。但是,当我刚进入时

example.com

我收到一个常规的 http 连接,并收到一条消息说与该站点的连接不安全。

如何在用户刚进入时将https设为默认

example.com

【问题讨论】:

    标签: apache amazon-web-services ssl amazon-ec2 https


    【解决方案1】:

    当用户点击非 SSL 站点时,您需要强制重定向到 SSL 站点。 我将添加这样的部分:

    <VirtualHost *:80>
       ServerName www.example.com
       Redirect / https://www.example.com/
    </VirtualHost>
    

    或者更好地使用重写规则将所有请求重定向到 SSL 站点:

    RewriteEngine On
    # This will enable the Rewrite capabilities
    
    RewriteCond %{HTTPS} !=on
    # This checks to make sure the connection is not already HTTPS
    
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    

    查看apache wiki 了解更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 2021-09-18
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      相关资源
      最近更新 更多