Serving Static Files

Hello everyone. I’ve been using nginx for a while now but haven’t
scratched the surface, just used recommended options.

I decided I needed to know more about it, so here I am.

In the main http module nginx.conf file there are these lines

include       /etc/nginx/mime.types;
default_type  application/octet-stream;

I assumed what they meant is that if a client tries to download a file
that is described in the mime.types file, then nginx will handle that
download for me.

In one of our server blocks I’ve noticed that another developer has a
location block like this

location ~*
^.+.(docx|pdf|mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg|jpg|jpeg|gi
f|png|ico|css|bmp|js)$ {
root /var/django/django_projects/glamlifenews;
limit_rate 2000K;
access_log
/var/django/conf/log/glamlifenews_nginx_django_media.log download;
access_log off;
}

Would I be right in assuming that what would happen here is that in this
particular website nginx would still handle the download, but the extra
options are there to handle logging the downloads for this particular
app, setting the limit_rate and giving a specific place to download the
files from?

Would this also speed up the download process?

Any help, greatly appreciated.

Posted at Nginx Forum:

Hi,

I also want to ask some tips on how to configure my site with nginx
while static files serves on the other server. How this can be done?

Thanks in advance.

  • Bernard

Posted at Nginx Forum:

On Thu, Sep 24, 2009 at 03:43:32AM -0400, bernard.barbosa wrote:

Hi,

I also want to ask some tips on how to configure my site with nginx while static files serves on the other server. How this can be done?

How do you want to serve static files from another server ?
Just proxying or using caching ? May these files change in future ?

Hi Igor,

Im sorry im a bit lost with the question and i have no idea which is the
best. Is it by proxying or caching? I think yes they can change in the
future…

My setup should be something like this:

sample.server.com serve as the main or official site that users will
access and the static files should be located at sample.static.com. How
am i suppose to do it? both servers have nginx how should configure
that?

Thanks a lot.

Bernard

Posted at Nginx Forum:

Igor S. Wrote:

My setup should be something like this:
keys_zone=STATIC:10m
invalid_header updating;


Igor S.
Igor Sysoev

Thanks Igor!!
A few questions again :stuck_out_tongue:
Should the two server must have identical files? i mean same directory
and same files?
How should i check that it works?
Is there any documentation i can refer this to as well?

Thank you so much…

Bernard

Posted at Nginx Forum:

Sorry I am getting an error:

“unknown directive “proxy_cache_path” in
/usr/local/nginx/conf/nginx.conf:57
2009/09/25 20:09:51 11656#0: the configuration file
/usr/local/nginx/conf/nginx.conf test failed”

I am using nginx version: 0.6.35 . Can you please help me?

Thank you,

Bernard

Posted at Nginx Forum:

On Fri, Sep 25, 2009 at 02:30:32AM -0400, bernard.barbosa wrote:

Hi Igor,

Im sorry im a bit lost with the question and i have no idea which is the best. Is it by proxying or caching? I think yes they can change in the future…

My setup should be something like this:

sample.server.com serve as the main or official site that users will access and the static files should be located at sample.static.com. How am i suppose to do it? both servers have nginx how should configure that?

sample.server.com:

proxy_cache_path /path/to/cache levels=1:2 keys_zone=STATIC:10m
inactive=1h;

server {

   location /static/ {
       proxy_pass             http://sample.static.com;
       proxy_cache            STATIC;
       proxy_cache_valid      1d;
       proxy_cache_use_stale  error timeout invalid_header updating;
   }

sample.static.com:

server {

   location /static/ {
       root  /path/to/files;
   }

On Fri, Sep 25, 2009 at 05:19:13AM -0400, bernard.barbosa wrote:

Sorry I am getting an error:

“unknown directive “proxy_cache_path” in /usr/local/nginx/conf/nginx.conf:57
2009/09/25 20:09:51 11656#0: the configuration file /usr/local/nginx/conf/nginx.conf test failed”

I am using nginx version: 0.6.35 . Can you please help me?

The HTTP cache has been introduced in 0.7.x. You should use 0.7.62.

On Fri, Sep 25, 2009 at 05:12:30AM -0400, bernard.barbosa wrote:

future…

       proxy_cache_valid      1d;
   }


Igor S.
Igor Sysoev

Thanks Igor!!
A few questions again :stuck_out_tongue:
Should the two server must have identical files? i mean same directory and same files?

Try to keep configuration as simple as possible to avoid any issues in
future.
This means that file names should be identical.

You can use some directory mapping as

   location /static/ {
        proxy_pass    http://sample.static.com/another/dir/;
   }

or

   location /static/ {
        alias  /path/to/some/files/;
   }

but I prefere direct mapping.

How should i check that it works?

Using logs.

Is there any documentation i can refer this to as well?

http://wiki.nginx.org/NginxHttpProxyModule

Hope you can help me clear on my issue.

Thank you so much Igor!!

Bernard

Posted at Nginx Forum:

Igor S. Wrote:

I am using nginx version: 0.6.35 . Can you
please help me?

The HTTP cache has been introduced in 0.7.x. You
should use 0.7.62.


Igor S.
Igor Sysoev

Hi Igor,

I have upgraded my nginx to version 0.7.62 and there is no error now
thanks.
But i think it is not working as i expected it to be. With the
configuration you gave me I can’t find any activities on logs showing
any connection or request on both server. Can you please check what
seems to be wrong on my configuration?

These are the configurations:

On sample.server.com:

user www-data www-data;
worker_processes 1;

[ debug | info | notice | warn | error | crit ]

error_log /usr/local/nginx/logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid;

events {
worker_connections 1024;
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;
# check: http://wiki.nginx.org/NginxOptimizations
use epoll;
}

http {
include /usr/local/nginx/conf/mime.types;
include /usr/local/nginx/conf/fastcgi_params;

#
# logging
#
default_type application/octet-stream;
log_format   main '$remote_addr - $remote_user [$time_local] 

$status ’
'“$request” $body_bytes_sent “$http_referer” ’
‘“$http_user_agent” “$http_x_forwarded_for”’;
access_log /usr/local/nginx/logs/access.log main;

#
# other
#
sendfile     on;
tcp_nopush   on;
server_tokens off;
server_names_hash_bucket_size 128;

#
# timeouts
#
client_header_timeout   3m;
client_body_timeout     3m;
send_timeout            3m;
keepalive_timeout       10;

#
# gzip
#
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 2;
gzip_proxied any;
gzip_vary on;
gzip_types text/plain text/css text/javascript 

application/x-javascript text/xml application/xml application/xml+rss;

proxy_cache_path /home/site/cache levels=1:2 keys_zone=STATIC:10m 

inactive=1h;

 server {
    listen 80;
    server_name sample.server.com;

    root /home/site/sample;
    index index.php;

    location /static/ {
    proxy_pass      http://sample.static.com;
    proxy_cache     STATIC;
    proxy_cache_valid 1d;
    proxy_cache_use_stale error timeout invalid_header updating;
    }

   if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php?q=$1 last;
    }

    error_page 404 index.php;

    # hide protected files
    location ~* 

.(engine|inc|info|install|module|profile|po|sh|.sql|theme|tpl(.php)?|xtmpl)$|^(code-style.pl|Entries.|Repository|Root|Tag|Template)$
{
deny all;
}

    # hide backup_migrate files
    location ~* ^/files/backup_migrate {
      deny all;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME 

/home/site/sample$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}
}

}

While on sample.static.com:

#user nobody;
user www-data www-data;
worker_processes 1;

[ debug | info | notice | warn | error | crit ]

error_log /usr/local/nginx/logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid;

events {
worker_connections 1024;
use epoll;
}

http {
include /usr/local/nginx/conf/mime.types;
include /usr/local/nginx/conf/fastcgi_params;

 default_type application/octet-stream;
 log_format   main '$remote_addr - $remote_user [$time_local] 

$status ’
'“$request” $body_bytes_sent “$http_referer” ’
‘“$http_user_agent” “$http_x_forwarded_for”’;
access_log /usr/local/nginx/logs/access.log main;

sendfile        on;
tcp_nopush     on;
server_tokens off;
server_names_hash_bucket_size 128;

#
# timeouts
#
client_header_timeout   3m;
client_body_timeout     3m;
send_timeout            3m;
keepalive_timeout       10;

gzip

gzip on;
gzip_buffers 16 8k;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript
text/xml application/xml
application/xml+rss text/javascript;

Allows directory listing

autoindex on;

server {
listen 80;
server_name sample.static.com;
root /home/site/staticfiles;
index index.php;

    # PHP files
            location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            }

    # redirect server error pages to the static page /50x.html
    #
    error_page 404 index.php;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME 

/home/site/staticfiles$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}
}
}

Posted at Nginx Forum:

Hi Igor,

I hope you find some time checking the configurations I’ve posted and
let me know of the possible solutions on my problem.

Thank you so much.


Bernard

Posted at Nginx Forum:

bernard.barbosa Wrote:

2009/09/25 20:09:51 11656#0: the

be. With the configuration you gave me I can’t
user www-data www-data;
http://wiki.nginx.org/NginxOptimizations
default_type application/octet-stream;
# other
client_body_timeout 3m;
gzip_vary on;

ries.*|Repository|Root|Tag|Template)$ {
fastcgi_index index.php;
}
user www-data www-data;
http {
“$http_x_forwarded_for”';
#
gzip_buffers 16 8k;

            location ~ \.php$ {

}

        fastcgi_param  CONTENT_TYPE    

$content_type;
fastcgi_param CONTENT_LENGTH
$content_length;
}
}
}

Dear Igor,

Any update please?

Thank you.


Bernard

Posted at Nginx Forum: