Nginx Transparent URL rewrite

416 views
Skip to first unread message

Rastin Mehr

unread,
Apr 21, 2015, 1:17:38 AM4/21/15
to nooku-f...@googlegroups.com
Hello Nooku Tribe,

in my Nginx server I have a url type 1



and I want it to rewrite the request to url type 2



without url type 1 being changed in the address bar.

I have tried several approaches in the config file, but no results. I appreciate your help.

Johan Janssens

unread,
Apr 21, 2015, 8:05:30 AM4/21/15
to nooku-f...@googlegroups.com
Hi Rastin,

You wanna do this on a Nginx level ? Why not do it in your routing in PHP ? If in Nginx it should be as easy as do an if statement checking if /component/ is in the url and if so dropping it. What approach have you tried so far ?

Happy coding,

Johan

Rastin Mehr

unread,
Apr 21, 2015, 4:58:03 PM4/21/15
to nooku-f...@googlegroups.com
Doing it at the Nginx level would be much cleaner. This is for a mobile app that connects to Anahita via API. Anahita 2.0 had a /component/ in urls and Anahita 4.0 doesn't. The mobile app still sends requests with the /component/ and still thinks that http://example.com/component is the base url. We don't have the option of updating the mobile app, so redirecting the requests that contain /component/ to the ones without in a transparent fashion would be the next elegant approach.

I've tried:

location /component/ {
    rewrite ^/component/(.*)$ $scheme://www.example.com/$1 permanent;
}

but that redirects and changes the url. When that happens the mobile app compares it with the url that it requested and throws an error, thinking that it is the wrong base url. How would you change this rewrite?

Johan Janssens

unread,
Apr 21, 2015, 5:29:05 PM4/21/15
to nooku-f...@googlegroups.com
Hi Rastin,

Using rewrite in combination with permanent will trigger a redirect. You can either try to use rewrite in combination with last, if you want this to be the last rule to be processed, however I think the best solution for you would be to use alias.


Does that help you ?

Johan

Rastin Mehr

unread,
Apr 21, 2015, 6:23:37 PM4/21/15
to nooku-f...@googlegroups.com
I think alias is the correct choice if I can get the expressions right. The following configuration seems correct, but it redirects back to the homepage. What am I doing wrong?

location ~ ^/component/(.*)$ {
    alias /var/www/example.com/www/abc/$1;
}

Rastin Mehr

unread,
Apr 21, 2015, 7:01:49 PM4/21/15
to nooku-f...@googlegroups.com
and this didn't work either:

location /component/(.*)$ {
    alias $scheme://example.com/abc/$1;
}

Steven Rombauts

unread,
Apr 22, 2015, 4:46:32 AM4/22/15
to nooku-f...@googlegroups.com
Hi Rastin,

The alias directive offers the same functionality as root, so you have to use local (absolute) filepaths for it. Alias is also used for mapping files on the file system, not rewriting http paths. If I understand this correctly, you still want whatever is behind that path to be processed by PHP, so it should still redirect to your .php location block and send off to PHP-FPM. So you need rewrite instead of an alias.

Johan's suggestion about using rewrite in combination with the last flag should be the right answer. What you don't want is to include the scheme and hostname in your rewrite. If you do, Nginx will stop processing and automatically return an (external) redirect to the client. Just rewrite to a relative path.

The highlighted lines in Nooku Platform's nginx.conf file do exactly what you are looking for. So in your example, the following should work. In theory ;)

location /component/ {
    rewrite ^(.*) /$1 last;
}

Hope that helps!

Cheers,
Steven

--

---
You received this message because you are subscribed to the Google Groups "Nooku Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nooku-framewo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rastin Mehr

unread,
Apr 22, 2015, 4:03:33 PM4/22/15
to nooku-f...@googlegroups.com, ste...@kotuha.be
Thank you Steve for the informative description you provided.

I tried:

location /component/ {
rewrite ^(.*) /$1 last;
}

and it takes the urls containing /component/ to 404 page now. What does $1 represent and why do we have ^(.*) before it which means zero or many number of any type of character right?

Steven Rombauts

unread,
Apr 25, 2015, 9:18:00 AM4/25/15
to nooku-f...@googlegroups.com
Hi Rastin,

I copied that from your last example and didn't look at your original post.The rewrite line explained:

- The first part ^(.*) is a regular expression that will grab everything from the start of the URI path to the end. The ^ symbol matches the start of the string, . matches any character, and as you said * means "the previous character zero or many times". The parentheses around it form a capturing group so we can reuse it later 
- The second part $1 refers to that capturing group. 

To go back to your original post, I think this should be the correct version:

location /component/ {
rewrite ^/component/(.*) /$1 last;
}

So if we break this down:

- if the path is /component/abc/categories, we should end up in the location block because it starts with /component
- the write rule will match ^/component/(.*) - meaning that abc/categories should be captured
- we then rewrite the path /component/abc/categories to simply /abc/categories 

Let me know if that works :)

Cheers,
Steven
Message has been deleted

Rastin Mehr

unread,
Apr 25, 2015, 4:17:38 PM4/25/15
to nooku-f...@googlegroups.com, ste...@kotuha.be
Thank you so much Steven. I will test that today and let you know. That could save us an app resubmission for a mere url change.

Rastin Mehr

unread,
Apr 25, 2015, 4:20:44 PM4/25/15
to nooku-f...@googlegroups.com, ste...@kotuha.be
May I please ask that you to edit and remove the url to the project. Since this is a public post and perhaps our Client won't appreciate having the link to staging site posted here. I appreciate it.

Steven Rombauts

unread,
Apr 26, 2015, 9:21:52 AM4/26/15
to nooku-f...@googlegroups.com, ste...@kotuha.be
Great, hope you get it solved now.

I removed the previous post containing the staging URL. Sorry about that, did not realise this might be an issue.


Rastin Mehr

unread,
Apr 29, 2015, 5:26:49 PM4/29/15
to nooku-f...@googlegroups.com, ste...@kotuha.be
Steven, with a bit of manipulation the example that you provided worked. Thank you so much for your help!
Reply all
Reply to author
Forward
0 new messages