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

时间:2022-10-13 12:45:11

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)

大约1999年的HTTP 1.1规范建议浏览器和服务器将对同一主机名的并行请求限制为两个。 (更多)

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

如果我要从两个单独的子域(两个不同的主机名)提供我的图像,那么浏览器将并行下载最多4个图像(每个主机名2个)。

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.

这需要我手动浏览相应的文件并更改每个图像的'src'。


I'm looking for a far more simple/reusable solution that involves no visible changes to the HTML.

我正在寻找一个更简单/可重用的解决方案,它不会对HTML进行任何可见的更改。

I have an idea:

我有个主意:

  1. All image-like URLS on [example.com] are redirected (via .htaccess) to [example.com/imghandler.php]
  2. [example.com]上的所有类似图像的URL都被重定向(通过.htaccess)到[example.com/imghandler.php]

  3. imghandler.php redirects to either subdomain1 or subdomain2 - randomly chosen.
  4. imghandler.php重定向到subdomain1或subdomain2 - 随机选择。

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. 从理论的角度来看,这会有用吗?

  3. Is there a better way of accomplishing what I want?
  4. 有没有更好的方法来实现我想要的?

3 个解决方案

#1


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:

我们在这里使用的技巧是散列文件名(C#中的GetHashCode)并使用它来选择一个桶:

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.

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

#2


You are better off just having all your images on 1 server rather than making there have to be 2 requests for each image.

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

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:

如果你有动态页面内容(即看起来你使用的是php),你可以获得相同的效果,并通过创建一个执行相同类型操作的函数直接在html中编码子域。您的图片代码可能如下所示:

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

您应该能够通过一个像样的编辑器和一些正则表达式搜索和替换全局地将这种更改应用于您的站点。

#3


  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×2主域重定向的请求。

  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.

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

#1


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:

我们在这里使用的技巧是散列文件名(C#中的GetHashCode)并使用它来选择一个桶:

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.

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

#2


You are better off just having all your images on 1 server rather than making there have to be 2 requests for each image.

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

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:

如果你有动态页面内容(即看起来你使用的是php),你可以获得相同的效果,并通过创建一个执行相同类型操作的函数直接在html中编码子域。您的图片代码可能如下所示:

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

您应该能够通过一个像样的编辑器和一些正则表达式搜索和替换全局地将这种更改应用于您的站点。

#3


  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×2主域重定向的请求。

  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.

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