Welcome! Log In Create A New Profile

Advanced

Hacking ngx_http_proxy_module

Posted by GZ1 
GZ1
Hacking ngx_http_proxy_module
May 30, 2014 11:59AM
Hello,

I'm modifying/extending proxy_pass so that the proxy url is dynamic, not fixed in a config file. Otherwise I want the rest of proxy_pass to behave the same.

Looking at nix_http_proxy_module.c I'm not finding where the url is actually changed. I thought I'd found it in ngx_http_proxy_host_variable(), so I put code in there to change the value of the host variable to what I wanted. But... that didn't work. I ran 2 tests: one with my forced value set (in code) and one unmodified proxy_pass with the proxy host set to the same value. In both cases I see my host variable's value set to what I wanted. My unmodified proxy_pass code processed the request successfully and hit the proxied server, but my code-set version fails with an INVALID_URL error when I hit my test endpoint. In both cases the host variable is set to the same value!

So clearly just setting the host value to what I need isn't the whole story. What am I missing? I find interesting code all over the place, particularly in ngx_http_proxy_create_request() for doing things with the URI, but not the host URL. :-(

(I do see where URL is set into conf on setup--that's not what I need. I need something called from a request handler so the proxy host can be determined dynamically.)

Some sample code from nix_http_proxy_module below:

u_char launchpad[] = "launchpad.appflighter.com";

static ngx_int_t
ngx_http_proxy_host_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_proxy_ctx_t *ctx;

// Added hook to plcf to determine if proxy_pass enabled w/my new dynamic host code.
ngx_http_proxy_loc_conf_t *plcf;
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);

ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);

if (ctx == NULL) {
v->not_found = 1;
return NGX_OK;
}

if( plcf->getDynamicURL == NULL ) {
// orig behavior
v->len = ctx->vars.host_header.len;
v->data = ctx->vars.host_header.data;
} else {
// normally I'd call my code here, but hard-coding for now to the same value used in unmodified test
v->len = strlen((char*)launchpad);
v->data = launchpad;
}
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;

// Generate some output to see what the value was set to
char c[256] = "";
strncat( (char*)c, (const char*)v->data, v->len);
ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "Route URL %d: %s",v->len,c);

return NGX_OK;
}

Any guidance appreciated!
GZ
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 204
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready