Welcome! Log In Create A New Profile


undoing rewrite (for proxy_pass)

Thorben Thuermer
February 27, 2012 12:46PM

i recently tried to set up nginx as reverse-proxy in front of apache
to offload serving of cached static versions of pages,
where i needed to apply a rewrite to construct the local path of the
cache files:
* receive incoming requests
* apply a rewrite-rule, use try_files to serve files if they exist
* otherwise proxy_pass the original request to apache

the problem i ran into was, that i needed to pass the original unprocessed
request to apache, as it may contain script parameters that get broken by
(the best reference to this issue that i found is this:
http://mailman.nginx.org/pipermail/nginx/2010-April/019905.html )

as i do not even need to forward a rewritten version,
this solution should apply:
"If it is necessary to transmit URI in the unprocessed form then directive
proxy_pass should be used without URI part:"
-- http://wiki.nginx.org/NginxHttpProxyModule#proxy_pass

but this does not work anymore after a rewrite was applied, as the
rewritten version will be transmitted.
the natural method to undo a rewrite would appear to be:
rewrite .* $request_uri break;
but this replaces the request with a processed version!

i found the code for constructing the request in:
the if-block starting at line ~910 appears to pick the request-uri
to be used in the request.
there the problem appears to be that r->valid_unparsed_uri is no longer
set after rewrites were applied, so it will never choose to use the
unparsed uri!

i simply ignored r->valid_unparsed_uri, and replaced the whole block with:
unparsed_uri = 1;
uri_len = r->unparsed_uri.len;
and now my reverse-proxying works as required.
(ofcourse i broke the other cases by hardcoding this, but that's not an
issue for me.)

i am wondering if there might be interest in including a proper solution
for this situation in nginx.
(or maybe there is one already and i missed it?)

Thorben Thuermer

when working on this, i spent most time on finding out how to get nginx
running in gdb - is there some introduction to development documentation
where such information might be found/useful?

before anybody asks, the configuration:
server {
server_name ...;
root /var/www/...;
location / {
rewrite "^(lots of stuff here)$" /cache/(lots of submatches) break;
try_files $uri $uri/ @apache;
location @apache {
rewrite ^(.*)$ $request_uri break; # useless
proxy_pass http://localhost;

nginx mailing list
Subject Author Posted

undoing rewrite (for proxy_pass)

Thorben Thuermer February 27, 2012 12:46PM

Re: undoing rewrite (for proxy_pass)

Maxim Dounin February 27, 2012 04:14PM

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 273
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