# The best configuration for serving static content

**URL:** <https://www.ruby-forum.com/t/the-best-configuration-for-serving-static-content/214543>\
**Category:** NGINX\
**Created:** [December 5, 2011, 11:29am UTC](https://www.ruby-forum.com/t/the-best-configuration-for-serving-static-content/214543 "2011-12-05T11:29:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [December 5, 2011, 11:29am UTC](https://www.ruby-forum.com/t/the-best-configuration-for-serving-static-content/214543/1 "2011-12-05T11:29:59Z")

</div>

I want to create a config for domains serving static content. I made the  
following conf file. Is it the best possible code? (I just kept php  
interpretation for possible usage)

location ~\*  
^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf|wma|wmv)$  
{

proxy\_redirect off;  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
client\_max\_body\_size 10m;  
client\_body\_buffer\_size 128k;  
proxy\_connect\_timeout 90;  
proxy\_send\_timeout 90;  
proxy\_read\_timeout 90;  
proxy\_buffer\_size 4k;  
proxy\_buffers 4 32k;  
proxy\_busy\_buffers\_size 64k;  
proxy\_temp\_file\_write\_size 64k;  
access\_log off;  
}  
location ~ .(css|js|jpg|jpeg|png|gif)$ {  
expires 31536000s;  
add\_header Pragma public;  
add\_header Cache-Control public;  
}  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_read\_timeout 600;  
fastcgi\_send\_timeout 600;  
include /etc/nginx/fastcgi\_params;  
}

Posted at Nginx Forum:

> **[The best configuration for serving static content](https://forum.nginx.org/read.php?2,219622,219622#msg-219622)**
>
> I want to create a config for domains serving static content. I made the following conf file. Is it the best possible code? (I just kept php interpretation for possible usage) location ~\*...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 5, 2011, 4:21pm UTC](https://www.ruby-forum.com/t/the-best-configuration-for-serving-static-content/214543/2 "2011-12-05T16:21:13Z")

</div>

On Monday 05 December 2011 14:29:28 etrader wrote:

> I want to create a config for domains serving static content. I made the  
> following conf file. Is it the best possible code? (I just kept php  
> interpretation for possible usage)

Actually, there can not exists the best possible config in general. The  
best way  
would be to configure your server exactly for your needs. And only you  
can fully  
understand your case.

Instead of using big ugly regexps, it would be much better to keep all  
static  
content in separate folders.

For example:

location /media/ {  
…  
}

or more specific:

location /images/ {  
…  
}

location /styles/ {  
…  
}

etc…

> location ~\*  
> ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|ta  
> r|mid|midi|wav|bmp|rtf|js|swf|wma|wmv)$ {

Probably, you forgot to escape the dot. (like “.(jpg…$”)

location ~\*  
.(jpe?g|gif|png|ico|css|zip|t?gz|rar|bz2?|doc|xls|exe|pdf|ppt|txt|  
tar|midi?|wav|bmp|rtf|js|swf|wm[av])$ {

Do you really need all these extensions? Do you really want to configure  
“css”  
and “wmv” the same way?

> proxy\_redirect off;  
> proxy\_set\_header Host $host;  
> proxy\_set\_header X-Real-IP $remote\_addr;  
> proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
> […]  
> proxy\_connect\_timeout 90;  
> proxy\_send\_timeout 90;  
> proxy\_read\_timeout 90;  
> proxy\_buffer\_size 4k;  
> proxy\_buffers 4 32k;  
> proxy\_busy\_buffers\_size 64k;  
> proxy\_temp\_file\_write\_size 64k;

What are they for? I mean all these proxy\_\* directives.

1. They are meaningless without the “proxy\_pass” directive.
2. Actually, I really doubt that you want to proxy your static content  
to  
another web server.

> ```
> access_log off;
> 
> ```
> 
> }  
> location ~ .(css|js|jpg|jpeg|png|gif)$ {  
> expires 31536000s;  
> add\_header Pragma public;  
> add\_header Cache-Control public;  
> }

This location will never get a request, cuz it entirely overlapped by  
the  
above one.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 5, 2011, 5:24pm UTC](https://www.ruby-forum.com/t/the-best-configuration-for-serving-static-content/214543/3 "2011-12-05T17:24:26Z")

</div>

On 5 Dez 2011 10h29 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> location ~ .(css|js|jpg|jpeg|png|gif)$ {  
> expires 31536000s;  
> ^^^^^^^^^  
> Why don’t you use:

expires 1y;

It’s easier to understand IMO.

— appa

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 5, 2011, 5:34pm UTC](https://www.ruby-forum.com/t/the-best-configuration-for-serving-static-content/214543/4 "2011-12-05T17:34:22Z")

</div>

Hello!

On Mon, Dec 05, 2011 at 04:22:19PM +0000, António P. P. Almeida wrote:

> On 5 Dez 2011 10h29 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > location ~ .(css|js|jpg|jpeg|png|gif)$ {  
> > expires 31536000s;  
> > ^^^^^^^^^  
> > Why don’t you use:
> 
> expires 1y;
> 
> It’s easier to understand IMO.

The whole location in question is a nop (it’s never matched as  
previous one will always win), so it doesn’t really matter.

Maxim D.

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 5, 2011, 6:23pm UTC](https://www.ruby-forum.com/t/the-best-configuration-for-serving-static-content/214543/5 "2011-12-05T18:23:54Z")

</div>

On 5 Dez 2011 16h33 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Why don’t you use:
> > 
> > expires 1y;
> > 
> > It’s easier to understand IMO.
> 
> The whole location in question is a nop (it’s never matched as  
> previous one will always win), so it doesn’t really matter.

Yep. I know. It’s just for his information, so that in future he can  
use a more legible config style.

— appa
