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

发布于 2024-11-08 20:15:09 字数 296 浏览 0 评论 0原文

我之前的问题中,我询问了多域解决方案,但问题太复杂了。

简而言之:

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

有什么想法吗?

In my previous question I asked about a multi-domain solution, but the question was too complex.

Now in short:

Is it possible to somehow setup name-based virtual hosts with Starman (or with any other pure perl PSGI server) like with Apache's <VirtualHost ...> directive? Or do I need to use Apache to get this kind of functionality?

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

战皆罪 2024-11-15 20:15:09

中间件已在 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 };
  };

The middleware is already done in Plack::Builder with Plack::App::URLMap. The pod saying:

Mapping URL with host names is also
possible, and in that case the URL
mapping works like a virtual host.

Syntax is in 3rd mount:

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

      mount "/bar" => $app2;
      mount "http://example.com/" => builder { $app3 };
  };
め七分饶幸 2024-11-15 20:15:09

这里是一个例子:一些网站的一个模块(应用程序)。

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

Here the example: one module (App) for some sites.

Your lib/YourApp.pm should be as:

    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;

Your bin/app.psgi should be as:

    #!/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;
    };

You can try a this my module - i think it will be more easy for virtual hosting than builder & mapping:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文