location / {
	proxy_pass http://{$UPSTREAM_NAME};

	client_max_body_size 10m;
	client_body_buffer_size 128k;

	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header Host $proxy_host;
	proxy_connect_timeout 90;
	proxy_send_timeout 90;
	proxy_read_timeout 90;
	proxy_buffer_size 4k;
	proxy_buffers 4 32k;
	proxy_busy_buffers_size 64k;
	proxy_temp_file_write_size 64k;
}


#location /upstream_status {
#    allow 127.0.0.1;
#    deny all;
#    access_log off;
#    default_type text/plain;
#    content_by_lua_block {
#        local hc = require "resty.upstream.healthcheck"
#        ngx.say("OpenResty Worker PID: ", ngx.worker.pid())
#        ngx.print(hc.status_page())
#    }
#}

location /upstream_status_{$UPSTREAM_NAME} {
    allow 127.0.0.1;
    deny all;
    access_log off;
    default_type text/plain;
    content_by_lua_block {
    	local json = require "cjson"
    	local ok, upstream = pcall(require, "ngx.upstream")
		if not ok then
			ngx.print("[]")
			return
		end

	    local get_primary_peers = upstream.get_primary_peers
		local get_backup_peers = upstream.get_backup_peers
	    local upstream_name = "{$UPSTREAM_NAME}"

	    peers, err = get_primary_peers(upstream_name)
	    if not peers then
            ngx.print("[]")
            return
        end

	    peers_backup, err = get_backup_peers(upstream_name)
	    if peers_backup then
            for k, v in pairs(peers_backup) do
	        	table.insert(peers,v)
	        end
        end

	    ngx.print(json.encode(peers))
    }
}