Forum: NGINX Rewrite assistance needed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John S. (Guest)
on 2009-05-05 07:59
Hi, im hoping i can get some insight into why the following rewrite
rules are not working (or if I am missing the boat alltogether in using
rewrites at all for this problem).

I am currently using nginx to support a backend website to serve static
files. In most cases everything is fine with the urls that come in the
format of:

/clientfiles/foldera/somecategory/somefile.doc

the problem however occurs when 2 specific requests come to the server
with the following requests:

/clientfiles/General%20Business/somecategory/somefile.doc
and
/clientfiles/Insurance%20&%20Finance/somecategory/somefile.doc

typically all the requests are mapped to a specific folder like so:

/clientfiles/Support/somecategory/somefile.doc would be mapped to
/var/www/data/clientfiles/Support/somecategory/somefile.doc

but the 2 specific urls above are problematic because of the spaces (in
both) and the ampersand (in one).

i've tried endless rewrite rules to make the 2 examples above point to a
different directory on the filesystem but nginx cannot find the file and
i get 404 errors. Filesystem is linux.

I have tried using a directory with spaces escaped with \ - no luck

Thanks in advance for any help.
John
Igor S. (Guest)
on 2009-05-05 09:23
(Received via mailing list)
On Tue, May 05, 2009 at 05:59:14AM +0200, John Stykes wrote:

> the problem however occurs when 2 specific requests come to the server
>
> but the 2 specific urls above are problematic because of the spaces (in
> both) and the ampersand (in one).
>
> i've tried endless rewrite rules to make the 2 examples above point to a
> different directory on the filesystem but nginx cannot find the file and
> i get 404 errors. Filesystem is linux.
>
> I have tried using a directory with spaces escaped with \ - no luck

If these files are served by nginx without backend participation,
the you need just

    location /clientfiles {
        root  /var/www/data;
    }
John S. (Guest)
on 2009-05-05 09:53
Hi Igor, thanks for the response.

I have that setting already. The problem is that when the request comes
with a space (%20) or an ampersand (&) it causes problems and nginx
cannot find that directory. So when a request comes for file1.doc in the
directory 'Client Files' it cannot find that directory even if i named
it Client\ Files on the filesystem.

This is why i thought perhaps i could rewrite the url or the directory
portion so that if a request comes for Client%20Files i simply use a
different directory called clientfiles with no spaces.

thanks
John


I have tried using a directory such as

Igor S. wrote:
> On Tue, May 05, 2009 at 05:59:14AM +0200, John Stykes wrote:
>
>> the problem however occurs when 2 specific requests come to the server
>>
>> but the 2 specific urls above are problematic because of the spaces (in
>> both) and the ampersand (in one).
>>
>> i've tried endless rewrite rules to make the 2 examples above point to a
>> different directory on the filesystem but nginx cannot find the file and
>> i get 404 errors. Filesystem is linux.
>>
>> I have tried using a directory with spaces escaped with \ - no luck
>
> If these files are served by nginx without backend participation,
> the you need just
>
>     location /clientfiles {
>         root  /var/www/data;
>     }
Igor S. (Guest)
on 2009-05-05 10:36
(Received via mailing list)
On Tue, May 05, 2009 at 07:53:22AM +0200, John Stykes wrote:

> different directory called clientfiles with no spaces.
Could you show the error_log message about these files ?
nginx uses decoded names.
Payam C. (Guest)
on 2009-05-05 10:47
(Received via mailing list)
2009/5/4 Igor S. <removed_email_address@domain.invalid>:
>> This is why i thought perhaps i could rewrite the url or the directory
>> I have tried using a directory such as
>> >> different directory on the filesystem but nginx cannot find the file and
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>
> --
> Igor S.
> http://sysoev.ru/en/
>
>

could you not look at the request then do a re-write and match for  if
( $string =~ s/\\s/\%20/) { ......}  ?
There is probably a way more elegant way to do this...
Igor S. (Guest)
on 2009-05-05 10:54
(Received via mailing list)
On Mon, May 04, 2009 at 11:39:14PM -0700, Payam C. wrote:

> >>
> >>
> >> >> i've tried endless rewrite rules to make the 2 examples above point to a
> >> > š š }
> could you not look at the request then do a re-write and match for  if
> ( $string =~ s/\\s/\%20/) { ......}  ?
> There is probably a way more elegant way to do this...

I do not undestand the suggestion.
The problem with all these escapes and encodings, that they must be done
only once.
Marcus C. (Guest)
on 2009-05-05 11:03
(Received via mailing list)
Hi John,

Is the problem that Nginx is URL-encoding the whole string, including
&'s, so that you'd have to name your directories with %20 or %26 (for &)
in the directory names?  I believe both (space) and %20 will go to %20 ,
the same for &, though I may be wrong.

I think you'd need to name your directory Client%20Files for it to work,
and if you have an ampersand, you'd need to use %26.

I've had this problem before when proxying from Nginx to PBMS - it
wouldn't work because the & was URL-encoded and PBMS can (or could at
that time) only use non-url-encoded &'s.

On Apache and Litespeed (which uses Apache's rewrite rules) there's a
re-write option to add to prevent URL-encoding ([NE]), but I don't
believe there is for Nginx (or at least I didn't know about one when I
was looking at it).

That option may have been added since,  or if not, it might be something
useful to add. Something like:

rewrite ... nourlencode;

Cheers,

Marcus.
This topic is locked and can not be replied to.