【问题标题】:Configuring multiple websites on a single Apache instance在单个 Apache 实例上配置多个网站
【发布时间】:2015-04-23 19:40:31
【问题描述】:

我正在寻找一种在单个服务器上托管多个网站的方法。目前我已经使用反向代理来托管两个网站,方法如下:

我在 /var/www/html 文件夹中有一个 php 站点,并且在 localhost:3015 上运行了一个 nodejs 应用程序。我的 apache2 配置如下:-

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

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

<VirtualHost *:80>
        ProxyPreserveHost On
        ProxyPass / http://localhost:3015/
        ProxyPassReverse / http://localhost:3015/
        ServerName site2.example.com
</VirtualHost>

现在我还想在以下位置托管几个旧网站:

site1.example.com/archives/2014、13 等

site2.example.com/archives/2014、13 等等。

在 site1.example.com 的情况下,我可以使用别名,因为托管的站点是 php 站点。 在我使用反向代理的第二种情况下,托管 php 站点的最佳方式是什么。

另外,请提出一种可以轻松添加新站点并将旧站点移动到存档文件夹的方法。这些站点可能在 django、ROR 等。

这可能吗?

【问题讨论】:

  • 您应该可以只复制代理节,更改名称和端口...
  • 我不能将 site1.example.com/archive/2014 作为服务器名称。是否有任何其他选项可以与代理一起使用以将 url 映射到另一个端口。
  • 试试 ProxyPass / localhost:<port>/archive/2014 和 ProxyPassReverse / localhost:<port>/archive/2014 我还没有这样做,但它可能会起作用

标签: apache


【解决方案1】:

这可能不是您所要求的,但请尝试一下。这是我在开发 VM 中使用的 Apache 配置的一种变体,它是为通配符域托管而设置的。我没有专门测试过这个配置,但你应该可以调整它以满足你的需要。

这实质上是告诉 Apache 如何查找以下网站:

  • site1.example.com =&gt; /var/www/html/site1/public_html
  • site2.example.com =&gt; /var/www/html/site1/public_html
  • 2014.archive.site1.example.com =&gt; /var/www/html/site1/public_html/archives/2014

    <VirtualHost *:80>
        ServerName example.com
        ServerAlias *.example.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
    
        <Directory />
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>
    
        <Directory /var/www/html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>
    
        DirectoryIndex index.html index.php
    
        RewriteEngine on
    
        RewriteMap lowercase int:tolower
    
        # *.example.com
        RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-zA-Z0-9-]+\.example\.com$
        RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
        RewriteRule ^([a-zA-Z0-9-]+)\.example\.com/(.*) /var/www/html/$1/public_html/$2 
    
        # *.archive.*.example.com
        RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-zA-Z0-9-]+\.archive\.[a-zA-Z0-9-]+\.example\.com$
        RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
        RewriteRule ^([a-zA-Z0-9-]+)\.archive\.([a-zA-Z0-9-]+)\.example\.com/(.*) /var/www/html/$2/public_html/archives/$1/$3
    </VirtualHost>
    

如果这不是您想要的,请告诉我。

【讨论】:

  • 我面临的问题是这些网站是在不同的平台上制作的。例如,node.js 应用程序在 localhost:3015 上运行。现在,任何来自 site2.example.com 的请求都会转到 localhost:3015。如何通过 apache 确保 site2.example.com/archive/2014 附带的请求应该转到其他地方而不是 localhost:3015
【解决方案2】:

我终于想出了一个方法来做同样的事情。您需要做的是将所有站点托管在不同的端口:

site1 => 3015
site2 => 4015

site3 => 3014
site4 => 4014

and so on.

现在您可以在 apache2 中配置您的 default.conf,如下所示:

<VirtualHost *:80>    
        ProxyPreserveHost On

        ProxyPass /archive/2014/ http://localhost:3014/
        ProxyPassReverse /archive/2014/ http://localhost:3014/

        ProxyPass / http://localhost:3015/
        ProxyPassReverse / http://localhost:3015/

        ServerName site1.example.com    
</VirtualHost>
<VirtualHost *:80>
        ProxyPreserveHost On

        ProxyPass /archive/2014/ http://localhost:4014/
        ProxyPassReverse /archive/2014/ http://localhost:4014/

        ProxyPass / http://localhost:4015/
        ProxyPassReverse / http://localhost:4015/

        ServerName site2.example.com
</VirtualHost>

这适用于任何平台上的网站,目前我的 site3 和 site4 是 php,site1 和 site2 分别是 node 和 django。您可能需要稍微使用 url 和链接以使一切正常运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2010-09-09
    相关资源
    最近更新 更多