如何在运行Apache2的单个虚拟主机上托管多个MVC3站点?

pmk*_*mko 21 apache mono mod-mono osx-snow-leopard asp.net-mvc-3

我正在尝试在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>
Run Code Online (Sandbox Code Playgroud)

sha*_*wty 3

您的问题是您的别名和物理路径是相同的,因此 apache 不知道要提供哪一个。

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

反正...

如果你查看你的路径配置,你有......

/Library/WebServer/vhosts/api
/Library/WebServer/vhosts/api/gamecenter
/Library/WebServer/vhosts/api/gamecenter-stage
Run Code Online (Sandbox Code Playgroud)

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

/Library/WebServer/vhosts/api  = /
/Library/WebServer/vhosts/api/gamecenter  = /gamecenter
/Library/WebServer/vhosts/api/gamecenter-stage  = /gamecenter-stage
Run Code Online (Sandbox Code Playgroud)

然后你告诉阿帕奇

/ = /
/gamecenter = /gamecenter
/gamecenter-stage = /gamecenter-stage
Run Code Online (Sandbox Code Playgroud)

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

/gamecenter to /gamecenter/
Run Code Online (Sandbox Code Playgroud)

有了别名就可以告诉它 Alias ... 位于位置 x,然后它必须尝试做出决定来提供服务

/gamecenter/
Run Code Online (Sandbox Code Playgroud)

或者

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

最终会变得混乱,任何虚拟主机设置在无法解析路径时都会执行的操作,即返回网站根目录。

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

我建议将其分成 3 个虚拟主机,即使只有一个 IP,您也可以非常轻松地完成此操作。

您要做的第一件事是在您的主 Apache 配置文件中的某个位置,有一个

Listen 9005
Run Code Online (Sandbox Code Playgroud)

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

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

<VirtualHost *>
  DocumentRoot "/some/folder/where/the/default/is/"
  #Followed by other server directives. NOTE: there is NO servername line
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

设置完成后,然后转到您的“api”子域

<VirtualHost *>
  ServerName api
  DocumentRoot "/Library/WebServer/vhosts/api/"
  #Other required directives here
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

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

例如:

创建根区域,并将其命名为“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>
Run Code Online (Sandbox Code Playgroud)

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,将路径设置为与上面相同,即使这可行,我强烈建议您为每个路径提供自己唯一的文件夹,我通常会执行以下操作:

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
Run Code Online (Sandbox Code Playgroud)

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