nginx的嵌入perl脚本的配置文件样例

user  apache apache;

worker_processes 8;
pid        /opt/modules/nginx/nginx.pid;

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

events
{
        use epoll;
        worker_connections 65535;
}

http
{
        include       mime.types;
        default_type  application/octet-stream;
        charset  utf-8;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 300m;

        sendfile on;
        tcp_nopush     on;

        keepalive_timeout 60;

        tcp_nodelay on;

        client_body_buffer_size  512k;
        proxy_connect_timeout    5;
        proxy_read_timeout       60;
        proxy_send_timeout       5;
        proxy_buffer_size        16k;
        proxy_buffers            4 64k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 128k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;

        #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
        proxy_temp_path   /opt/data/proxy_temp_dir;
        #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
        proxy_cache_path  /opt/data/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;

        log_format  main  '$request_uri,$status,$remote_addr,$http_x_forwarded_for,[$time_local],$body_bytes_sent';
        log_format  count  '$request_uri,$status,$http_x_forwarded_for,$remote_addr,[$time_local]';

        upstream storm_perl {
                server   192.168.1.87:8000 weight=1 max_fails=2 fail_timeout=30s;
                server   192.168.1.110:8000 weight=1 max_fails=2 fail_timeout=30s;
        }

        upstream backend_server {
                server   192.168.1.87:8080 weight=1 max_fails=2 fail_timeout=30s;
                server   192.168.1.110:8080 weight=1 max_fails=2 fail_timeout=30s;
        }


        perl_set $storm_key_new '
                use MIME::Base64::Perl;
                use URI::Escape;
                sub{
                        my $r = shift;
                        my $request_uri = $r->variable("request_uri");
                        my $query_uri = $r->unescape($request_uri);
                        
                        my $buffer = $query_uri;
                        my @pairs = split(/&/, $buffer);
                        foreach $pair (@pairs)
                        {
                                if($pair =~ /c=storm/i){
                                        $pair =~ s/c=//g;
                                        return $pair;
                                }
                        }
                }
        ';

        server
        {
                listen       80;
                server_name  storm.baofeng.net;
                index index.html index.htm index.php;
                root  /opt/baofeng-data/;
                
                location /
                {
                
                        expires 1h;
#                       add_header Cache-Control no-cache;
#                       add_header Cache-Control private;
                        
                        proxy_next_upstream http_502 http_504 error timeout invalid_header;
                        proxy_cache cache_one;
                        #对不同的HTTP状态码设置不同的缓存时间
                        proxy_cache_valid  200 304 30d;
                        proxy_cache_valid  404 1m;
                        #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
                        
                        proxy_cache_key $storm_key_new;
                        proxy_set_header Host  $host;
                        proxy_set_header X-Forwarded-For  $remote_addr;
                        proxy_pass http://storm_perl;

                        error_log  /opt/modules/nginx/logs/error.log crit;
                        access_log  /opt/modules/nginx/logs/access.log main;
                }

                location ~* ^/NginxStatus/? { 
                         stub_status on; #Nginx 状态监控配置
                         access_log off; 
                 } 
        }

        server
        {
                listen       8000;
                server_name  storm.baofeng.net;
                index index.html index.htm index.php;
                root  /opt/baofeng-data/;  

                set $memd 0;
                set $memcached_server "127.0.0.1:11211";

                location ~ .*\.(php|jsp|cgi)?
                {
                        perl 'sub {
                                use Cache::Memcached;
                                my $r = shift;
                                if($r->variable("memd")){
                                        my $memd = $r->variable("memd");
                                }
                                if(!$memd){
                                        $memd = new Cache::Memcached {"servers" => [$r->variable("memcached_server")],"debug" => 0,"compress_threshold" => 10_000};
                                        $r->variable("memd",$memd);
                                }

                                my $realkey = 0;
                                $realkey = $r->variable("storm_key_new");
                                my $stormbox = $memd->get($realkey);

                                if($stormbox){
                                        $r->send_http_header;
                                        $r->print($stormbox);
                                        return 200;
                                }else{
                                        return 404;
                                }
                        }';
                        error_page      404 500 502 503 504 = @get_storm;
                        access_log  /opt/modules/nginx/logs/log_mm.log count;
                }

                location @get_storm {
                        proxy_set_header Host  $host;
                        proxy_set_header X-Forwarded-For  $remote_addr;
                        proxy_pass http://backend_server;
                        access_log  /opt/modules/nginx/logs/log_db.log count;
                }
        }

                     
}

你可能感兴趣的:(nginx,cache,脚本,memcached,perl)