使用lua+openresty实现nginx动态反代

#user  nobody;
worker_processes  1;

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

pid        logs/nginx.pid;
events {
    worker_connections  1024;
}

stream {
    server {
        listen 192.168.20.254:21;
        proxy_pass ftp_cluster;
        proxy_buffer_size 16k;

    }
   upstream ftp_cluster{
        zone ftp_cluster 64k;
        server 120.92.141.156:21;
    }
}

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" "$request_body"';

    lua_need_request_body on;
    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	# proxy_temp_path /usr/local/openresty/nginx/proxy_temp_dir 1 2;
		
	# keys_zone=cache1:100m 表示这个zone名称为cache1,分配的内存大小为100MB
	# /usr/local/nginx/proxy_cache_dir/cache1 表示cache1这个zone的文件要存放的目录
	# levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录是2个字符,即/usr/local/nginx/proxy_cache_dir/cache1/a/1b这种形式
	# inactive=1d 表示这个zone中的缓存文件如果在1天内都没有被访问,那么文件会被cache manager进程删除掉
	# max_size=10g 表示这个zone的硬盘容量为10GB
											#
	# proxy_cache_path  /usr/local/openresty/nginx/proxy_cache_dir/cache1  levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g;    
    server {
        listen       10086;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

location ^~ /ody/swift/agent {
set $agentip '';
access_by_lua '
ngx.log(ngx.NOTICE, "--- 1 ---")
ngx.req.read_body()
local args = ngx.req.get_post_args()
for key, val in pairs(args) do
if type(val) == "table" then
ngx.log(ngx.NOTICE, "--- 2 ---")
ngx.log(ngx.NOTICE,key, ": ", table.concat(val, ", "))
else
ngx.log(ngx.NOTICE, "--- 3 ---")
ngx.log(ngx.NOTICE,key)
ngx.log(ngx.NOTICE,val)
local cjson = require("cjson")
local j = cjson.decode(key)
ngx.log(ngx.NOTICE,j.ip)
ngx.var.agentip = j.ip
end
ngx.log(ngx.NOTICE, "--- 4 ---")
end
';
proxy_pass http://$agentip:33433;
   proxy_connect_timeout 600s;
   proxy_send_timeout 600s;
   proxy_read_timeout 600s;
}

location ^~ /ody/swift/collo {
proxy_pass http://172.16.2.44:8080;
   proxy_connect_timeout 600s;
   proxy_send_timeout 600s;
   proxy_read_timeout 600s;
}

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

}