【问题标题】:How do I host multiple MVC3 sites on a single virtual host running Apache2?如何在运行 Apache2 的单个虚拟主机上托管多个 MVC3 站点?
【发布时间】:2011-06-08 15:17:37
【问题描述】:

我正在尝试在 OSX 上使用 Apache2 配置 mod_mono。我想在同一个虚拟主机上运行多个 MVC3 项目,但由于某种原因,只有列出的第一个项目在工作。对此的任何帮助将不胜感激,因为没有太多关于此的文档。我尝试了很多不同的配置选项,但似乎都不起作用。

Listen *:9005
<VirtualHost *:9005>
  DocumentRoot "/Library/WebServer/vhosts/api"
  ServerName api
  MonoAutoApplication disabled

  Alias /gamecenter "/Library/WebServer/vhosts/api/gamecenter"
  AddMonoApplications gamecenter "/gamecenter:/Library/WebServer/vhosts/api/gamecenter"
  MonoServerPath gamecenter "/usr/bin/mod-mono-server4"
  MonoDebug gamecenter true
  MonoSetEnv gamecenter MONO_IOMAP=all
  MonoUnixSocket gamecenter-stage /tmp/mod_mono_server_gc
  <Location /gamecenter>
    Allow from all
    Order allow,deny
    MonoSetServerAlias gamecenter
    SetHandler mono
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
  </Location>

  Alias /gamecenter-stage "/Library/WebServer/vhosts/api/gamecenter-stage"
  MonoServerPath gamecenter-stage "/usr/bin/mod-mono-server4"
  MonoDebug gamecenter-stage true
  MonoSetEnv gamecenter-stage MONO_IOMAP=all
  AddMonoApplications gamecenter-stage "/gamecenter-stage:/Library/WebServer/vhosts/api/gamecenter-stage"
  MonoUnixSocket gamecenter-stage /tmp/mod_mono_server_gcs
  <Location /gamecenter-stage>
    Allow from all
    Order allow,deny
    MonoSetServerAlias gamecenter-stage
    SetHandler mono
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
  </Location>

  <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
  </IfModule>
</VirtualHost>

【问题讨论】:

  • 您好,您已经解决了这个问题吗?我也有同样的问题。
  • 还没有。已经尝试了我能找到的所有配置,但似乎都没有解决这个问题。不过还是看看吧。
  • 如果您要否决并编辑我的问题,请确保您首先知道问题是什么。
  • @chris,您能否将 Locations 更改为 Directory 指令并进行测试?

标签: apache asp.net-mvc-3 mono mod-mono osx-snow-leopard


【解决方案1】:

你的问题是你的别名和物理路径是一回事,所以 apache 不知道要提供哪一个。

注意:我是根据一般 Apache2 配置给出答案,而不是根据 mod_mono,也许 mod_mono 做了一些事情来防止这种情况发生,我之前没有在 *nix 框下设置 MVC 应用程序:-)

无论如何...

如果您查看您的路径配置...

/Library/WebServer/vhosts/api
/Library/WebServer/vhosts/api/gamecenter
/Library/WebServer/vhosts/api/gamecenter-stage

如果没有您的别名,这些已经解析为您尝试映射的路径。

/Library/WebServer/vhosts/api  = /
/Library/WebServer/vhosts/api/gamecenter  = /gamecenter
/Library/WebServer/vhosts/api/gamecenter-stage  = /gamecenter-stage

然后你告诉 Apache

/ = /
/gamecenter = /gamecenter
/gamecenter-stage = /gamecenter-stage

当 Apache 尝试传递内容时,如果没有文件子前缀或现有斜杠(如最后 2 个),它会自动使用 / 为文件夹添加子前缀,然后发出重定向(我相信是 306)基本上告诉浏览器从 EG 重定向:

/gamecenter to /gamecenter/

有了别名,告诉它 Alias ... 位于位置 x,然后它必须尝试做出服务的决定

/gamecenter/

/gamecenter/gamecenter/../ (Because in terms of folder structure the alias name is 1 folder level down in the web than it is physically)

最终会感到困惑,任何设置的虚拟主机在无法解析路径时所做的事情也是如此,那就是返回网站根目录。

然而,正如我所说,这是一般的 NON-MONO Apache 行为,mod_mono 可能会以某种方式改变处理管道,从而可能会改变这种行为。

我建议将其拆分为 3 个虚拟主机,即使仅在一个 IP 上也可以非常轻松地完成。

你要做的第一件事是在你的主 Apache 配置文件的某个地方,有一个

Listen 9005

声明。这将使所有虚拟实例在该端口以及任何其他配置的端口 EG 上侦听:80

接下来确保你有一个默认的 catch all 虚拟主机,这将捕获任何未映射到其他地方的服务器名称:

<VirtualHost *>
  DocumentRoot "/some/folder/where/the/default/is/"
  #Followed by other server directives. NOTE: there is NO servername line
</VirtualHost>

设置完成后,进入“api”子域

<VirtualHost *>
  ServerName api
  DocumentRoot "/Library/WebServer/vhosts/api/"
  #Other required directives here
</VirtualHost>

此时,我将暂停讨论您的域名。如果这是一个内部测试系统(我怀疑它是),那么如果您在盒子上安装 DNS 服务器,然后使用私有内部网络地址将其设置为主域,您会发现虚拟域的生活会更容易。

EG:

创建一个根区域,并将其命名为“mydevnetwork.local”

然后添加机器名称:

EG:如果你的电脑叫做devpc1,为“devpc1.mydevnetwork.local”创建一个IP地址,并给你的电脑一个静态IP地址EG:192.168.50.1

然后为它设置一个别名

api.mydevnetwork.local = devpc1.mydevnetwork.local

我没有足够的空间在此处发布完整的 DNS 设置帖子,但希望您能理解。

一旦您设置了 DNS(或至少主机文件条目),那么您在 Apache 下的虚拟主机变得非常易于管理:

<VirtualHost *>
  ServerName api.mydevnetwork.local
  DocumentRoot "/Library/WebServer/vhosts/api/"
  #Other required directives here
</VirtualHost>

如果您也需要,也可以轻松迁移到另一台机器上。

您可以以几乎相同的方式设置其余的虚拟主机

<VirtualHost *>
  ServerName gamecenter.mydevnetwork.local
  DocumentRoot "/Library/WebServer/vhosts/api/gamecenter/"
  #Other required directives here
</VirtualHost>

<VirtualHost *>
  ServerName gamecenter-stage.mydevnetwork.local
  DocumentRoot "/Library/WebServer/vhosts/api/gamecenter-stage/"
  #Other required directives here
</VirtualHost>

请注意,我将路径设置为与您上面的相同,即使这样可行,我强烈建议您为每个人提供自己独特的文件夹,我通常会这样做:

wwwroot
    api.mydevnetwork.local
        htdocs   <-- Web files go here
        cgi-bin  <-- cgi scripts go here and it's mapped to /cgi-bin/
        logs     <-- logs here
        access   <-- htpasswd files here

希望如果以上不是一个完整的解决方案,您至少可以从中获得一些进一步的调查想法。

【讨论】:

  • 我已经放弃了找到解决方案的希望。我一定会试一试并报告。谢谢。
  • @chris,这对你有用吗?我遇到的问题是,当我设置多个虚拟主机时,会产生多个 mod-mono-server4 实例。我希望能够从同一个 mod-mono-server4 实例运行多个 MVC 应用程序,但我无法弄清楚如何。这和你的问题相似吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 2023-03-28
  • 2014-12-26
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 2012-05-10
相关资源
最近更新 更多