在我之前的问题中,我询问了多域解决方案,但问题太复杂了。
现在简而言之:
是否可以像使用 Apache 的指令一样使用 Starman(或任何其他纯 perl PSGI 服务器)设置基于名称的虚拟主机?<VirtualHost ...>
还是我需要使用 Apache 来获得这种功能?
任何的想法?
中间件已经在Plack::Builder中使用Plack::App::URLMap完成。豆荚说:
也可以使用主机名映射 URL,在这种情况下,URL 映射就像虚拟主机一样工作。
语法在第三个挂载中:
builder {
mount "/foo" => builder {
enable "Plack::Middleware::Foo";
$app;
};
mount "/bar" => $app2;
mount "http://example.com/" => builder { $app3 };
};
这里的例子:一些网站的一个模块(应用程序)。
你的 lib/YourApp.pm 应该是:
package YourApp;
use strict;
use warnings;
use Dancer ':syntax';
setting apphandler => 'PSGI';
Dancer::App->set_running_app('YourApp');
# This and other routes ...
get '/' => sub {
# Static and template files will be from different directories are
# based by host http header
template 'index';
};
1;
你的 bin/app.psgi 应该是:
#!/usr/bin/perl
use strict;
use warnings;
use Dancer;
# The next line can miss but need for quickly loading in L<Starman> server
use YourApp;
use Plack::Builder;
# Please notice that here no need ports in url
# So for http://app1.foo.com:3000/ will work
# http://app1.foo.com/
my $hosts = {
'http://app1.foo.com/' => '/appdir/1',
'http://app2.foo.com/' => '/appdir/2'
};
builder {
my $last;
foreach my $host (keys %$hosts) {
$last = mount $host => sub {
my $env = shift;
local $ENV{DANCER_APPDIR} = $hosts->{$host};
load_app "YourApp";
Dancer::App->set_running_app('YourApp');
setting appdir => $hosts->{$host};
Dancer::Config->load;
my $request = Dancer::Request->new( env => $env );
Dancer->dance($request);
};
}
$last;
};
你可以试试这个我的模块 - 我认为虚拟主机比构建器和映射更容易: