Forum: NGINX Getting rewritten and encoded/escaped url in nginx module

Fb6843bebac553caa3b78475a657b33b?d=identicon&s=25 Jayadev C (Guest)
on 2014-06-03 18:43
(Received via mailing list)
Hi,


I am writing a nginx proxy module and want to grab the url which is
urlencoded (as the client sends it) and also after rewrite rules are
applied. My typical url looks like :
path1/path2/path3/urlencoded(key)?args, after rewriting the url I would
love to have is something like :

newpath1/newpath2/newpath3/../urlencoded(key)?args.

Currently , r->uri is decoded rewritten uri, r->unparsed_uri is encoded
but not rewritten.

I read on the forum that nginx decodes the url for rewrite, is there a
handy internal function I can use encode the rewritten url back. A
simple use of ngx_escape_uri(r->uri) with different parameters doesn't
do what I want out of the box.


Thanks,
Jayadev
1266aa99d1601b47bbd3ec22affbb81c?d=identicon&s=25 B.R. (Guest)
on 2014-06-03 19:10
(Received via mailing list)
I am not providing a direct answer but could not you use some standard
modules to do that?

Such as using (examples):
- rewrite associated with a map, loaded from a separate configuration
file
reloaded after changes
- the perl module
<http://nginx.org/en/docs/http/ngx_http_perl_module.html>
to invoke external perl scripts doing that for you, maybe in conjunction
with the ssi module
<http://nginx.org/en/docs/http/ngx_http_ssi_module.html>
?
---
*B. R.*
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2014-06-03 19:17
(Received via mailing list)
Hello!

On Tue, Jun 03, 2014 at 09:42:41AM -0700, Jayadev C wrote:

>
> newpath1/newpath2/newpath3/../urlencoded(key)?args.
>
> Currently , r->uri is decoded rewritten uri, r->unparsed_uri is
> encoded but not rewritten.
>
> I read on the forum that nginx decodes the url for rewrite, is
> there a handy internal function I can use encode the rewritten
> url back. A simple use of ngx_escape_uri(r->uri) with different
> parameters doesn't do what I want out of the box.

The ngx_escape_uri(r->uri) _is_ the internal function you should
use if you need encoded URI.

If in doubt, try looking, e.g., into the ngx_http_proxy_create_request()
function to see how it's done there.

--
Maxim Dounin
http://nginx.org/
Fb6843bebac553caa3b78475a657b33b?d=identicon&s=25 Jayadev C (Guest)
on 2014-06-03 23:09
(Received via mailing list)
The problem is ngx_http_proxy doesn't do that either, once rewrite is
applied the url remains decoded (or I am not reading the code
correctly). In fact escaping uri back is a bit tricky since you have to
split the rewritten url to segments and apply encoding again (which
would require looking at the original url to know the correct number of
'/'s).


Anybody had attempted doing something like this ? or is there an easier
way.

Jayadev



On Tuesday, June 3, 2014 10:17 AM, Maxim Dounin <mdounin@mdounin.ru>
wrote:



Hello!

On Tue, Jun 03, 2014 at 09:42:41AM -0700, Jayadev C wrote:

>
> newpath1/newpath2/newpath3/../urlencoded(key)?args.
>
> Currently , r->uri is decoded rewritten uri, r->unparsed_uri is
> encoded but not rewritten.
>
> I read on the forum that nginx decodes the url for rewrite, is
> there a handy internal function I can use encode the rewritten
> url back. A simple use of ngx_escape_uri(r->uri) with different
> parameters doesn't do what I want out of the box.

The ngx_escape_uri(r->uri) _is_ the internal function you should
use if you need encoded URI.

If in doubt, try looking, e.g., into the ngx_http_proxy_create_request()
function to see how it's done there.

--
Maxim Dounin
http://nginx.org/
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2014-06-04 17:05
(Received via mailing list)
Hello!

On Tue, Jun 03, 2014 at 02:08:36PM -0700, Jayadev C wrote:

> The problem is ngx_http_proxy doesn't do that either, once
> rewrite is applied the url remains decoded (or I am not reading
> the code correctly).

The proxy does ngx_escape_uri() if URI was rewritten.  It has to,
as unencoded URI can't be used in a HTTP request.  Take a look at
the ngx_http_proxy_create_request() function as previously
suggested.

>In fact escaping uri back is a bit tricky
> since you have to split the rewritten url to segments and apply
> encoding again (which would require looking at the original url
> to know the correct number of '/'s).

As long as there were encoded slashes, the encoding of these
slashes will be lost if URI was rewritten.  This is the expected
behaviour.

--
Maxim Dounin
http://nginx.org/
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.