当前位置: 首页 > 面试题库 >

使用Postgres和Gunicorn + Nginx作为反向代理的Django应用程序中'类型inet的输入语法无效'db错误

喻昀
2023-03-14
问题内容

您能帮我破译这个比较深奥的错误吗?启动应用程序时一切正常,但是尝试启动时崩溃login

在/ login /的DatabaseError

类型inet的输入语法无效:“” LINE 1:… 00101 Firefox / 41.0’,‘2015-12-12
09:39:55.590036 + 00:00’,’‘)

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

我知道一个事实,Inet数据类型接受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', '')

urls.py中的 / login / 网址是url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name="login"),

环境是:

*具有 *postgresql后端的 Django 1.5.1 ,位于Ubuntu 14.04计算机上。特别要注意的是,在我将
nginx 设置 为gunicorn背后的反向代理之后 (使用端口80进行设置),此错误开始出现。事情的来龙去脉 完美 ,当我用 纯粹
gunicorn(和女服务员,另一纯Python Web服务器)上的8080端口。

/ 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。这是我在nginx配置中设置的一组转发标头:

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/zh/docs/http/ngx_http_proxy_module.html#proxy_set_header

然后在Django中,您可以执行以下操作:

user_ip = request.META['HTTP_X_REAL_IP`] or request.META['REMOTE_ADDR']

请注意,X-Forwarded-Proto在将Django与SSL结合使用时,这是必要的,在这种情况下,您还需要对Django进行一些配置:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

Django文档中的更多内容-
https://docs.djangoproject.com/zh/1.9/ref/settings/#std:setting-
SECURE_PROXY_SSL_HEADER



 类似资料:
  • 我将Nginx配置为简单反向代理。 我只是使用基本设置 问题是,经过一段时间(几天)后,nginx背后的站点变得不可访问。Indead nginx尝试调用一个坏的ip(nginx后面的站点在我家盒子后面,我正在使用dyn dns,因为我的ip不固定)。这个dyn dns总是有效的(我可以直接调用我的站点),但由于不清楚的原因,Nginx被卡住了。。 如前所述,nginx只需在一段时间后给我504网

  • 我有一个部署在野蝇10.1应用程序服务器上的webapp。此webapp被监听:http://localhost:8080/app-profile-jsp/(1) 我成功安装了nginx。服务器已成功安装,我测试提供静态网页和图像。 现在我想配置nginx从“http://www . fri zio . local”(2)URL访问web应用程序。我在/etc/hosts中配置了这个地址。 我在n

  • 我已经被这个问题困扰了3天。 我的服务器是Centos,在Httpd服务中使用wordpress(WP)。 它的IP是'103.232.120.178' 我想使用nginx作为WP的反向代理。 Httpd位于端口2101 Nginx位于端口80 WP位于子目录'bongda69'(URL:'103.232.120.178:2101/bongda69') 我想如果访问我的网站,它重定向到WordPr

  • 问题内容: 我有一个在Docker外部在端口5000上运行的应用程序。我试图通过Dockercompose在nginx中运行反向代理,但无法与主机的端口5000通信。在我的docker-compose.yml文件中,我具有: 当我尝试运行此命令时,我得到: 如果我注释掉,我得到: 如何从Docker Nginx容器连接到主机中已运行的应用程序? 编辑: 我的nginx.conf文件 当我尝试卷曲l

  • 使用nginx作为前端的反向代理将请求传递给应用是可行的。然而,只有nginx 1.4版本以上才支持WebSocket协议。下面是nginx代理HTTP和WebSocket请求的一个最基本的配置: server { listen 80; server_name _; location / { include proxy_params; pro

  • Nginx的配置文件如下: server { listen 80; #此处应该配置你的域名: server_name doc.iminho.me; charset utf-8; #此处配置你的访问日志,请手动创建该目录: access_log /var/log/nginx/webhook.iminho.me/access.log