使用 Catalyst 从 CDN 提供静态文件

发布于 2025-01-05 02:47:31 字数 620 浏览 2 评论 0原文

我正在尝试从 CDN 而不是 /static 文件夹提供所有静态文件(js、css、图像)。我开始编写自己的子程序来重写图像 URL:

sub uri_for_image { 
    my ( $c, $path, @args ) = @_; 
    my $uri = $c->uri_for($path, @args);

  if (MyApp->config->{use_cdn}) {
    my $cdn = MyApp->config->{cdn_location};
    $uri = $path;
    $uri =~ s/^/$cdn/si;
  } 

return $uri;
}

但后来我意识到需要做更多的工作才能自动重新路由所有静态文件。我应该如何为静态文件设置 CDN url?

我正在考虑配置 Static::Simple 但它似乎只有本地文件位置的设置。

I'm trying to serve all static files (js, css, images) from a CDN instead of the /static folder. I started writing my own sub to rewrite image URLs:

sub uri_for_image { 
    my ( $c, $path, @args ) = @_; 
    my $uri = $c->uri_for($path, @args);

  if (MyApp->config->{use_cdn}) {
    my $cdn = MyApp->config->{cdn_location};
    $uri = $path;
    $uri =~ s/^/$cdn/si;
  } 

return $uri;
}

But then I realized its gonna take a lot more work to get all the static files rerouted automatically. How should I go about setting a CDN url for static files?

I was looking into configuring Static::Simple but it seems to only have settings for location of files locally.

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

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

发布评论

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

评论(2

顾挽 2025-01-12 02:47:31

您认为 Catalyst 实际上可以并且应该为您做什么?

恐怕如果您想在应用程序之外生成自己的链接,那么您几乎只能靠自己了——Catalyst 无法真正做或提供任何东西来帮助您,不是吗?

或者您只是希望 uri_for 粉碎以 /static 开头的所有路径以指向您的 CDN?你可以只包装 uri_for 并让它做到这一点吗?

我认为你的版本(有一个单独的方法)更干净整洁,但如果你所有的静态内容都进入 CDN,那么只提供你想要的行为 uri_for 是合理的。

What do you think Catalyst actually can and should do for you here?

I'm afraid if you want to generate your own links which are outside the app, then you're pretty much on your own—there isn't anything Catalyst can really do or provide to help you here, is there?

Or do you just want uri_for to smash all paths starting in /static to point your CDN? As you can just wrap uri_for and make it do that?

I think your version (having a separate method) is cleaner and neater, but if all your static content is going onto the CDN, then just giving uri_for the behavior you want is reasonable.

傾旎 2025-01-12 02:47:31

2013 年晚些时候,有人编写了 Catalyst ::Plugin::ExternalURI 用于此目的。

从概要部分:

使用 Catalyst qw/...ExternalURI.../;

__包__->配置(
    外部uri => [
        # 将/static/css形式的url转换为其他域名开头 
        { '^/静态' => 'http://static.mydomain.com' },
        ...
        { '匹配' => '重写'}
        或者
        { 匹配 => '^/static',重写=> 'http://static.mydomain.com' },
    ]
);

Later in 2013 someone wrote the Catalyst::Plugin::ExternalURI for this purpose.

From the synopsis section:

use Catalyst qw/ ... ExternalURI ... /;

__PACKAGE__->config(
    externaluri => [
        # Converts urls with the form of /static/css to start with another domain name 
        { '^/static' => 'http://static.mydomain.com' },
        ...
        { 'MATCH' => 'REWRITE' }
        or
        { match => '^/static', rewrite => 'http://static.mydomain.com' },
    ]
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文