首先,一些背景信息:
大约 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)
我有两个问题:
- 从理论的角度来看,这行得通吗?
- 有没有更好的方法来完成我想要的?