determining the url of a request

Maxim Dounin mdounin at mdounin.ru
Wed Oct 17 09:02:27 UTC 2012


Hello!

On Tue, Oct 16, 2012 at 03:02:11PM -0400, Jeff Kaufman wrote:

> If I have a ngx_http_request_t, is there a built-in way to get the
> full url for the request? r->uri has "/path/to/page.html" but is there
> a way to get a full "scheme://host[:port]/path/to/page.html"?

As you probably know, in most cases there is no full URL in HTTP 
requests.  It's broken down into URI in a request line and the 
Host header (if present).  The "scheme" part usually isn't sent at 
all, and should be determined from other factors.  So the 
reconstruction of a full URL isn't something trivial (and not 
always possible).

In nginx original URI as sent by a client is available as 
r->unparsed_uri (that is, "/path?arguments" part).  Parsed URI 
(and probably changed due to internal redirects) is available as 
r->uri.  Note it's unescaped and doesn't include query string, 
which is available separately as r->args.

The Host header, if present, is available in r->headers_in->host.  
You may want to use r->headers_in.server though (it's normalized 
and includes hostname if it's sent in a request line).  You 
probably want to fallback to local ip address or a server name if 
it's not present.  (You may want to look at ngx_http_variable_host() 
in ngx_http_variables.c, and at the Location header construction 
in ngx_http_header_filter() in ngx_http_header_filter_module.c)

The scheme may be determined from r->connection->ssl presense, see 
e.g. ngx_http_variable_scheme() in ngx_http_variables.c.

-- 
Maxim Dounin
http://nginx.com/support.html



More information about the nginx-devel mailing list