Gunicorn + Nginx(反向代理)安装时,使用postgres在Django应用程序中inputinettypes的dbinput语法无效

我有一个Django应用程序与postgres后端,我configurationgunicorn后面的nginx作为反向代理工作。 我的机器运行Ubuntu 14.04。

一切似乎都奏效了,除非我尝试login到我的应用程序时遇到了一个令人讨厌的错误:

DatabaseError在/ login /

inputinettypes的input语法无效:“”LINE 1:… 00101 Firefox / 41.0','2015-12-12 09:39:55.590036 + 00:00','')

exception位置:/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py执行中,第54行

请注意,如果我单独使用Gunicorn,这个错误永远不会出现! 你能帮我解决这个问题吗? 我有几个念头:

1) inet数据types接受IPv4和IPv6主机和networking(但不包括域名)。 也许我应该改变我在/ etc / nginx / sites-available / myproject中使用的域名? 但我试过了; 我的网站根本无法加载(迄今为止它只我尝试login后才失败)。 / etc / nginx / sites-available / myproject中的代码粘贴在下面。

2)不同的理论是这样的:

当我尝试login时,我的代码尝试添加一行到空的远程IP的日志表。 当我使用反向代理时,代码可能不知道远程IP,因为它被代理的IP所遮蔽

由于它是空的,有可能代码试图忽略代理的IP,但没有find更好的。 因此,它应该使用X-Forwarded-For头

如果没有合理的IPlogging,程序应该简单地logging“NULL”作为IP。

为此,我在/ etc / nginx / sites-available / myproject中join了proxy_params 。 之后,我跑了sudo service nginx restart ,并重新加载我的网站。 错误仍然存在

我需要一位能帮助我解决问题的专家的帮助。


在/ etc / nginx的/网站启用/ 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; # } #} 

注意:如果您需要,请随时索取更多信息。

由于这是一个postgresql错误,你应该首先启用postgresql查询日志,看看究竟是错误的sql查询,提供给postgres作为'inet',应该是ip / mask(type inet)。

一旦你掌握了这个破碎的SQL查询,你可以更好地理解代码的哪一部分正在发布,并继续下去。

你也可以检查这个似乎是常见的Django / nginx问题

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

很明显,代理通常不会传递一些http头文件,这应该可以解决这个问题。