短地址服务(二)

编写lua脚本split.lua

function split(str,pat)
    local t = {}
    local fpat = "(.-)" .. pat
    local last_end = 1
    local s,e,cap = str:find(fpat,1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(t,cap)
        end
        last_end = e+1
        s,e,cap = str:find(fpat,last_end)
    end
    if last_end <= #str then
        cap = str:sub(last_end)
        table.insert(t,cap)
    end
    return t
end

function split_path(str)
    return split(str,'[\\/]')
end

在nginx.conf中脚本如下:

redis无密码:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    #keepalive_timeout  65;

    #gzip  on;
    init_worker_by_lua_file D:/split.lua;
    server {
        listen       80;
        location = /redis {
            internal;
            set_unescape_uri $key $arg_key;
            redis2_query get $key;
            redis2_pass 127.0.0.1:6379;
        }

        location / {
            set $target '';            
            access_by_lua '
        local parameters = split_path(ngx.var.uri)
        local action = parameters[1]
        if(#parameters == 0) then
                   ngx.exit(ngx.HTTP_FORBIDDEN)
        end
        
        local key = action
        local res = ngx.location.capture(
                    "/redis", { args = { key = key } }
        )if res.status ~= 200 then
                    ngx.log(ngx.ERR, "redis server returned bad status: ",
                        res.status)
                    ngx.exit(res.status)
        end

        if not res.body then
                    ngx.log(ngx.ERR, "redis returned empty body")
                    ngx.exit(500)
        end

        local parser = require "redis.parser"
        local server, typ = parser.parse_reply(res.body)
        if typ ~= parser.BULK_REPLY or not server then
                    ngx.log(ngx.ERR, "bad redis response: ", res.body)
                    ngx.exit(500)
        end

        if server == "" then
                    server = "default.com"
        end
        server = server
        if ngx.var.QUERY_STRING ~= nil and ngx.var.QUERY_STRING ~= "" then
          server = server .."&"..ngx.var.QUERY_STRING
        end

        ngx.var.target = server        
       ';
       resolver 8.8.8.8;               
       proxy_pass http://$target;

        #server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    }

}

redis有密码(持续优化中):

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    #keepalive_timeout  65;

    #gzip  on;
    init_worker_by_lua_file D:/split.lua;
    #init_worker_by_lua_file D:/redis_iresty.lua;
    server {
        listen       80;
        
        location / {
            set $target '';            
            access_by_lua '
        local parameters = split_path(ngx.var.uri)
        local action = parameters[1]
        if(#parameters == 0) then
                   ngx.exit(ngx.HTTP_FORBIDDEN)
        end
        local function pool_redis(red)  
            if not red then  
                return  
            end  
            --释放连接(连接池实现)  
            local pool_max_idle_time = 10000 --毫秒  
            local pool_size = 500 --连接池大小  
            local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)  
            if not ok then  
                ngx.say("set keepalive error : ", err)  
            end  
        end  
        local redis = require "resty.redis"
        local red = redis:new()

        red:set_timeout(6000)

        local ok, err = red:connect("redis地址", 6379)
        if not ok then
            ngx.say("failed to connect: ", err)
            return pool_redis(red)
        end

        local res, err = red:auth("redis密码")
        if not res then
            ngx.say("failed to authenticate: ", err)
            return pool_redis(red)
        end
        local server, err = red:get(action) 
        if not server then
            ngx.log(ngx.ERR, "bad redis response: ", server)
            ngx.exit(500)
            return pool_redis(red)  
        end
        if server == "" then
                    server = "default.com"
        end
        server = server
        if ngx.var.QUERY_STRING ~= nil and ngx.var.QUERY_STRING ~= "" then
          server = server .."&"..ngx.var.QUERY_STRING
        end

        ngx.var.target = server        
       ';
        resolver 8.8.8.8;
        proxy_pass $target;
        #server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    }

}

 

posted @ 2019-03-05 09:55  瑾谦  阅读(606)  评论(0编辑  收藏  举报