The best configuration for serving static content

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:
http://forum.nginx.org/read.php?2,219622,219622#msg-219622

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

On 5 Dez 2011 10h29 WET, [email protected] wrote:

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

expires 1y;

It’s easier to understand IMO.

— appa

Hello!

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

On 5 Dez 2011 10h29 WET, [email protected] 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.

On 5 Dez 2011 16h33 WET, [email protected] 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