Apache 服务器端文件通过 .htaccess 缓存?

发布于 2024-08-31 02:49:06 字数 375 浏览 3 评论 0原文

  1. 我正在启动新网站,并将包含几个 JS 库,并且想知道 .htaccess 文件模板在缓存媒体和 JS 文件的情况下应该是什么样子?

  2. GZipDeflate 哪个压缩效果更好?

  3. 是否可以更好/更快地从 Google CDN 上提供这些 JS 库,然后在本地提供这些 JS 库?

我问 CDN 问题,因为 GoogleCDN 提供的一些脚本可能会更新并最终破坏网站布局,所以我认为最好在本地托管它们并通过网络服务器缓存(如果它可以与相同/接近的文件一起使用) -相同的速度。

  1. I'm starting new website and gonna include several JS libs and would like to know how .htaccess file template should look like with caching of media and JS files on?

  2. Whats better for compression, GZip or Deflate?

  3. Is it better/faster solution to serve those JS libs off the Google CDN perhaps then locally?

I'm asking CDN question since some of scripts served off GoogleCDN are potentially going to update and eventually break the website layout so i thought it would be better for me to host them locally and cache via webserver if its going to work with same/near-same speed.

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

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

发布评论

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

评论(1

待"谢繁草 2024-09-07 02:49:06

(抱歉,代表太低,无法发表评论)

在包含多个 js 文件方面,重​​新构建网站时应该记住一些事情。首先,每个文件都是一个请求,如果您提供它们,将会给您的服务器带来一些开销。即使在 CDN 上,它仍然是客户端需要发出的请求。如果你的网站设置了一个cookie,那么每个请求都会因此产生更多的开销。我提到这一点是因为你的缓存,因为你要求重新 CDN。我的意思是,您应该首先考虑从哪里提供内容,然后再进行缓存。就我个人而言,我设置了第二个子网址来提供此类内容,即 static.website.com。这样,内容在请求中的开销就会减少,并且您可以利用并行下载。那么关于 Google CDN,是的,如果您拥有大部分内容,那肯定是这样。如果您担心它发生变化或者只想在本地托管它,那么我建议您为此目的设置一个无 cookie 的域。

这让我想到了缓存和压缩。如果您有许多文件,那么您可能需要考虑将它们全部合并为一个并提供该文件。我所做的就是获取我们使用的 15 个左右的 js 文件,将它们全部放入一个文件中并对其进行哈希处理,并将哈希值作为文件名。 (注意,这是我为部署之前的构建步骤编写的一个小东西)。这样,如果任何文件发生更改,则哈希值会发生更改,您将立即提供新内容。将 js 文件缓存一年的危险在于,您无法控制客户端计算机来获取新文件,除非您更改它的名称。这些都是重新设置缓存、cdn 和多个 js 文件时需要记住的事情。

就我个人而言,就像我所说的,我将所有 js 文件合并为一个,然后将它们压缩到一个 min 文件(ajax minifier),并使用它。我对 css 文件做同样的事情。它位于子域上并在那里提供服务。

在 apache 中,我有以下内容:

# Enable memory caching
CacheEnable mem /
# Limit the size of the cache to 50 Megabyte
MCacheSize 51200
AddOutputFilterByType DEFLATE application/x-javascript
ExpiresActive On
ExpiresDefault "access plus 1 second"
# hashed file so safe for long out cache
ExpiresByType text/javascript "access plus 1 years"

随着垃圾负载更多...但你明白了

最后一点,如果你正在构建一个移动网站,你真的必须记住关键的事情是保持较低的带宽成本。这对答案有何影响?对于我的移动网站,我只有 2-3 个自定义 js 文件,我以相同的方式提供这些文件,但对于 jquery-mobile 网站,我从官方存储库提供这些文件。为什么?因为客户端的移动设备已经缓存了它,因此可以为他们节省带宽。对于桌面我不太关心,但这在移动世界中产生了很大的不同。

我希望这有帮助,如果您需要更多信息,请告诉我,

干杯

罗宾

(Sorry rep is too low to post comment)

In terms of including several js files there are some things that you should keep in mind re building the site. First, each of those files is a request that will have some overhead on your server if you serve them. Even on a cdn it will still be a request that the client needs to make. If you have your site setup with say a cookie then every request will have a bit more overhead because of it. I mention this because of your caching because you asked re CDN. What I mean is you should consider first where you are going to serve the content from and then caching. Personally I have setup a second sub url to serve this type of content, so static.website.com. This way the content has less overhead in the request and you can take advantage of parellel downloads. So re the Google CDN, yeah sure if you have most stuff there. If you are afraid of it changing or want to just host it locally then I would recommend you setup a cookie free domain for that purpose.

That brings me to caching and compression. If you have many files then you might want to consider combining them all into one and serve that up. What I do is get the 15 or so js files that we use, put them all into one file and hash it and make the hash the file name. (Note, this is a small thing I wrote for the build step before deploy). This way if any of the files change then the hash changes and you will serve the new content straight away. Your danager of caching the js files for say a year is that you have no control re the client machine to get the new file, unless you change the name of it. Those are sort of things to keep in mind re setting up caching and cdn and multiple js files

Personally, like I said, I combine all js files into one and then compress them to a min file (ajax minifier), and use that. I do the same for css files. This sits on a sub domain and gets served there.

In apache I have the following:

# Enable memory caching
CacheEnable mem /
# Limit the size of the cache to 50 Megabyte
MCacheSize 51200
AddOutputFilterByType DEFLATE application/x-javascript
ExpiresActive On
ExpiresDefault "access plus 1 second"
# hashed file so safe for long out cache
ExpiresByType text/javascript "access plus 1 years"

Along with a crap load more... but you get the idea

Final note, if you are building a mobile site, you really must remember the key thing being keep bandwidth cost low. How does this affect the answer? Well for my mobile site, I only have 2-3 custom js files which I serve in the same way, but for the jquery-mobile ones I serve this from the offical repo. Why? because the client's mobile will already have it cached and hence it saves bandwidth for them. For a desktop I don't care as much but this makes a big difference in the mobile world.

I hope this help, let me know if you need more info on something,

Cheers

Robin

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