将图像请求路由到单独的子域

发布于 2024-07-23 09:44:18 字数 1532 浏览 4 评论 0原文

首先,一些背景信息:

大约 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 的可见更改。

我有一个想法:

  1. [example.com] 上的所有类似图像的 URL 都被重定向(通过 .htaccess)到 [example.com/imghandler.php]
  2. 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)

我有两个问题:

  1. 从理论角度来看,这可行吗?
  2. 有更好的方法来实现我想要的吗?

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:

  1. All image-like URLS on [example.com] are redirected (via .htaccess) to [example.com/imghandler.php]
  2. 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:

  1. From a theoretical point of view, would this work?
  2. Is there a better way of accomplishing what I want?

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

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

发布评论

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

评论(3

我也只是我 2024-07-30 09:44:18

对于这样的策略要记住的一件事是您希望将对同一文件的请求定向到同一服务器。 请求:

http://subdomain1.example.com/img1.jpg
http://subdomain2.example.com/img2.jpg
http://subdomain1.example.com/img3.jpg
http://subdomain2.example.com/img4.jpg

在一页上然后:

http://subdomain2.example.com/img1.jpg
http://subdomain1.example.com/img2.jpg
http://subdomain2.example.com/img3.jpg
http://subdomain1.example.com/img4.jpg

在下一页上并没有多大好处。

我们在这里使用的技巧是对文件名进行哈希处理(C# 中的 GetHashCode)并使用它来选择存储桶:

var serverNumber = image.GetHashCode() % serverCount;

这可以确保对同一图像的进一步请求由浏览器的缓存提​​供服务,而不是由不同的服务器提供。

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:

http://subdomain1.example.com/img1.jpg
http://subdomain2.example.com/img2.jpg
http://subdomain1.example.com/img3.jpg
http://subdomain2.example.com/img4.jpg

On one page then:

http://subdomain2.example.com/img1.jpg
http://subdomain1.example.com/img2.jpg
http://subdomain2.example.com/img3.jpg
http://subdomain1.example.com/img4.jpg

On the next.

The trick we use here is to hash the file name (GetHashCode in C#) and use that to choose a bucket:

var serverNumber = image.GetHashCode() % serverCount;

This ensures that further requests for the same image are served from the browser's cache and not by a different server.

通知家属抬走 2024-07-30 09:44:18

最好将所有图像都放在一台服务器上,而不是每个图像必须有 2 个请求。

如果您有动态页面内容(即看起来您正在使用 php),则可以通过创建执行相同操作的函数来获得相同的效果并直接在 html 中对子域进行编码。 您的图像标签可能如下所示:

<img src="<?php echo image_root(); ?>/dir/image.jpg">

您应该能够使用合适的编辑器和一些正则表达式搜索和替换将这种更改全局应用到您的网站。

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:

<img src="<?php echo image_root(); ?>/dir/image.jpg">

You should be able to apply this kind of change globally to your site with a decent editor and some regex search and replace.

机场等船 2024-07-30 09:44:18
  1. 是的。 它会起作用的。 但最终您会收到两倍数量的请求,但仍然会阻碍第一个请求通过二乘二主域获得可能的重定向。

  2. 在图像实际开始加载之前,您可以尝试使用 JavaScript(又名 jQuery)在 DOM 的就绪事件中快速更改图像源。 您需要使用一致的方法来计算要使用的子域选项,以便将来引用相同图像的页面将导致对相同子域选择的更改。

  1. 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.

  2. 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.

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