您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

您没有转发代理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

Postgres 2022/1/1 18:28:02 有327人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶