Ningx/Django Bad Request Error

Hello Nginx Group.

I am new to Ngnix and installing my first Nginx/Django server. I’ve
been struggling with a Bad Request 400 Error for two days now and I have
nowhere else to go. Here is the problem:

I have Nginx configured with django. My default config file is below.
I am have a basic django-piston REST service setup that works correctly
with django’s runserver. The following two commands work perfectly with
django’s runserver http server:

url = ‘http://localhost/receipts

GET receipts data

req = urllib2.Request(url)
f = urllib2.urlopen(req)

POST Receipts data with json data

data =
req = urllib2.Request(url, data=data, headers={‘Content-Type’:
‘application/json’})
f = urllib2.urlopen(req)

When I run the same requests against my nginx installation the GET
function works just fine. However, the POST request returns a HTTP
Error 400: BAD REQUEST error.

It is exactly the same code and I know this is working with the django
runserver. It must be some configuration in Nginx that is regecting the
POST request. I get that error for any Content-Type.

I have looked everywhere and I cannot find anything about allowing this
post request. I hope someone here can help nudge me in the right
direction. My Nginx config file settings is below. I have modified
many variables like buffer sizes and body sizes and it is not that. The
data I’m posting is less than 8k.

Any ideas?


server {
include mime.types;
default_type application/json;

    client_body_buffer_size 1200K;
    client_header_buffer_size 1200K;
    client_max_body_size 2m;
    large_client_header_buffers 2 1200K;

    listen   80; ## listen for ipv4; this line is default and 

implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6

    # Make site accessible from http://localhost/
    server_name localhost;

    # Log information
    access_log /home/henry/logs/access.log;
    error_log /home/henry/logs/error.log;

    root /home/henry/webservice/public;

    location /site_media
    {
            root /path/to/yoursite.com/public;
    }

    location / {
                    # host and port to fastcgi server
                    fastcgi_pass 127.0.0.1:8081;
                    fastcgi_param PATH_INFO $fastcgi_script_name;
                    fastcgi_param REQUEST_METHOD $request_method;
                    fastcgi_param QUERY_STRING $query_string;
                    fastcgi_param SERVER_NAME $server_name;
                    fastcgi_param SERVER_PORT $server_port;
                    fastcgi_param SERVER_PROTOCOL $server_protocol;
                    fastcgi_param CONTENT_TYPE $content_type;
                    fastcgi_param CONTENT_LENGTH $content_length;
                    fastcgi_pass_header Authorization;
                    fastcgi_intercept_errors off;
    }

    location /doc {
            root /usr/share;
            autoindex on;
            allow 127.0.0.1;
            deny all;
    }

    location /images {
            root /usr/share;
            autoindex off;
    }

}

Hello,

Le 18 janv. 2012 01:35, Henry Ward a crit :

Any ideas?

With this much information no.

Imho running django using fcgi is something you don’t want to do.
I would recommend using http://gunicorn.org as a wsgi server (or uwgi
module)

Take a look here too:
http://stackoverflow.com/questions/5706230/nginx-gunicorn-post-request-error

In the case you still want to use the old fcgi way, you should provide
use with debug log on both site (nginx and application server).

Regrds,

xav

On Wednesday 18 January 2012 04:35:40 Henry Ward wrote:
[…]

I have looked everywhere and I cannot find anything about allowing this
post request. I hope someone here can help nudge me in the right
direction. My Nginx config file settings is below. I have modified many
variables like buffer sizes and body sizes and it is not that. The data
I’m posting is less than 8k.

Any ideas?

Could you provide debug log with your POST request?
http://nginx.org/en/docs/debugging_log.html


server {
include mime.types;
default_type application/json;

    client_body_buffer_size 1200K;
    client_header_buffer_size 1200K;

It’s a lot!

wbr, Valentin V. Bartenev

Thanks Xav and Valentin for the quick response. Sorry - I have to copy
your responses because I was subscribed on digest rather than real-time
email. But I am now on the normal mailing list.

Error Log
The error log shows nothing. Literally. The access_log shows the
access url and the 400 error. But the error log does not. I know that
is hard to believe its similar to what is said here:

http://www.ruby-forum.com/topic/2573586

In that the also had the same problem that Bad Request nothing shows in
the error log. I thought it might be a similar issue but I don’t have
any spaces in python urllib2 header so I don’t think that is the issue.

I have also created application logging in Django and have “GOT HERE”
log statement in the first line of the view that handles the POST
request… That line never gets called so it appears Nginx is kicking
out the request before it ever passes to the django app.

This is what makes so irritatingly difficult to debug. It is also I
feel it some type of permission error or setting that does not allow
POST requests. I’ve also read here (https://calomel.org/nginx.html)
that Nginx by default only allows GET requests but I couldn’t find
anywhere where the is verified nor how to enable that.

I’m stumped.

WSGI
Xav - I am happy try gunicorn.org as a wsgi server instead of fcgi.
Particularly if there isn’t a reasonable solution to this problem.
Again - I’m now to this. Can you give a quick background on why you
don’t recommend fcgi and do recommend gunicorn? (current. problem
excluded :wink:

Henry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs