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

   2023-02-09 学习力0
核心提示:到底web服务器的极限是多少呢?100 r/s? 500 r/s?还是1000 r/s?在StaticGenerator上看到,原来1000 r/s都不算什么.真的是这样吗?如果是真的那就太恐怖了。目前的web基本上都是动态的,为了提高性能,我们常常会使用各种各样的手段优化,例如减少IO,合理

到底web服务器的极限是多少呢?100 r/s? 500 r/s?还是1000 r/s?

StaticGenerator上看到,原来1000 r/s都不算什么.

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

真的是这样吗?如果是真的那就太恐怖了。

目前的web基本上都是动态的,为了提高性能,我们常常会使用各种各样的手段优化,例如减少IO,合理的字符串使用和操作,memcached等等。但是尽管全部优化都做足了,全动态的web点击率以是在1000 - 2000 r/s(4核4G标准的服务器配置)之间。

还能进一步优化吗?能,这就需要利用前面提到的,“静态文件”。我们知道,web服务器处理静态文件的请求是非常高效的,特别是nginx,它宣称“支持高达 50,000 个并发连接数的响应”。(呵呵,感觉有点像在为nginx卖广告。)

将动态内容静态化输出,有几个问题是必须解决的:

  1. url怎么对应到静态文件路径?
  2. 静态文件什么时候失效?即防止脏数据的存在。
  3. 什么时候生成静态文件?

 

URL转换成静态文件路径

如果web应用的url是友好的,是非常容易对应到静态文件的。例如:

http://hostname/post/some-post-detailname/

这样我们很容易想象到对应的静态文件路径是/post/some-post-detailname/index.html或者/post/some-post-detailname.html等等.

一个通用的方案:对url算md5后来确定静态文件的路径.

还是使用上面的url,它的path是/post/some-post-detailname/,算md5结果为:81982658fe1d78f51d228950babd1457,则可以取路径为/8/1/982658fe1d78f51d228950babd1457

在nginx中如果生成这样的路径呢?答案是使用内嵌perl,以下是我的一个例子:

 

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;
    }
';

 

生成静态文件

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

至于怎样生成静态文件?最简单的方式是将response的内容直接写到文件中,如果你是基于django的话,StaticGenerator可以帮你大忙了。

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

 

完整的配置文件例子如下: 点击nginx.conf下载

 

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;
  }
}

 

希望本文对你有用. ^_^

 

 

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Linux下安装Perl和Perl的DBI模块
    今天在虚拟机测试shell脚本的时候,有些命令使用不了。比如说 mysqlhotcopy ,它提示Perl的版本太低。我用的 RedHat9 的Perl才5.8.0版本。。。(2002年以前的)严重过时。所以重新安装了新版本的 Perl,过程记录如下: 1、在官方网站下载新版本的源码包:http:
    03-16
  • Perl 与Form
    说明事项: 這個範例用來說明如何經由網頁上的HTML form 表單元件來呼叫伺服器端的perl 程式。这个范例用来说明如何经由网页上的HTML form 表单元件来呼叫伺服器端的perl 程式。首先在網頁上設計表單元件,這個範例是設計一個按鈕,其原始碼如下:首先在网页
    02-10
  • Perl学习 perl培训
    http://www.sun126.com/perl5/perl5-1.htm翻译: flamephoenix 第一章 概述一、Perl是什么?二、Perl在哪里?三、运行四、注释一、Perl是什么?  Perl是Practical Extraction and Report Language的缩写,它是由Larry Wall设计的,并由他不断更新和维护,用
    02-10
  • - calm_水手">Perl中的箭头符-> - calm_水手
    Perl中的箭头符-2012-05-21 17:14 calm_水手 阅读(623) 评论(0) 编辑 收藏 举报  有两种用法,都和解引用有关。第一种用法,就是解引用。根据 - 后面跟的符号的不同,解不同类型的引用,-[] 表示解数组引用,-{} 表示解散列引用,-() 表示解子程序引
    02-09
  • Regex in Perl
    Regex in Perl
    regex literal   代表正则文字, 就是 m/regex/ 部分中的 regex, 这部分有自己的解析规则. 用 Perl 的行话就是 "表示正则含义的双引号字符串(regx-aware double-quoted string)", 及处理后传递给正则引擎的结果. 正则文字支持的特性:  1. 变量插值.    
    02-09
  • perl脚本语言学习 perl脚本调用perl脚本
    来公司的第二个星期便看了一下perl语言,发现掌握一门脚本语言还是非常有用的。到现在为止已经入职两个月,用perl脚本做了这些活:1. 修改了公司的一个爬取网页源代码的脚本2. 改进了一个出特征库的脚本,根据svn status的状态,来优化,将只需要添加的DB的数
    02-09
  • Perl模块的安装方法 perl 安装模块
    1. 下载离线安装包 *.tar.gz的形式解包后,#perl Makefile.PL#make#make install2. 在联网的情况下,通过CPAN安装# perl -MCPAN -e shellcpan install PAR::Packer 
    02-09
  • Perl像C一样强大,像awk、sed等脚本描述语言一
    Perl是由Larry Wall设计的,并由他不断更新和维护的编程语言。Perl具有高级语言(如C)的强大能力和灵活性。事实上,你将看到,它的许多特性是从C语言中借用来的。Perl与 脚本语言一样,Perl不需要编译器和链接器来运行代码,你要做的只是写出程序并告诉Perl
    02-09
  • 27-Perl 进程管理
    1.Perl 进程管理Perl 中你可以以不同的方法来创建进程。本教程将讨论一些进程的管理方法。你可以使用特殊变量 $$ 或 $PROCESS_ID 来获取进程 ID。%ENV 哈希存放了父进程,也就是shell中的环境变量,在Perl中可以修改这些变量。exit() 通常用于退出子进程,主
    02-09
  • 在perl中简单的正则匹配 正则匹配或的使用
    (一)、在perl中关于元字符的匹配元字符代表含义点号( .)匹配处换行符以外的任何单字符星号(*)匹配前面的内容零次或多次反斜线屏蔽元字符的特殊含义。\\代表\,\.匹配点号.*匹配所有的字符串加号(+)匹配前一个条目一次以上问号(?)表示前面一个条目可
    02-09
点击排行