如何设置URL重写到Heroku实例?

我更像是一个工程师,而不是一个开发人员,所以请原谅,如果这是非常基本的。

我的情况:我在example.edunetworking上有一个服务器,所以我不控制example.edu的主DNS接口。 所以我不相信我可以使用DNSSimple或者Zerigo。 但是我在服务器上有root,它有以下两个主机名:

foo.example.edu bar.example.edu 

我的目标是设置Apache执行以下操作。

我如何去做这件事? 我想我应该使用Apache mod_rewrite,并编辑httpd.conf。 我刚开始看教程,但主要棘手的部分是,我没有完全控制域,并要确保我设置正确。

假设您已经拥有了所有必要的Apache VirtualHost指令,您基本上需要将以下内容添加到“bar.example.com”的端口80和端口443 VirtualHost指令中(select其中一个)

选项1:重写规则既然你表示你正在寻找一个重写规则,我会发布这个,但正如在下面的参考链接中指出的,这不是执行此操作的最佳方式。

 RewriteEngine on RewriteRule ^(.*)$ https://bar-example-edu.herokuapp.com/$1 [P] 

参考: http : //httpd.apache.org/docs/current/rewrite/flags.html#flag_p

选项2:直接使用mod_proxy

 ProxyPass / https://bar-example-edu.herokuapp.com/ ProxyPassReverse / https://bar-example-edu.herokuapp.com/ 

参考: http : //httpd.apache.org/docs/current/mod/mod_proxy.html#proxypass

考虑到heroku应用程序使用的是https(SSL),你应该把代理configuration放在端口443(ssl)的VirtualHost指令中,并且在80端口的VirtualHost中有以下行:

 Redirect / https://bar.example.edu/ 

在启动之前应该将访问者引导至https。

静态网站的Apacheconfiguration是直截了当的(我没有标准的不相关的行)

 <VirtualHost *:80> ServerName foo.example.edu DocumentRoot /var/www/foo.example.edu </VirtualHost> 

bar.example.edu的redirect可以遵循apache指南进行sslredirect 。 我会使用内置的redirect,因为mod_rewrite在这个简单的用例中是不需要的。

 <VirtualHost *:80> ServerName bar.example.edu Redirect permanent / https://bar.example.edu/ </VirtualHost> 

而为了redirect到Heroku,我会使用mod_proxy_http来redirect。 encryption的虚拟主机可以是类似的东西

 ProxyRequests Off <Proxy *> AddDefaultCharset off Order deny,allow Deny from none </Proxy> ProxyVia On <VirtualHost *:443> ServerName bar.example.edu SSLEngine On SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key ProxyPass / https://bar-example-edu.herokuapp.com/ ProxyPassReverse / https://bar-example-edu.herokuapp.com/ ProxyPassReverseCookieDomain / https://bar-example-edu.herokuapp.com/ </VirtualHost> 

在你的应用程序源代码Heroku一边,你将创build一个应用程序:

  heroku create bar-example-edu 

并configuration自定义域加载项

  heroku domains:add bar.example.edu 

从我写的有关代理 Heroku 的文档的一部分稍作修改。