# Spaces in URI

**URL:** <https://www.ruby-forum.com/t/spaces-in-uri/190007>\
**Category:** NGINX\
**Created:** [June 11, 2010, 10:10am UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007 "2010-06-11T10:10:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [June 11, 2010, 10:10am UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/1 "2010-06-11T10:10:09Z")

</div>

On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote:

> switched our environment to front those Apache servers with a pair of Nginx  
> or configuration parameter in Nginx that would make it more forgiving of
> 
> Thanks for any help or pointers you can offer.

Try the attached patch against 0.8.40.

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [June 11, 2010, 10:07pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/2 "2010-06-11T22:07:59Z")

</div>

Hi Igor,

This is working great. Thank you!

Should we be prepared to maintain this as a patch on the 0.8 stream, or  
would some variation of this fix be folded into 0.8 (via some config  
setting  
or as the new behavior)?

And while we are at it, how stable is 0.8? I thought I had seen some  
discussion of 0.8 being marked “stable” soon. Just curious.

Thanks,

-peter

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [June 11, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/3 "2010-06-11T22:54:49Z")

</div>

Great. Thanks Igor, we are using it. 🙂 -peter

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [June 11, 2010, 10:36pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/4 "2010-06-11T22:36:05Z")

</div>

On Fri, Jun 11, 2010 at 04:07:10PM -0400, Peter Portante wrote:

> Hi Igor,
> 
> This is working great. Thank you!
> 
> Should we be prepared to maintain this as a patch on the 0.8 stream, or  
> would some variation of this fix be folded into 0.8 (via some config setting  
> or as the new behavior)?
> 
> And while we are at it, how stable is 0.8? I thought I had seen some  
> discussion of 0.8 being marked “stable” soon. Just curious.

Here is a new patch that encodes these spaces as %20 while talking  
to a proxied backend. I’m going to commit the patch in next 0.8.41.  
0.8 is stable, use it.

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 4:25pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/5 "2010-09-28T16:25:24Z")

</div>

Hello folks,

I’m tried both nginx 0.8.46 and 0.8.52 to reverse proxy a URI with a  
whitespace, but I’m having no luck. Any URI without a space loads just  
fine, but one that contains a white space results in a “Bad Request -  
HTTP Error 400. The request is badly formed.” I verified that the  
patch reference above was in 0.8.46. [The %20 substitution was missing  
from the proxy module so I added it but it didn’t help.]

Are you folks about to reverse proxy a white space URI like  
“[Nginx Forum](http://forum.nginx.org/siteX/storage/Nice) Images/test-image.jpg”  
That’s not a valid URI. I just wanted to give an example that shows the  
type of white space that is in my URI.

thanks,  
Chris

Posted at Nginx Forum:

> **[Re: spaces in URI](https://forum.nginx.org/read.php?2,97031,134984#msg-134984)**
>
> On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote: \> Hi Folks, \> \> We are running into a small problem with bad HTTP clients. \> \> We have sold a bunch of hardware with embedded HTTP clients which don't \> URL-encode the...

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 4:36pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/6 "2010-09-28T16:36:32Z")

</div>

Reposting to fix the grammar errors!

Hello folks,

I have tried both nginx 0.8.46 and 0.8.52 to reverse proxy a URI that  
contains a whitespace, but I’m having no luck. Any URI without a space  
loads just fine, but one that contains a white space results in a “Bad  
Request - HTTP Error 400. The request is badly formed.”

I had verified that the patch referenced above was in the source of  
0.8.46. [The %20 substitution was missing from the proxy module so I  
added it, but adding that substitution didn’t help.]

Are any of you folks able to reverse proxy a white space URI like  
“[Nginx Forum](http://forum.nginx.org/siteX/storage/Nice) Images/test-image.jpg” That’s  
not a valid URI. I just wanted to give an example that shows the type of  
white space that is in my URI.

thanks,  
Chris

Posted at Nginx Forum:

> **[Re: spaces in URI](https://forum.nginx.org/read.php?2,97031,134985#msg-134985)**
>
> On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote: \> Hi Folks, \> \> We are running into a small problem with bad HTTP clients. \> \> We have sold a bunch of hardware with embedded HTTP clients which don't \> URL-encode the...

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/7 "2010-09-28T18:39:28Z")

</div>

Thanks Matthieu. I’ve applied your patch, but unfortunately the  
behaviour remains the same.

This format of link doesn’t work. It reports a badly formed request:  
[http://www.xxxxx.xxx/us/storage/113/images/Member%20Images/image-2-web.png](http://www.xxxxx.xxx/us/storage/113/images/Member%20Images/image-2-web.png)

But this better formatted URI does:  
[http://www.xxxx.xxx/us/storage/113/images/blog/child-eating-health-food.jpg](http://www.xxxx.xxx/us/storage/113/images/blog/child-eating-health-food.jpg)

While it’d be nice to eliminate all white space from the source URI, I  
do not have control over the source file structure/system.

Posted at Nginx Forum:

> **[Re: spaces in URI](https://forum.nginx.org/read.php?2,97031,135016#msg-135016)**
>
> On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote: \> Hi Folks, \> \> We are running into a small problem with bad HTTP clients. \> \> We have sold a bunch of hardware with embedded HTTP clients which don't \> URL-encode the...

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 7:15pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/8 "2010-09-28T19:15:48Z")

</div>

As I said, my patch modifies the behavior to accept single ‘%’  
(percent) in the url, not spaces. But maybe you can take inspiration  
from that and apply it to your own case.

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/9 "2010-09-28T18:00:16Z")

</div>

Hi,

I was in a similar situation trying to reverse proxy to a site that  
has single percent signs in the url, which aren’t part of a url  
encoding.  
I did a quick patch to allow that, maybe this can help you.

Matthieu.

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/10 "2010-09-28T19:19:54Z")

</div>

Glad to do so!

from nginx.conf  
upstream backendcms {  
server 172.26.161.230;  
}

from my location specific conf  
location ~\* ^/us(.\*)? {  
proxy\_set\_header Host xxx.xxx.xxx;  
proxy\_pass [http://backendcms$1](http://backendcms$1);  
subs\_filter …  
}

It truly is the white-space causing the issue because I copied the  
“Member Images” folder to “Member-Images”. Trying the URI with the  
hypen works and with the %20 does not.

Posted at Nginx Forum:

> **[Re: spaces in URI](https://forum.nginx.org/read.php?2,97031,135025#msg-135025)**
>
> On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote: \> Hi Folks, \> \> We are running into a small problem with bad HTTP clients. \> \> We have sold a bunch of hardware with embedded HTTP clients which don't \> URL-encode the...

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 7:21pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/11 "2010-09-28T19:21:04Z")

</div>

On Tue, Sep 28, 2010 at 01:06:36PM -0400, cgarver wrote:

> ```
> proxy_pass http://backendcms$1;
> subs_filter .....
> 
> ```
> 
> }
> 
> It truly is the white-space causing the issue because I copied the  
> “Member Images” folder to “Member-Images”. Trying the URI with the  
> hypen works and with the %20 does not.

The 400 error is sent by backend because backend does not support  
unencoded  
spaces in URI. nginx can espace it, if you will use

location ^~ /us {  
proxy\_pass [http://backendcms/](http://backendcms/);  
proxy\_set\_header Host xxx.xxx.xxx;  
subs\_filter …  
}

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/12 "2010-09-28T19:25:10Z")

</div>

Thanks, but when I try that I cannot get nginx to start.

> /etc/init.d/nginx restart  
> [emerg]: “proxy\_pass” may not have URI part in location given by regular  
> expression, or inside named location, or inside the “if” statement, or  
> inside the “limit\_except” block in /etc/nginx/national\_sites.conf:4  
> configuration file /etc/nginx/nginx.conf test failed

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,97031,135037#msg-135037](http://forum.nginx.org/read.php?2,97031,135037#msg-135037)

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 7:27pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/13 "2010-09-28T19:27:31Z")

</div>

On Tue, Sep 28, 2010 at 01:24:25PM -0400, cgarver wrote:

> Thanks, but when I try that I cannot get nginx to start.
> 
> > /etc/init.d/nginx restart  
> > [emerg]: “proxy\_pass” may not have URI part in location given by regular  
> > expression, or inside named location, or inside the “if” statement, or  
> > inside the “limit\_except” block in /etc/nginx/national\_sites.conf:4  
> > configuration file /etc/nginx/nginx.conf test failed

You should use

```
  location ^~ /us {

```

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 7:15pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/14 "2010-09-28T19:15:48Z")

</div>

On Tue, Sep 28, 2010 at 10:24:03AM -0400, cgarver wrote:

> “[http://forum.nginx.org/siteX/storage/Nice](http://forum.nginx.org/siteX/storage/Nice) Images/test-image.jpg”  
> That’s not a valid URI. I just wanted to give an example that shows the  
> type of white space that is in my URI.

Could you show proxy\_pass directive ?

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/15 "2010-09-28T19:38:39Z")

</div>

Sorry to be a pain, but I cannot claim success yet.

The good news - the image now appears.

The bad news - the /us/ is now case sensitive and the www.xxxx.xxx/us/  
site never returns. It’s in some kind of loop. A page further down  
like www.xxxx.xxx/us/some-directory/some-page will work.

location ^~ /us {  
proxy\_pass [http://backendcms/](http://backendcms/);  
proxy\_set\_header Host xxx.xxx.xxx;  
subs\_filter …

Posted at Nginx Forum:

> **[Re: spaces in URI](https://forum.nginx.org/read.php?2,97031,135046#msg-135046)**
>
> On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote: \> Hi Folks, \> \> We are running into a small problem with bad HTTP clients. \> \> We have sold a bunch of hardware with embedded HTTP clients which don't \> URL-encode the...

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/16 "2010-09-28T20:00:54Z")

</div>

On Tue, Sep 28, 2010 at 01:37:01PM -0400, cgarver wrote:

> ```
> proxy_set_header Host xxx.xxx.xxx;
> subs_filter ....
> 
> ```

You have to create 4 locations:

location ^~ /us {  
location ^~ /US {  
location ^~ /Us {  
location ^~ /uS {

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 7:41pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/17 "2010-09-28T19:41:35Z")

</div>

The problem with the looping was that I needed to update my subs\_filter  
to eliminate the leading slash in pattern matches. So, it is now  
working very well except it is case sensitive which is bound to cause  
some issues. Is there a rewrite I can do to force the request to  
lowercase without killing server performance?

Posted at Nginx Forum:

> **[Re: spaces in URI](https://forum.nginx.org/read.php?2,97031,135050#msg-135050)**
>
> On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote: \> Hi Folks, \> \> We are running into a small problem with bad HTTP clients. \> \> We have sold a bunch of hardware with embedded HTTP clients which don't \> URL-encode the...

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/18 "2010-09-28T20:18:27Z")

</div>

On Tue, Sep 28, 2010 at 02:14:09PM -0400, cgarver wrote:

> will be any pitfalls later…  
> Then

rewrite (?i)^/us(._)$ /us$1 last;  
rewrite (?i)^/ca(._)$ /ca$1 last;

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/19 "2010-09-28T20:15:32Z")

</div>

As I have other countries like Canada to deal with, it can get rather  
unwieldy doing every permutation of case. What about something like  
this:

rewrite (?i)^/us(.\*)$ /us$1;  
location ^~ /us {  
proxy\_pass [http://backendcms/](http://backendcms/);  
proxy\_set\_header Host xxx.xxx.xxx;

It seems to be allowing www.xxxx.xxx/uS/ to work. Not sure if there  
will be any pitfalls later…

Posted at Nginx Forum:

> **[Re: spaces in URI](https://forum.nginx.org/read.php?2,97031,135058#msg-135058)**
>
> On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote: \> Hi Folks, \> \> We are running into a small problem with bad HTTP clients. \> \> We have sold a bunch of hardware with embedded HTTP clients which don't \> URL-encode the...

---

<div class="post-metadata">

**Author:** ![imixaly4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3ab097/32.png) [@imixaly4](https://www.ruby-forum.com/u/imixaly4)\
**Post date:** [September 28, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/spaces-in-uri/190007/20 "2010-09-28T20:43:03Z")

</div>

Works like a champ! Thanks for your help.

Posted at Nginx Forum:

> **[Re: spaces in URI](https://forum.nginx.org/read.php?2,97031,135070#msg-135070)**
>
> On Fri, Jun 11, 2010 at 02:54:35AM -0400, Peter Portante wrote: \> Hi Folks, \> \> We are running into a small problem with bad HTTP clients. \> \> We have sold a bunch of hardware with embedded HTTP clients which don't \> URL-encode the...
