Total Pageviews

Wednesday, 16 November 2011

Nginx的内嵌perl-module,实现高性能的web静态化

生成静态文件的时机,这个取决你的应用,像blog,新闻等内容为主的应用,可以在第一次请求的时候生成;又如一些web api类型的,可以在同一请求达到指定的次数时,才生产静态文件。

至于怎样生成静态文件?最简单的方式是将response的内容直接写到文件中,

静态文件的失效时间,像上面生成静态文件路径的例子中,我设定的失效时间的30分钟。



完整的配置文件例子如下:


user www www;

pid  / home / test / nginx . pid;
worker_processes   8 ;
error_log  / data / nginx / logs / error . log ;

events {
  worker_connections   2048 ;
   use  epoll;
}

http {
   #  default nginx location
  include         / home / test / mime . types;
  default_type    text / html;
  log_format main
       ' $remote_addr $host $remote_user [$time_local] "$request" $status $bytes_sent "$http_referer" "$http_user_agent" $process $request_time $sent_http_x_type ' ;
 
  client_header_timeout  10s;  #  If after this time the client send nothing, nginx returns error "Request time out" (408).
  client_body_timeout    10s;
  send_timeout           10s;  #  if after this time client will take nothing, then nginx is shutting down the connection.
  connection_pool_size         256 ;
  client_header_buffer_size    1k;
  large_client_header_buffers     4  2k;
  request_pool_size        4k;
  output_buffers    4  32k;
  postpone_output   1460 ;
  sendfile        on;
  tcp_nopush             on;
  keepalive_timeout       20   10 ;
  tcp_nodelay            on;
 
  fastcgi_connect_timeout  300 ;
  fastcgi_send_timeout  300 ;
  fastcgi_read_timeout  300 ;
  fastcgi_buffer_size 64k;
  fastcgi_buffers  4  64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  client_max_body_size       10m;
  client_body_buffer_size    256k;
  proxy_connect_timeout       90 ;
  proxy_send_timeout          90 ;
  proxy_read_timeout          90 ;
  client_body_temp_path       / data / nginx / logs / client_body_temp;
  proxy_temp_path             / data / nginx / logs / proxy_temp;
  fastcgi_temp_path           / data / nginx / logs / fastcgi_temp;

  gzip off;
  gzip_min_length   1100 ;
  gzip_buffers      4  32k;
  gzip_types       text / plain text / html application / x - javascript text / xml text / css;

  ignore_invalid_headers    on;
 
  perl_set   $path_md5    '
    use Digest::MD5 qw(md5_hex);
    use File::stat;
   
    sub {
        my $r = shift;
        my $s = md5_hex($r->uri);
        my $path_md5 = join "", join("/", substr($s, 0, 1), substr($s, 1, 1), substr($s, 2)), ".html";
        my $filepath = "/data/www/".$path_md5;
        if(-f $filepath) {
            my $mtime = stat($filepath)->mtime;
            if(time() - $mtime > 1800) {
                return $path_md5.".new";
            }
        }
        return $path_md5;
    } ' ;
 
  server {
     listen   80 ;
    server_name  127.0 . 0.1 ;
     index   index . html;
    root    / data / www;
    set  $process   " nginx " ;
   
     #  static resources
    #location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
    #{
    #  expires 30d;
    #  break;
    #}
   
    location  / site_media {
        root    / home / test / web;
        access_log   off;
         # expires 30d;
        rewrite  ^/ site_media / ( .* )  / media / $ 1  break;
    }
   
    location  / nginx_status {
       #  copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/
      stub_status on;
      access_log   off;
      allow  127.0 . 0.1 ;
      allow  192.168 . 0.0 / 16 ;
      allow  219.131 . 196.66 ;
      deny all;
      break;
    }
   
    location  / request_status {
      access_log   off;
      allow  127.0 . 0.1 ;
      allow  192.168 . 0.0 / 16 ;
      deny all;
      rewrite  ^/ request_status / ( .* )  / rrd / $ 1  break;
      autoindex  on;
    }
   
    location  ~*   ^/ (webmd5 | weburl | urlsafe | website | urlnotfound | reporturl | suggesturl | receive | leak | virus | site | admin | index ) /  {
       # rewrite (.*) /$path_md5 redirect;
      try_files  / $path_md5   @fastcgi ;
    }
   
    location  @fastcgi  {
       set  $process   " fcgi " ;
       fastcgi_pass unix :/ home / test / gateway . sock;
       fastcgi_param PATH_INFO  $fastcgi_script_name ;
       fastcgi_param REQUEST_METHOD  $request_method ;
       fastcgi_param QUERY_STRING  $query_string ;
       fastcgi_param CONTENT_TYPE  $content_type ;
       fastcgi_param CONTENT_LENGTH  $content_length ;
       fastcgi_pass_header Authorization;
       fastcgi_param REMOTE_ADDR  $remote_addr ;
       fastcgi_param SERVER_PROTOCOL  $server_protocol ;
       fastcgi_param SERVER_PORT  $server_port ;
       fastcgi_param SERVER_NAME  $server_name ;
       fastcgi_param REQUEST_FILENAME  $path_md5 ;
        # fastcgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;
       fastcgi_intercept_errors off;
       break;
    }
   
    location  / 403 . html {
      root    / usr / local / nginx;
      access_log   off;
    }

    location  / 401 . html {
      root    / usr / local / nginx;
      access_log   off;
    }

    location  / 404 . html {
      root    / usr / local / nginx;
      access_log   off;
    }

    location  =   / _ . gif {
      empty_gif;
      access_log   off;
    }

    access_log     / data / nginx / logs / access . log  main;
  }
}


希望本文对你有用。

No comments:

Post a Comment