Serving Images and attachments via nginx

Mickie D nginx-forum at
Tue Jun 1 14:07:24 MSD 2010

Hi there I changed the way my servers act so I could reduce the load from my machines but would like some help with setting it up.

It would be really nice to let all users hit my main server (PHP),  and then nginx can offload the images to one server and the attachments to another ???

here is my main config:

user  www www;

worker_processes 8;

error_log  /disk1/logs/nginx_main_error.log  crit;

pid        /usr/local/webserver/nginx/;

#Specifies the value for maximum file descriptors that can be opened by this process.
#worker_rlimit_nofile 51200;

     use epoll;

     worker_connections 1024;

     include       mime.types;
     default_type  application/octet-stream;
     server_tokens   off;

## Size Limits
  client_body_buffer_size         1k;
  client_header_buffer_size       1k;
  client_max_body_size            6m;
  large_client_header_buffers   4 4k;
  connection_pool_size           256;
  request_pool_size               4k;
  server_names_hash_bucket_size  128;

## Timeouts
  client_body_timeout   60;
  client_header_timeout 60;
  keepalive_timeout     60;
  send_timeout          60;

     sendfile on;
     tcp_nopush     on;

     tcp_nodelay on;

     fastcgi_connect_timeout 60;
     fastcgi_send_timeout 120;
     fastcgi_read_timeout 45;
     fastcgi_buffer_size 8k;
     fastcgi_buffers 8 4k;
     fastcgi_busy_buffers_size 8k;
     fastcgi_temp_file_write_size 28k;
     fastcgi_temp_path /dev/shm;

        gzip                    on;
        gzip_min_length         1100;
        gzip_buffers            4 8k;
        gzip_http_version       1.0;
        gzip_comp_level         1;
        gzip_proxied            any;
        gzip_types              text/plain text/html text/css text/javascript text/xml application/x-javascript application/xml application/xml+rss;

             listen       80;
             index index.html index.htm index.php;
             root  /data0/htdocs/www/forum;

             location ~ .*\.php?$
                  include fcgi.conf;
                  fastcgi_index index.php;


		error_page 500 502 503 504 /50x.html;
 		location = /50x.html {
  		root /data0/htdocs/www/forum;

        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
              access_log        off;
       	      expires           30d;
	proxy_pass http://IP-ADDY-TO-IMAGE-SERVER;       

	log_format gzip '$remote_addr - $remote_user [$time_local]  '
                '"$request" $status $bytes_sent '
                '"$http_referer" "$http_user_agent" "$gzip_ratio"';
	access_log  /disk1/logs/nginx-access.log  gzip  buffer=32k;
	error_log  /disk1/logs/nginx_error.log warn;

rewrite ^/((urllist|sitemap_).*\.(xml|txt)(\.gz)?)$ /vbseo_sitemap/vbseo_getsitemap.php?sitemap=$1 last;

# globally disable external access, but enable for X-Accel-Redirect
location /uploads {
# enable direct access to blogs & posts thumbnails, but nothing else!
location ~* /uploads/(.*)\.thumb$ {
# enable direct access to album thumbnails
location /uploads/thumbs/ {

if ($request_filename ~ "\.php$" ) {
rewrite ^/(.*)$ /vbseo.php break;

if (!-e $request_filename) {
rewrite ^/(.*)$ /vbseo.php break;


rewrite ^/archive/index.php/(.*)$ /archive/index.php?$1;



I do not know how to make it work correctly for the images when I put the IP-ADDY-TO-IMAGE-SERVER  it makes everything show up without images ?

Also what code would I use to point attachements like ZIP/RAR etc to another server?  I was thinking this might work but again not sure?

        location ~* ^.+.(rar|zip)$ {
              access_log        off;
       	      expires           30d;
	proxy_pass http://IP-ADDY-TO-ATTACHMENT-SERVER;       

and another thing should I use local network IP's or the Public ones in the proxy_pass directive?

Thank you very much for any help on this subject.


Posted at Nginx Forum:,93066,93066#msg-93066

More information about the nginx mailing list