强制用户浏览器将文件保存到缓存
我的网站是一个带有在线播放系统的在线音乐门户。
当用户播放歌曲时,它由我服务器上的 8080 端口上的 nginx 服务器提供服务。
问题是当歌曲播放完毕后,播放器会重复播放该歌曲。但每次重播时,它都会再次调用服务器并开始再次下载文件进行播放。
以前我有 apache 来提供这些文件。当时,用户只请求一次文件,然后从他的缓存中播放重播。 我如何使用 nginx 实现这一点
[我没有使用任何脚本(如 php 等)来提供文件。这只是直接链接。就像 http://www.myserver.com:8080/music/song1.mp3]
My website is a online music portal with online playing system.
When user play a song, it is served by nginx server on port 8080 on my server.
The problem is when the song has finished playing, the player repeats the song. But every time it replays, it makes another call to server and starts downloading the file again to play.
Previously i had apache to serve these files. At that time, user just requested file once and replays were played from his cache.
How can i achieve this using nginx
[I am not using any script like php or so to serve the file. It's just direct link. Like http://www.myserver.com:8080/music/song1.mp3]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看一些以前的 Apache 配置和当前的 Nginx 配置,了解它们如何设置以在出站文件上放置过期标头,将会有很大帮助。您可能知道,这并不是一种万无一失的强制缓存方法,但听起来“足够好”对您有用。尝试在您的
server{}
块中使用 Nginx 配置块,其内容如下:这应该为您的文件设置一个遥远的到期日期,并鼓励浏览器将它们缓存更长时间。
It would help a lot to see some of your former Apache configuration and current Nginx configuration to see how they're set up to put expiration headers on outbound files. This isn't a foolproof way to enforce caching, as you may know, but it sounds like "good enough" will work for you. Try an Nginx configuration block within your
server{}
block which reads like this:That should set a far-future expiration date on your files and encourage the browser to cache them longer.