# A bit of ajax works fine locally but breaks on nginx

**URL:** <https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018>\
**Category:** Deployment\
**Created:** [July 24, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018 "2008-07-24T18:01:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 24, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/1 "2008-07-24T18:01:53Z")

</div>

I have an ajax call that works fine locally:

#in my view  
\<%= link\_to\_remote “Add a New Property”, { :url =\>  
admin\_properties\_path, :method =\> ‘POST’ } %\>

This calls the create action in my admin/properties controller. But, on  
our server, which uses nginx and a mongrel cluster, i get this error  
reported in firebug when the post request is sent:

411 Length Required
# 411 Length Required

* * *
nginx/0.6.29

Can anyone explain what’s going on here, and how to fix it? My  
knowledge of server mechanics is shaky at best and i’m not sure what the  
‘length’ thing is in reference to.

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 24, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/2 "2008-07-24T18:03:34Z")

</div>

Oh, and this is the generated html from the link\_to\_remote:

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 24, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/3 "2008-07-24T18:56:35Z")

</div>

Max W. wrote:

> Oh, and this is the generated html from the link\_to\_remote:

I actually fixed this, on my second attempt, i’d still be very  
interested in finding out why it broke though.

Attempt 1 - (FAIL)  
Read that a remote post request with no parameters sometimes breaks over  
length, so i added a dummy parameter in there.

Attempt 2 - (WIN)  
Changed it to a remote get request instead.

It’s not really a true win though as i’m still none the wiser why the  
post request worked locally but broke on our server.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 25, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/4 "2008-07-25T16:47:08Z")

</div>

Whould you please post you Nginx config?

–  
Aníbal Rojas

> **[Home - Has Many Developers](https://hasmanydevelopers.com/)**
>
> We are the best choice for your building design and development

> **[RUBYCORNER.COM](https://www.rubycorner.com/)**
>
> RUBYCORNER.COM - Contact us for any business inquiries

[http://anibal.rojas.com.ve](http://anibal.rojas.com.ve)

On Jul 25, 11:56 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 25, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/5 "2008-07-25T17:24:33Z")

</div>

I assume you mean this file (i didn’t write this) - here you go: thanks!

#/config/nginx/rails\_nginx\_vhost.conf

upstream fake\_application {  
server 127.0.0.1:8000;  
server 127.0.0.1:8001;  
}

server {  
listen 80;  
server\_name [dev.fakedomain.com](http://dev.fakedomain.com)[assets0.fakedomain.com](http://assets0.fakedomain.com)  
[assets1.fakedomain.com](http://assets1.fakedomain.com) [assets2.fakedomain.com](http://assets2.fakedomain.com) [assets3.fakedomain.com](http://assets3.fakedomain.com);  
root /var/www/apps/fake\_application/current/public;  
access\_log  
/var/www/apps/fake\_application/shared/log/dev.fakedomain.com-access.log;  
error\_log  
/var/www/apps/fake\_application/shared/log/dev.fakedomain.com-error.log;  
client\_max\_body\_size 50M;

```
    if (-f $document_root/system/maintenance.html){
            rewrite ^(.*)$ /system/maintenance.html last;
            break;
    }
    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host $http\_host;  
proxy\_redirect false;

```
            if (-f $request_filename/index.html) {
                    rewrite (.*) $1/index.html break;
            }
            if (-f $request_filename.html) {
                    rewrite (.*) $1.html break;
            }
            if (!-f $request_filename) {
                    proxy_pass http://fake_application;
                    break;
            }
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root html;
    }

```

}

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 27, 2008, 2:11am UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/6 "2008-07-27T02:11:14Z")

</div>

Thanks for looking at this anibal.

Like i said in my follow up post, above, my first attempt at fixing the  
problem was to add a dummy parameter to the post request, and it still  
broke. I did fix it by doing a get instead though.

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 26, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/7 "2008-07-26T15:32:02Z")

</div>

The proxy configuration looks good, I first tough it could be a  
missing header.

Googled a little, and it looks like Nginx chokes on a empty POST  
request, currently your alternatives looks like switching to GET or  
adding a dumb variable to the POST.

This is interesting, will play a little with it maybe post it to Nginx  
list.

Best regards,

–  
Aníbal

On Jul 26, 10:24 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 16, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/8 "2008-11-16T19:28:17Z")

</div>

I’ve run into the same thing. Nginx chokes with a 411 error ("Length  
required), when the jQuery Ajax call leaves the data parameter blank,  
e.g. as in:

“jQuery.post(’/your\_ajax\_method’);”

The request never gets passed through to the app in this case.

After some Googling, it turned out the simple fix is to provide an empty  
data set:

“jQuery.post(’/your\_ajax\_method’, {});”

This is probably a bug on the jQuery side.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 11, 2008, 5:29am UTC](https://www.ruby-forum.com/t/a-bit-of-ajax-works-fine-locally-but-breaks-on-nginx/144018/9 "2008-08-11T05:29:49Z")

</div>

Hi all,

I’m also having the same issue. What’s weird is that the Content-Length  
is clearly 114 here. It’s sending a PUT request instead of POST. Can  
anyone help? Thanks in advance!

Host www._.com  
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US;  
rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1  
Accept text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8  
Accept-Language en-us,en;q=0.5  
Accept-Encoding gzip,deflate  
Accept-Charset ISO-8859-1,utf-8;q=0.7,_;q=0.7  
Keep-Alive 300  
Connection keep-alive  
X-Requested-With XMLHttpRequest  
Content-Type application/x-www-form-urlencoded; charset=UTF-8  
Referer [http://www](http://www)._ **.com/** _/1/\*\*\*  
Content-Length 114  
Cookie  
\_\*_**\_session=BAh7CjoPbGFzdF9sb2dpbnU6CVRpbWUNYR0bgAAAYL86EHJlcXVlc3RfdXJp%250AMDoMY3NyZl9pZCIlMDg2ZDNhMDc5MTVlMjU5OTc5YzNiYWU5ODMzMTJhOGUi%250ACmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7%250ABzoLbm90aWNlMDoMd2FybmluZzAGOgpAdXNlZHsHOwpUOwtUOgl1c2Vybzo1%250AQWN0aXZlUmVjb3JkOjpBc3NvY2lhdGlvbnM6OkJlbG9uZ3NUb0Fzc29jaWF0%250AaW9uCToLQG93bmVybzoLRGV0YWlsCDoMQHBlcnNvbkALOhBAYXR0cmlidXRl%250Ac3sLIhV3b3JsZF92aXNpYmlsaXR5Ig9ldmVyeXRoaW5nIhVpbmZvcm1hdGlv%250Abl90eXBlIhFFbWFpbEFkZHJlc3MiD3VwZGF0ZWRfb24iGDIwMDgtMDgtMDQg%250AMDU6NDk6MTUiB2lkIgYyIhNpbmZvcm1hdGlvbl9pZCIGMSIOcGVyc29uX2lk%250AIgYxOhZAYXR0cmlidXRlc19jYWNoZXsAOgxAbG9hZGVkVDoMQHRhcmdldG86%250ACVVzZXIXOiVAZGV0YWlsX21lbWJlcnNoaXBfcmVsYXRpb25zaGlwczA6KUB1%250Ac2VyX3Byb2ZpbGVfZW1haWxfYWRkcmVzc19yZXF1ZXN0czA6E0BoaXN0b3J5%250AX2l0ZW1zMDsSewoiD2FjY291bnRfaWQiBjEiFW9wdGlvbmFsX2RhdGVfaWQw%250AIgl0eXBlIglVc2VyIgdpZCIGMSIMbmFtZV9pZCIGMToNQGFjY291bnQwOihA%250AdXNlcl9wcm9maWxlX2VtYWlsX2FkZHJlc3NfcmVxdWVzdDA6DUBkZXRhaWxz%250AMDojQGRldGFpbF9lbWFpbF9hZGRyZXNzX3JlcXVlc3RzMDoQQGJpcnRoX2Rh%250AdGUwOhRAZ3JvdXBzX21hbmFnZWQwOhxAZW1haWxfYWRkcmVzc19yZXF1ZXN0%250AczA6HUBtYW5hZ2VyX3N0YXR1c19yZXF1ZXN0czA7E3sAOgxAZ3JvdXBzMDor%250AQGRlbGV0ZV9wcmltYXJ5X2VtYWlsX2FkZHJlc3NfcmVxdWVzdHMwOhFAbWVt%250AYmVyc2hpcHMwOiFAZ3JvdXBfbWFuYWdlcl9yZWxhdGlvbnNoaXBzMDoKQG5h%250AbWUwOhBAcmVmbGVjdGlvbm86NEFjdGl2ZVJlY29yZDo6UmVmbGVjdGlvbjo6%250AQXNzb2NpYXRpb25SZWZsZWN0aW9uDToYQHRocm91Z2hfcmVmbGVjdGlvbkY6%250AE0BhY3RpdmVfcmVjb3JkYwtEZXRhaWw6EEBjbGFzc19uYW1lIgtQZXJzb246%250AC0BrbGFzc2MLUGVyc29uOgtAbWFjcm86D2JlbG9uZ3NfdG86DUBvcHRpb25z%250AewA7JjoLcGVyc29uOhZAcHJpbWFyeV9rZXlfbmFtZSIOcGVyc29uX2lk–020f3f75cdb3d084d5b4c2dccef5c941eff0b6c0;  
email\_address=**_%[40hotmail.com](http://40hotmail.com);  
connection=4e210ec5836a6b4fbb0afe35c18eacb9

Max W. wrote:

> Thanks for looking at this anibal.
> 
> Like i said in my follow up post, above, my first attempt at fixing the  
> problem was to add a dummy parameter to the post request, and it still  
> broke. I did fix it by doing a get instead though.
> 
> thanks  
> max
