系统
Linux hosek 4.15.0-48-generic #51-Ubuntu SMP Wed Apr 3 08:28:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
问题
我应该如何使用 ssl 和重定向在 Apache 中使用单个配置文件配置多个虚拟主机?
在我下面的配置中需要什么和不需要什么?例如可以设置Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/hosek/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/hosek/privkey.pem
为仅文件开头吗?对于所有vhost
s?
是否可以对一个文件进行整个配置,尤其是一个VirtualHost
?我现在有 2 个文件,一个用于80
,第二个用于443
.
我vhost
的例子。
no-ssl.conf
文件。
<VirtualHost *:80>
ServerName www.thehatmakers.cz
ServerAlias thehatmakers.cz
RewriteCond %{HTTP_HOST} ^(thehatmakers.cz) [NC]
RewriteRule ^(.*)$ http://www.thehatmakers.cz$1 [R=301,L]
RewriteCond %{SERVER_NAME} =www.thehatmakers.cz
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerName www.obrazduse.cz
ServerAlias obrazduse.cz
RewriteCond %{HTTP_HOST} ^(obrazduse.cz) [NC]
RewriteRule ^(.*)$ http://www.obrazduse.cz$1 [R=301,L]
RewriteCond %{SERVER_NAME} =www.obrazduse.cz
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
ssl.conf
文件。
<VirtualHost *:443>
ServerName www.thehatmakers.cz
ServerAlias thehatmakers.cz
RewriteCond %{HTTP_HOST} ^(thehatmakers.cz) [NC]
RewriteRule ^(.*)$ http://www.thehatmakers.cz$1 [R=301,L]
DocumentRoot /var/www/html/thehatmakers
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hosek/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hosek/privkey.pem
</VirtualHost>
<VirtualHost *:443>
ServerName www.obrazduse.cz
ServerAlias obrazduse.cz
RewriteCond %{HTTP_HOST} ^(obrazduse.cz) [NC]
RewriteRule ^(.*)$ http://www.obrazduse.cz$1 [R=301,L]
DocumentRoot /var/www/html/obrazduse
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hosek/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hosek/privkey.pem
</VirtualHost>
谢谢。
更新
如果不可能用 1 个 VirtualHost,那么这个配置呢?有没有更短的方法来做到这一点?可以Redirect
用于ssl吗?正如我评论的*:443
配置?我可以使用Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/hosek/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/hosek/privkey.pem
外部VirtualHost
配置吗?那么谷歌呢,这个重定向可以吗?我正在为所有域使用 1 个证书,可以吗?
<VirtualHost *:80>
ServerName www.thehatmakers.cz
ServerAlias thehatmakers.cz
Redirect / https://www.thehatmakers.cz
</VirtualHost>
<VirtualHost *:443>
ServerName www.thehatmakers.cz
ServerAlias thehatmakers.cz
#Redirect / https://www.thehatmakers.cz
RewriteCond %{HTTP_HOST} ^(thehatmakers.cz) [NC]
RewriteRule ^(.*)$ https://www.thehatmakers.cz$1 [R=301,L]
DocumentRoot /var/www/html/thehatmakers
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hosek/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hosek/privkey.pem
</VirtualHost>
谢谢。
除了这里建议的重复之外,很少有针对此问题的答案:
如何
ServerAlias
在使用中设置 HTTPS 虚拟主机。
如果您ServerAlias
在 HTTPS/SSL 虚拟主机中使用指令,则需要为所有域名颁发证书。通过使用,letsencrypt
您需要添加几个-d
选项:
sudo letsencrypt --apache .... -d www.example.com -d example.com
所有证书都将放在同一个证书文件中。
是否可以对一个文件进行整个配置,尤其是一个 VirtualHost?我现在有 2 个文件,一个是 80,第二个是 443。
您可以将所有 VirtualHost 的定义放在一个文件中,因此可以轻松地同时启用和禁用所有这些。但是没有办法配置一个 VirtualHost 来监听两个端口。
怎么样
Redirect
,而不是Rewrite
在SSL配置?
根据 Apache2 对这种情况的文档,最好使用Redirect
指令而不是Rewrite
规则。请注意,如果要重定向https://example.com
到https://www.example.com
. 所有相关的 VirtualHosts 可以使用相同的证书文件,以上述方式生成。
每个虚拟主机将负责不同的ServerName
,例如:ServerName example.com
用于第一个,分别ServerName www.example.com
用于第二个等。注意ServerAlias
必须删除该指令。
如果一切都按预期进行,您可以继续使用Rewrite
规则 - 这取决于您的决定。如果您正在使用Redirect
指令,请不要错过目标域名末尾的斜线!这是使用Redirect
指令的HTTPS VirtualHost 的示例。
<VirtualHost *:443>
ServerName thehatmakers.cz
Redirect permanent "/" "https://www.thehatmakers.cz/"
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hosek/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hosek/privkey.pem
</VirtualHost>
该关键字permanent
将指示客户端浏览器下次自动执行此重定向:
Redirect
== HTTP 300
Redirect permanent
== HTTP 301
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句