使用EC2/Route53设置子域名虚拟主机

5
我目前正在尝试通过Route53和EC2设置新的子域名,但每当我尝试通过浏览器访问它时,都会收到超时错误。亚马逊安全组已经设置允许端口80上的所有流量。
我编写了以下vhosts文件,并将其存储在sites-available下,命名为“new.mysite.com.conf”:
<VirtualHost *:80>
    ServerName new.mysite.com
    RewriteEngine On
    #RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    DocumentRoot /var/www/newmysite
        <Directory /var/www/newmysite>
                DirectoryIndex index.php
                AllowOverride All
        </Directory>
</VirtualHost>

我已启用此运行;
a2ensite new.mysite.com.conf
service apache2 reload

最后,在route53下,我设置了一个新的A记录,设置为非别名,并将其指向运行实例中公共IP地址的EC2服务器。
我已确保在var/www/newmysite/index.php下有一个测试页面,并创建了一个备份var/www/newmysite/index.html以供访问。
子域名本身只显示超时。 直接命中服务器的公共IP会显示默认的apache2“it works!”页面。
是否有人有任何想法,我在这里可能做错了什么?

1
请问您在A记录中指定了哪个域名? - Sanjeev Sachdev
2个回答

4

存在一个从HTTP到HTTPS的重定向规则。

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我猜当你访问new.mysite.com时,它会重定向到HTTPs https://new.mysite.com,因为没有启用SSL的VirtualHost(443)且443端口上没有监听器,所以连接被拒绝了。
尝试禁用重定向规则,或者您可以尝试使用自签名证书或配置ELB使用ACM证书并将Route53域指向ELB。
curl -v -L http://new.mysite.com
* Rebuilt URL to: http://new.mysite.com/
*   Trying 10.116.131.69...
* Connected to new.mysite.com (10.116.131.69) port 80 (#0)
> GET / HTTP/1.1
> Host: new.mysite.com
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
* Issue another request to this URL: 'https://new.mysite.com/'
* Found bundle for host new.mysite.com: 0x7ff7a3c0fbc0
*   Trying 10.116.131.69...
* connect to 10.116.131.69 port 443 failed: Connection refused
* Failed to connect to new.mysite.com port 443: Connection refused
* Closing connection 1
curl: (7) Failed to connect to new.mysite.com port 443: Connection refused

1

就像之前的答案中提到的那样,有一个SSL重定向。 SSL使用端口443,该端口在您的安全组中未公开。

  1. 修改安全组以允许通过端口443的所有流量。这将启用通过https访问网站。
  2. 安装和配置SSL证书用于域名。您可以从Let's Encrypt安装免费证书。按照以下步骤操作:
    • 安装Certbot,一个可以在90天到期之前自动更新您的证书的机器人
      • $ sudo add-apt-repository ppa:certbot/certbot
      • $ sudo apt-get update
      • sudo apt-get install python-certbot-apache
    • 为您的子域配置新证书
      • sudo certbot --apache -d new.mysite.com

现在您的网站应该在https上运行。

顺便提一下,在写这个答案的时候,我注意到你已经关闭了 http 跳转到 https,在启用网站的 https 后,你可能需要重新开启它。这样可以确保所有与你的网站的通信都是安全的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接