如何使用Starman设置类似Apache的基于名称的虚拟主机

时间:2011-05-18 13:22:17

标签: perl plack psgi

my previous question我询问了多域解决方案,但问题太复杂了。

现在简而言之:

是否有可能以某种方式设置基于名称的虚拟主机与Starman(或任何其他纯粹的perl PSGI服务器),如Apache的<VirtualHost ...>指令?或者我是否需要使用Apache来获得这种功能?

有什么想法吗?

2 个答案:

答案 0 :(得分:10)

中间件已在Plack::Builder Plack::App::URLMap中完成。豆荚说:

  

使用主机名映射URL也是   可能,在这种情况下是URL   映射就像虚拟主机一样。

语法处于第3次安装:

 builder {
      mount "/foo" => builder {
          enable "Plack::Middleware::Foo";
          $app;
      };

      mount "/bar" => $app2;
      mount "http://example.com/" => builder { $app3 };
  };

答案 1 :(得分:1)

这里的示例:某些网站的一个模块(App)。

你的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;
    };

你可以试试这个我的模块 - 我认为虚拟主机比构建器更容易映射:

https://github.com/Perlover/Dancer-Plugin-Hosts