How do I host multiple MVC3 sites on a single virt

2019-02-11 19:05发布

问题:

I'm trying to configure mod_mono with Apache2 on OSX. I would like to run multiple MVC3 projects on the same virtual host, but for some reason only the first one listed is working. Any help on this would be much appreciated as there is not much documentation on this. I've tried a lot of different config options, none of which seem to work.

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>

回答1:

your problem is that your Alias name and physical path are one and the same, so apache doesn't know which one to serve up.

NOTE: I'm giving the answer based on general Apache2 configuration, and not on mod_mono, maybe mod_mono does something to prevent this, I've not set MVC apps up under a *nix box before :-)

Anyway...

if you look at your path configurations you have...

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

without your aliases in place, these already resolve to the paths your trying to map.

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

Your then telling Apache that

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

When Apache tries to deliver the content if there is no file subfix or existing slash (as in the last 2) it will automatically, subfix the folder with a / then issue a redirect (306 I believe) essentially telling the browser to redirect from EG:

/gamecenter to /gamecenter/

With the alias in place to tell it that Alias ... is at location x it then has to try and make a desicion to serve

/gamecenter/

or

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

and ends up getting confused, and so does what any virtual host set up does when it's unable to resolve the path, and that's return the website root.

AS I SAY however, this is general NON-MONO Apache behaviour, it is possible that mod_mono may alter the processing pipeline in some way to that may change this behaviour.

What I would recommend is to split this into 3 virtual hosts which you can do very very easily even on just one IP.

First thing you'll want to do is somwhere in your master Apache config file, have a

Listen 9005

statement. This will make ALL virtual instances listen on that port as well as any other configured port EG: 80

Next make sure you have a default catch all virtual host, this will catch any server name not mapped elsewhere:

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

Once you have that set up, then move onto your "api" sub domain

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

At this point, I'm going to pause to discuss your domain name. If this is an internal test system (Which I suspect it is) then you'll find life with virtual domains way easier if you install a DNS server on you box, then set that up as a master domain using a private internal network address.

EG:

Create a root zone, and call it "mydevnetwork.local"

then add machine names to it:

EG: if your pc is called devpc1, create an IP address for "devpc1.mydevnetwork.local" and give your pc a static IP address of EG: 192.168.50.1

Then set an alias for that so

api.mydevnetwork.local = devpc1.mydevnetwork.local

Iv'e not got the room to do a full DNS setup post here, but hopefully you get the idea.

Once you have DNS (or at a minimum host file entries) set up, then your virtual hosts under Apache become really easy to manage:

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

and easy to relocate to another machine should you need too.

You can set the rest of your virtual hosts up in much the same way

<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>

Note iv'e set the paths to be the same as you had above, and even though this will work, I'd strongly advise that you give each one it's own unique folder, I generally do something like:

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

Hopefully if the above is not a complete solution, you might at least get some further ideas of investigation from it.