The best configuration for serving static content

Valentin V. Bartenev ne at
Mon Dec 5 15:20:48 UTC 2011

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/ {


> 	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

More information about the nginx mailing list