Apache在同一端口下配置HTTP到https重定向

时间:2019-01-20 12:25:07

标签: apache https virtualhost

我的目标是使用已启用SSL的已定义端口(例如123),并且“ http://example.com:123”应重定向到“ https://example.com:123”。

我使用Node.JS服务器成功完成了此操作,但未成功使用Apache服务器。

这是我在httpd.conf中的代码:

Listen 123
<VirtualHost *:123>
    ServerName example.com

    SSLEngine on
    SSLCertificateFile "/etc/ssl/crt/certificate.crt"
    SSLCertificateKeyFile "/etc/ssl/crt/private.key"
    SSLCertificateChainFile "/etc/ssl/crt/ca_bundle.crt"

</VirtualHost>

这适用于https://example.com:123, 但是当我尝试http://example.com:123时,它会显示

Bad Request
Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.

然后我也尝试了这个:

Listen 123
<VirtualHost *:123>
    ServerName example.com

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

</VirtualHost>

成功将http://example.com:123/whatever重定向到https://example.com:123/whatever

但是当我把这两个东西放在一起时,或者反过来:

Listen 123
<VirtualHost *:123>
    ServerName example.com

    SSLEngine on
    SSLCertificateFile "/etc/ssl/crt/certificate.crt"
    SSLCertificateKeyFile "/etc/ssl/crt/private.key"
    SSLCertificateChainFile "/etc/ssl/crt/ca_bundle.crt"

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

以先到者为准,但不能一起工作(即,将http重定向到https并启用SSL)

我如何才能使它们一起工作?谢谢。

0 个答案:

没有答案