'Invalid input syntax for type inet' 使用 postgres 和 Gunicorn+Nginx 作为反向代理的 Django 应用程序中的数据库错误
'Invalid input syntax for type inet' db error in Django app with postgres and Gunicorn+Nginx as reverse proxy
你能帮我破译这个相当深奥的错误吗?当我启动应用程序时一切正常,但当我尝试 login
.
时崩溃了
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
我知道 inet data type 接受 IPv4 和 IPv6 主机和网络(但不接受域名)。
回溯如下:
Traceback:
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
187. response = middleware_method(request, response)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/user_sessions/middleware.py" in process_response
46. request.session.save()
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/user_sessions/backends/db.py" in save
86. obj.save(force_insert=must_create, using=using)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/base.py" in save
546. force_update=force_update, update_fields=update_fields)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
650. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
215. return insert_query(self.model, objs, fields, **kwargs)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
1661. return query.get_compiler(using=using).execute_sql(return_id)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
937. cursor.execute(sql, params)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
41. return self.cursor.execute(sql, params)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py" in execute
58. six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py" in execute
54. return self.cursor.execute(query, args)
Exception Type: DatabaseError at /login/
Exception Value: invalid input syntax for type inet: ""
LINE 1: ...00101 Firefox/41.0', '2015-12-12 09:39:55.590036+00:00', '')
/login/url在urls.py是url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name="login"),
环境是:
Django 1.5.1 带有 postgresql 后端 并驻留在 Ubuntu 14.04 机器上。特别要注意的是,在我将 nginx 设置为 gunicorn 后面的反向代理(此设置使用端口 80)之后,此错误开始出现。当我在端口 8080 上 单独 gunicorn(和女服务员,另一个纯 python 网络服务器)时,事情 完美
/etc/nginx/sites-available/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;
# }
#}
怎么回事?
注意:如果您需要,请向我询问更多信息,例如我的 gunicorn.conf 和 nginx.conf 看起来如何,安装的软件包或者您是否需要与数据库相关的东西。问吧!
您的程序正在尝试向某些具有空远程 IP 的日志记录 table 添加一行。我想当你使用反向代理时,程序不知道远程 IP,因为它被代理的 IP 隐藏了。
因为它是空的,所以我想程序正试图忽略代理的 IP,但它没有找到更好的方法。它应该使用 X-Forwarded-For
header.
如果没有合理的 IP 记录,程序应该简单地记录 "NULL" 作为 IP。
您没有转发代理IP。这是我在 nginx 配置中设置的转发 headers 集:
location / {
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;
proxy_pass ......;
}
nginx 文档中的更多选项 - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
然后在 Django 中你可以这样做:
user_ip = request.META['HTTP_X_REAL_IP`] or request.META['REMOTE_ADDR']
请注意,在将 Django 与 SSL 结合使用时,X-Forwarded-Proto
是必需的,在这种情况下,您还需要对 Django 进行一些配置:
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
Django 文档中的更多内容 - https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-SECURE_PROXY_SSL_HEADER
添加自定义中间件解决了问题:
class XForwardedForMiddleware():
def process_request(self, request):
if request.META.has_key("HTTP_X_FORWARDED_FOR"):
request.META["HTTP_X_PROXY_REMOTE_ADDR"] = request.META["REMOTE_ADDR"]
parts = request.META["HTTP_X_FORWARDED_FOR"].split(",", 1)
request.META["REMOTE_ADDR"] = parts[0]
你能帮我破译这个相当深奥的错误吗?当我启动应用程序时一切正常,但当我尝试 login
.
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
我知道 inet data type 接受 IPv4 和 IPv6 主机和网络(但不接受域名)。
回溯如下:
Traceback:
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
187. response = middleware_method(request, response)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/user_sessions/middleware.py" in process_response
46. request.session.save()
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/user_sessions/backends/db.py" in save
86. obj.save(force_insert=must_create, using=using)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/base.py" in save
546. force_update=force_update, update_fields=update_fields)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
650. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
215. return insert_query(self.model, objs, fields, **kwargs)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
1661. return query.get_compiler(using=using).execute_sql(return_id)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
937. cursor.execute(sql, params)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
41. return self.cursor.execute(sql, params)
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py" in execute
58. six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
File "/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py" in execute
54. return self.cursor.execute(query, args)
Exception Type: DatabaseError at /login/
Exception Value: invalid input syntax for type inet: ""
LINE 1: ...00101 Firefox/41.0', '2015-12-12 09:39:55.590036+00:00', '')
/login/url在urls.py是url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name="login"),
环境是:
Django 1.5.1 带有 postgresql 后端 并驻留在 Ubuntu 14.04 机器上。特别要注意的是,在我将 nginx 设置为 gunicorn 后面的反向代理(此设置使用端口 80)之后,此错误开始出现。当我在端口 8080 上 单独 gunicorn(和女服务员,另一个纯 python 网络服务器)时,事情 完美
/etc/nginx/sites-available/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;
# }
#}
怎么回事?
注意:如果您需要,请向我询问更多信息,例如我的 gunicorn.conf 和 nginx.conf 看起来如何,安装的软件包或者您是否需要与数据库相关的东西。问吧!
您的程序正在尝试向某些具有空远程 IP 的日志记录 table 添加一行。我想当你使用反向代理时,程序不知道远程 IP,因为它被代理的 IP 隐藏了。
因为它是空的,所以我想程序正试图忽略代理的 IP,但它没有找到更好的方法。它应该使用 X-Forwarded-For
header.
如果没有合理的 IP 记录,程序应该简单地记录 "NULL" 作为 IP。
您没有转发代理IP。这是我在 nginx 配置中设置的转发 headers 集:
location / {
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;
proxy_pass ......;
}
nginx 文档中的更多选项 - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
然后在 Django 中你可以这样做:
user_ip = request.META['HTTP_X_REAL_IP`] or request.META['REMOTE_ADDR']
请注意,在将 Django 与 SSL 结合使用时,X-Forwarded-Proto
是必需的,在这种情况下,您还需要对 Django 进行一些配置:
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
Django 文档中的更多内容 - https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-SECURE_PROXY_SSL_HEADER
添加自定义中间件解决了问题:
class XForwardedForMiddleware():
def process_request(self, request):
if request.META.has_key("HTTP_X_FORWARDED_FOR"):
request.META["HTTP_X_PROXY_REMOTE_ADDR"] = request.META["REMOTE_ADDR"]
parts = request.META["HTTP_X_FORWARDED_FOR"].split(",", 1)
request.META["REMOTE_ADDR"] = parts[0]