0

我正在开发一个应用程序,其中我使用第三方服务提供的大部分照片。在构建我的第一个原型时,我直接从服务中获取图像,即

<img src="http://thirdpartyservice.com/img/12345.jpg">

虽然它是生产的,但每次收到请求时从服务中获取图像是没有意义的。它太慢了,因为它同时为数百万用户提供服务。

我想在 nginx 之上构建一个薄层,它可以根据需要 24 小时获取和缓存图像。所以不是每次都调用服务,我宁愿打电话

<img src="http://myapp.com/img?url=thirdpartyservice.com/img/12345.jpg">

如果这是第一次请求图像,它将从远程服务中获取并在我的服务器上缓存 24 小时。

这可能与nginx有关吗?

4

1 回答 1

0

First I would suggest not to change url format to http://myapp.com/img/12345.jpg in case we always proxy to the same thirdpartyservice.com. Here is config example based on nginx wiki.

http {
    proxy_cache_path  /data/nginx/cache  levels=1:2    keys_zone=STATIC:10m
                                         inactive=24h  max_size=1g;
    server {
        location /img/ {
            proxy_pass             http://thirdpartyservice.com;
            proxy_set_header       Host thirdpartyservice.com;
            proxy_cache            STATIC;
            proxy_cache_valid      200  1d;
            proxy_cache_use_stale  error timeout invalid_header updating
                                   http_500 http_502 http_503 http_504;
        }
    }
}

There are lot of directives about cache tweaking in official documentation.

于 2014-03-08T18:23:38.627 回答