将图像请求路由到单独的子域
首先,一些背景信息:
大约 1999 年的 HTTP 1.1 规范建议浏览器和服务器将对同一主机名的并行请求限制为两个。 (更多)
如果您继续阅读该文章作者建议通过让多个子域都指向同一事物来“欺骗”浏览器。
如果我要从两个独立的子域(两个不同的主机名)提供图像,那么浏览器将最多并行下载 4 个图像(每个主机名 2 个)。
鉴于此,我现在可以在两个子域之间平均分配请求以优化页面下载速度,如下所示:
<img src="http://subdomain1.example.com/img1.jpg" />
<img src="http://subdomain2.example.com/img2.jpg" />
<img src="http://subdomain1.example.com/img3.jpg" />
<img src="http://subdomain2.example.com/img4.jpg" />
这将需要我手动浏览适当的文件并更改每个图像的“src”。
我正在寻找一种更简单/可重用的解决方案,不涉及对 HTML 的可见更改。
我有一个想法:
- [example.com] 上的所有类似图像的 URL 都被重定向(通过 .htaccess)到 [example.com/imghandler.php]
- imghandler.php 重定向到 subdomain1 或 subdomain2 - 随机选择。
举例来说:
# Request from browser:
>> http://example.com/dir/image.jpg
# Rewritten to:
>> http://example.com/imghandler.php?location=%2Fdir%2Fimage.jpg
# *Redirects* to either:
1:
>> http://subdomain1.example.com/dir/image.jpg
(this is where the browser ends up getting the image from)
2:
>> http://subdomain2.example.com/dir/image.jpg
(this is where the browser ends up getting the image from)
我有两个问题:
- 从理论角度来看,这可行吗?
- 有更好的方法来实现我想要的吗?
First, a bit of background info:
The HTTP 1.1 specification, circa 1999, recommends that browsers and servers limit parallel requests to the same hostname to two. (more)
If you carry on reading that article the author suggests "fooling" browsers by having multiuple subdomains all pointing to the same thing.
If I was to serve my images from two separate subdomains (two different hostnames) then the browser would download a maximum of 4 images in parallel (2 per hostname).
Given that, I could now equally spread out the requests between the two subdomains to optimise page download speed, like so:
<img src="http://subdomain1.example.com/img1.jpg" />
<img src="http://subdomain2.example.com/img2.jpg" />
<img src="http://subdomain1.example.com/img3.jpg" />
<img src="http://subdomain2.example.com/img4.jpg" />
This would require me to manually go through the appropriate files and change the 'src' of each image.
I'm looking for a far more simple/reusable solution that involves no visible changes to the HTML.
I have an idea:
- All image-like URLS on [example.com] are redirected (via .htaccess) to [example.com/imghandler.php]
- imghandler.php redirects to either subdomain1 or subdomain2 - randomly chosen.
To illustrate:
# Request from browser:
>> http://example.com/dir/image.jpg
# Rewritten to:
>> http://example.com/imghandler.php?location=%2Fdir%2Fimage.jpg
# *Redirects* to either:
1:
>> http://subdomain1.example.com/dir/image.jpg
(this is where the browser ends up getting the image from)
2:
>> http://subdomain2.example.com/dir/image.jpg
(this is where the browser ends up getting the image from)
I have two questions:
- From a theoretical point of view, would this work?
- Is there a better way of accomplishing what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于这样的策略要记住的一件事是您希望将对同一文件的请求定向到同一服务器。 请求:
在一页上然后:
在下一页上并没有多大好处。
我们在这里使用的技巧是对文件名进行哈希处理(C# 中的 GetHashCode)并使用它来选择存储桶:
这可以确保对同一图像的进一步请求由浏览器的缓存提供服务,而不是由不同的服务器提供。
One thing to keep in mind with strategies like this is you want to direct the requests for the same file to the same server. It doesn't do much good to have requests for:
On one page then:
On the next.
The trick we use here is to hash the file name (GetHashCode in C#) and use that to choose a bucket:
This ensures that further requests for the same image are served from the browser's cache and not by a different server.
最好将所有图像都放在一台服务器上,而不是每个图像必须有 2 个请求。
如果您有动态页面内容(即看起来您正在使用 php),则可以通过创建执行相同操作的函数来获得相同的效果并直接在 html 中对子域进行编码。 您的图像标签可能如下所示:
您应该能够使用合适的编辑器和一些正则表达式搜索和替换将这种更改全局应用到您的网站。
You are better off just having all your images on 1 server rather than making there have to be 2 requests for each image.
If you have dynamic page content (i.e. it looks like you are using php), you can get the same effect and encode the subdomains directly in the html by creating a function which does the same sort of thing. Your image tags could look like:
You should be able to apply this kind of change globally to your site with a decent editor and some regex search and replace.
是的。 它会起作用的。 但最终您会收到两倍数量的请求,但仍然会阻碍第一个请求通过二乘二主域获得可能的重定向。
在图像实际开始加载之前,您可以尝试使用 JavaScript(又名 jQuery)在 DOM 的就绪事件中快速更改图像源。 您需要使用一致的方法来计算要使用的子域选项,以便将来引用相同图像的页面将导致对相同子域选择的更改。
Yes. It would work. But you would wind up with twice as many requests, still bottle-necking the first request to get a possible redirect through the two-by-two primary domain.
You might try using JavaScript (aka jQuery) to quickly change the source of the images in the ready event of the DOM, before the images actually begin loading. You'd want to use a consistent method of calculating which of your subdomain options to use so that future pages referencing the same image would result in a change to the same subdomain choice.