# NGINX as proxy before apache, and localy store php generated images

**URL:** <https://www.ruby-forum.com/t/nginx-as-proxy-before-apache-and-localy-store-php-generated-images/223293>\
**Category:** NGINX\
**Created:** [September 28, 2012, 10:11am UTC](https://www.ruby-forum.com/t/nginx-as-proxy-before-apache-and-localy-store-php-generated-images/223293 "2012-09-28T10:11:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 28, 2012, 10:11am UTC](https://www.ruby-forum.com/t/nginx-as-proxy-before-apache-and-localy-store-php-generated-images/223293/1 "2012-09-28T10:11:00Z")

</div>

Hello everyone,

I am using nginx as proxy before apache a long time already. And it is  
really terrific what it does.  
Ram usage dropped like 3 times  
I mainly set up image cache headers in nginx config

Today I’ve spotted, that one of vhosts is generating images ALWAYS on  
the  
fly, images are accessed like:  
/bin/825?w=121&h=87&cutted=fit  
/bin/4999?w=222  
/bin/5113?w=121&h=87&cutted=fit

etc…

I wonder is there any way I can tell nginx to store those images  
locally,  
ie for a week, and serve them  
to other users, so it won’t be resized multiple times?

I tried googling but don’t even know how/where to start from…

Thank you in advance!  
huglester

---

<div class="post-metadata">

**Author:** ![Jaroslav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eb9ed0/32.png) [@Jaroslav](https://www.ruby-forum.com/u/Jaroslav)\
**Post date:** [September 28, 2012, 11:15am UTC](https://www.ruby-forum.com/t/nginx-as-proxy-before-apache-and-localy-store-php-generated-images/223293/2 "2012-09-28T11:15:54Z")

</div>

Thank you very much Igor for a fast response!

So far I see the performance increase. I see the logs being created. And  
no  
longer see so lot access logs to apache images.

my config is:  
location /bin/ {  
expires 7d;  
add\_header X-Cache “Backend2-cache”;  
proxy\_pass [http://site.com](http://site.com);

```
            proxy_cache my-cache;
            proxy_cache_valid 200 302 360m;
            proxy_cache_valid 404 1m;
        }

```

One more question is left in my mind, I see the expire headers are set,  
but  
the HTTP response status is always 200.  
is there somewhere a way to make it respond with status 304 (not  
modified)  
?

Thank you

---

<div class="post-metadata">

**Author:** ![Jaroslav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eb9ed0/32.png) [@Jaroslav](https://www.ruby-forum.com/u/Jaroslav)\
**Post date:** [September 28, 2012, 10:26am UTC](https://www.ruby-forum.com/t/nginx-as-proxy-before-apache-and-localy-store-php-generated-images/223293/3 "2012-09-28T10:26:48Z")

</div>

On Sep 28, 2012, at 12:10 , Jaroslav wrote:

> etc..
> 
> I wonder is there any way I can tell nginx to store those images locally, ie for  
> a week, and serve them  
> to other users, so it won’t be resized multiple times?
> 
> I tried googling but don’t even know how/where to start from..

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

---

<div class="post-metadata">

**Author:** ![Jaroslav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eb9ed0/32.png) [@Jaroslav](https://www.ruby-forum.com/u/Jaroslav)\
**Post date:** [September 28, 2012, 11:25am UTC](https://www.ruby-forum.com/t/nginx-as-proxy-before-apache-and-localy-store-php-generated-images/223293/4 "2012-09-28T11:25:15Z")

</div>

On Sep 28, 2012, at 13:15 , Jaroslav wrote:

> ```
> proxy_cache my-cache;
> proxy_cache_valid 200 302 360m;
> proxy_cache_valid 404 1m;
> }
> 
> ```
> 
> One more question is left in my mind, I see the expire headers are set, but the  
> HTTP response status is always 200.  
> is there somewhere a way to make it respond with status 304 (not modified) ?

Do these images have “Last-Modified” or “ETag” header field ?

–  
Igor S.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![Jaroslav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eb9ed0/32.png) [@Jaroslav](https://www.ruby-forum.com/u/Jaroslav)\
**Post date:** [September 28, 2012, 11:32am UTC](https://www.ruby-forum.com/t/nginx-as-proxy-before-apache-and-localy-store-php-generated-images/223293/5 "2012-09-28T11:32:17Z")

</div>

I am afraid they not ☹

Those images by default are expiring in epoch.  
You want to say than nginx will handle this 304 thing if those headers  
are  
correctly set in PHP?

Can this be ‘fixed’ without touching php code?

Thank you

---

<div class="post-metadata">

**Author:** ![Jaroslav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eb9ed0/32.png) [@Jaroslav](https://www.ruby-forum.com/u/Jaroslav)\
**Post date:** [September 28, 2012, 11:42am UTC](https://www.ruby-forum.com/t/nginx-as-proxy-before-apache-and-localy-store-php-generated-images/223293/6 "2012-09-28T11:42:49Z")

</div>

On Sep 28, 2012, at 13:31 , Jaroslav wrote:

> I am afraid they not ☹
> 
> Those images by default are expiring in epoch.  
> You want to say than nginx will handle this 304 thing if those headers are  
> correctly set in PHP?

Not nginx but browsers: they do not send If-Modified-Since/If-None-Match  
header fields to nginx.  
If these images never expire then you can add

location /bin/ {  
expires 7d;  
add\_header ETag ‘“anything”’;

–  
Igor S.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.
