0

为了保护我的网站,我在 Ubuntu 上使用 apache2 并使用letsencrypt 达到极致。我也想从http://mywebsite.com重定向到https://mywebsite.com但这不适用于以下配置:

文件名-xy.conf 如下所示:

<VirtualHost *:80>
    ServerName mywebsite.com

    ProxyRequests off
    ProxyPreserveHost On
    <Location / >
        ProxyPass "ajp://localhost:9090/"
        ProxyPassReverse "ajp://localhost:9090/"
    </Location>
</VirtualHost>

文件名-xy-ssl.conf 如下所示:

<VirtualHost *:80>
    ServerName mywebsite.com

    RewriteEngine on
    #RewriteCond %{SERVER_NAME} =www.mywebsite.com [OR]
    RewriteCond %{HTTPS} =mywebsite.com
    RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

 # forward ORDS requests to tomcat
 <VirtualHost *:443>
    ServerName mywebsite.com

    # SSL certificates settings
    #Include /etc/apache2/conf-enabled/options-ssl-apache.conf
    SSLCertificateFile /etc/apache2/ssl/mywebsite.com/fullchain.cer
    SSLCertificateKeyFile /etc/apache2/ssl/mywebsite.com/mywebsite.com.key
    SSLCertificateChainFile /etc/apache2/ssl/mywebsite.com/ca.cer

    ProxyRequests on
    ProxyPreserveHost On
    <Location / >
        ProxyPass "ajp://localhost:9090/"
        ProxyPassReverse "ajp://localhost:9090/"
    </Location>
</VirtualHost>

使用此配置,我将导航到默认的 apache2 主页,https: //mywebpage.com工作正常。为了自动从http://mywebsite.com重定向到https://mywebsite.com ,此配置有什么问题?

4

2 回答 2

1

为了处理这种情况,我的 http vhost 如下。我认为它也不需要启用任何新模块,只需像最后一行一样添加重定向语句:)

http-配置

于 2019-12-26T00:00:48.950 回答
0

由于您将 Ubuntu 与 Apache 一起使用。LetsEncrypt 自动安装并为 apache 配置 SSL。如果您想进行手动配置,请按照以下步骤操作。

文件name-xy.conf应如下所示:

<VirtualHost *:80>
    ServerName mywebsite.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ProxyRequests off
    ProxyPreserveHost On
    <Location / >
        ProxyPass "ajp://localhost:9090/"
        ProxyPassReverse "ajp://localhost:9090/"
    </Location>
    #Add the below Lines
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =mywebsite.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>

文件name-xy-ssl.conf应如下所示:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName mywebsite.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # SSL certificates settings
    #Include /etc/apache2/conf-enabled/options-ssl-apache.conf
    SSLCertificateFile /etc/apache2/ssl/mywebsite.com/fullchain.cer
    SSLCertificateKeyFile /etc/apache2/ssl/mywebsite.com/mywebsite.com.key
    SSLCertificateChainFile /etc/apache2/ssl/mywebsite.com/ca.cer

    ProxyRequests on
    ProxyPreserveHost On
    <Location / >
        ProxyPass "ajp://localhost:9090/"
        ProxyPassReverse "ajp://localhost:9090/"
    </Location>
</VirtualHost>
</IfModule>

重新启动您的 apache 服务器sudo service apache2 restart并清除浏览器缓存和历史记录以生效。

于 2019-12-28T10:55:07.757 回答