Forum: Rails deployment A bit of ajax works fine locally but breaks on nginx

Posted by Max Williams (max-williams)
on 2008-07-24 18:01
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:

<html>
<head><title>411 Length Required</title></head>
<body bgcolor="white">
<center><h1>411 Length Required</h1></center>
<hr><center>nginx/0.6.29</center>
</body>
</html>

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
Posted by Max Williams (max-williams)
on 2008-07-24 18:03
Oh, and this is the generated html from the link_to_remote:

<a href="#" onclick="jQuery.ajax({async:true, dataType:'script', 
type:'POST', url:'/admin/properties'}); return false;">
Posted by Max Williams (max-williams)
on 2008-07-24 18:56
Max Williams wrote:
> Oh, and this is the generated html from the link_to_remote:
> 
> <a href="#" onclick="jQuery.ajax({async:true, dataType:'script', 
> type:'POST', url:'/admin/properties'}); return false;">

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.
Posted by Aníbal Rojas (Guest)
on 2008-07-25 16:47
(Received via mailing list)
Whould you please post you Nginx config?

--
Aníbal Rojas
http://hasmanydevelopers.com
http://rubycorner.com
http://anibal.rojas.com.ve

On Jul 25, 11:56 am, Max Williams <ruby-forum-incom...@andreas-s.net>
Posted by Max Williams (max-williams)
on 2008-07-25 17:24
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  assets0.fakedomain.com 
assets1.fakedomain.com assets2.fakedomain.com 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;
        }
}
Posted by Aníbal Rojas (Guest)
on 2008-07-26 15:32
(Received via mailing list)
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 Williams <ruby-forum-incom...@andreas-s.net>
Posted by Max Williams (max-williams)
on 2008-07-27 02:11
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
Posted by Caesar Indra (suntzu)
on 2008-08-11 05:29
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.***.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; 
connection=4e210ec5836a6b4fbb0afe35c18eacb9

Max Williams 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
Posted by Wolfram Arnold (wolframarnold)
on 2008-11-16 19:28
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.