Welcome! Log In Create A New Profile


rewrite not working

Posted by schmoove 
rewrite not working
September 01, 2009 04:09PM
I have a rewrite rule that works with Apache's mod_rewrite but not with nginx rewrite. I find this peculiar, since I thought both use a PCRE lib. Perhaps someone can help me getting this PCRE expression to work with with nginx rewrite:

rewrite "^/([a-zA-Z]{2,4})\/([0-9]{0,3})\/([0-9a-zA-Z]{0,6})\/([a-zA-Z0-9\-_,]*)\/(.*)\.png$" /test.bar;

here is an example url that does not get rewritten via this rule:

Re: rewrite not working
September 05, 2009 04:43AM
Delete the quotes and provide nginx with a statement of how to handle the rewrite: last or permanent?
last would be a simple rewrite without any status code and permanent will redirect the user (301 Moved Permanently).
Also type a break; statement after the rewrite rule.

So in the end, you would have one of the following:
rewrite ^/([a-zA-Z]{2,4})\/([0-9]{0,3})\/([0-9a-zA-Z]{0,6})\/([a-zA-Z0-9\-_,]*)\/(.*)\.png$ /test.bar last;
rewrite ^/([a-zA-Z]{2,4})\/([0-9]{0,3})\/([0-9a-zA-Z]{0,6})\/([a-zA-Z0-9\-_,]*)\/(.*)\.png$ /test.bar permanent;

Please tell us if this worked.

Re: rewrite not working
September 14, 2009 05:33AM
Removing the quotes results in an error: 'directive "rewrite" is not terminated by ";"'. I have got the rewrite to work (sort of) with quotes by now.

With some further testing I have encountered the following behavior: It seems as if the problem is with the quantifiers {0,3} and {0,6}. The rewrite works when both quantifiers match 1+ times and fail when matched 0 times. I am using the mentioned rewrite rule before relaying to another server via proxy_pass. I have noticed that the URI /ttf//000000//action-man.regular.png changes into /ttf/000000/action-man.regular.png when being received by the second server. I am now assuming that nginx applies a canonicalization function on URIs before passing them down the line for further processing as with my rewrite?!? If so, that would be bad practice IMHO. Uff :(

Anybody know how I could test this? I would like to find out where '//' is being modified into '/' in my URI? I assume it is happening before my nginx rewrite as this would explain my rewrites failing.
Re: rewrite not working
September 25, 2009 03:26AM
OK, I have given up on this. I am now parsing my request URI directly in my web-app instead of relying on nginx rewrite.

I have received some helpful information from some nice guys in freenode #nginx, suggesting to parse the $request_uri var in nginx which might not have been canonicalized, but I haven't tried that.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 46
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready