문제

I am developing an application, where I use most of the photos provided by a third party service. While building my first prototype, I got the images directly from the service, i.e.

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

It production though, it makes no sense grabbing the images from the service every time they get requested. It is just to too slow, because it serves millions of users at the same time.

I'd like to build a thin layer on top of nginx, which fetches, and caches images for 24 hours based on demand. So instead of calling the service every time, I'd rather call

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

If this is the first time an image is requested, it will be fetched from the remote service and cached on my servers for 24 hours.

Is this possible to do with nginx?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top