Nginx – ‘Invalid input syntax for type inet’ db error in Django app with postgres when Gunicorn+Nginx(reverse proxy) installed

djangonginxpostgresqlpythonreverse-proxy

I have a Django app with postgres back-end where I'm configuring gunicorn to work behind nginx as reverse proxy. My machine runs Ubuntu 14.04.

Everything seems to have worked, except I've run into a nasty error that's thrown the moment I try to log into my app:

DatabaseError at /login/

invalid input syntax for type inet: "" LINE 1: …00101 Firefox/41.0',
'2015-12-12 09:39:55.590036+00:00', '')

Exception Location:
/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py
in execute, line 54

Note that this error never comes if I use solely Gunicorn! Can you help me fix this issue? I have a few inklings:

1) The inet data type accepts IPv4 and IPv6 hosts and networks (but not domain names). Maybe I ought to change the domain name I've used in my /etc/nginx/sites-available/myproject? But I tried it; my website fails to load at all (whereby hitherto it only failed after I tried to log in). The code in /etc/nginx/sites-available/myproject is pasted below.

2) A different theory goes something like this:

When I try to log in, my code tries to add a row to some logging table with empty
remote IP
. It's possible that when I use reverse proxy, the code
doesn't know the remote IP, as it's shadowed by the proxy's IP.

Since it's empty, it's possible the code tries to ignore proxy's IP,
but it does not find any better. Hence, it should use X-Forwarded-For header.

And if there's no reasonable IP to log, the program should simply log
"NULL" as the IP.

To this end, I included proxy_params in /etc/nginx/sites-available/myproject. After that, I ran sudo service nginx restart, and reloaded my website. The error still remains.

I need help from an expert who can assist me in solving it.


/etc/nginx/sites-enabled/myproject:

server {
    listen 80;
    server_name example.cloudapp.net;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {

        root /home/mhb11/folder/myproject;
    }

    location / {

        include proxy_params;
        proxy_pass http://unix:/home/mhb11/folder/myproject/myproject.sock;

    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/mhb11/folder/myproject/templates/;
   }
}

/etc/nginx/proxy_params:

proxy_set_header Host $http_host;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

/etc/nginx/nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

env ON_AZURE=1;
env awsaccesskeyid=something;
env awssecretkey=something;

events {
        worker_connections 1024;
        multi_accept on;
        use epoll;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##
        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascrip$

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

Note: feel free to ask for more information in case you need it.

Best Answer

Since this is an postgresql error you should probably start by enabling postgresql query log and see what exactly is the erroneous sql query, what is supplied to postgres as 'inet', should be ip/mask(type inet).

Once you've got hold of the broken sql query you can get better understanding which part of the code is issuing it and continue down the line.

You can also check this one which seems to be a common Django/nginx issue

https://stackoverflow.com/questions/1627901/remote-addr-not-getting-sent-to-django-using-nginx-tornado.

Obviously the proxy is not normally passing some of the http headers, this should fix it.