首页 > 解决方案 > 502 错误导致我在 django 中的多个主机

问题描述

我试图允许从数据库中提取多个主机。它在本地工作正常,但在生产中我收到 502 错误。

这是我的生产设置文件

from .settings import *

DEBUG = False


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'projectname_settings',
        'USER': '******',
        'PASSWORD': '******',
        'HOST': 'localhost',
        'PORT': '',
    }
}

ALLOWED_HOSTS = [
    "mydomain.com",
] + get_allowed_hosts(DATABASES['default'])

Allowed_hosts.py

def get_allowed_hosts(db_params):
    connection = None

    if db_params['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
        import psycopg2
        connection = psycopg2.connect(user=db_params['USER'],
                                      password=db_params['PASSWORD'],
                                      host=db_params['HOST'],
                                      port=db_params['PORT'],
                                      database=db_params['NAME'])
    elif db_params['ENGINE'] == 'django.db.backends.sqlite3':
        import sqlite3
        connection = sqlite3.connect(db_params['NAME'])

    if connection is not None:
         cursor = connection.cursor()
         sites_query = cursor.execute("SELECT domain FROM django_site")
         sites_result = cursor.fetchall()
         cursor.close()
         connection.close()

我在想这种设置是否需要特殊的 nginx 设置

我的 nginx 配置:

server {
    listen 80;
    server_name *****.com;

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

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

结果cat /var/log/nginx/error.log

2020/02/16 07:16:06 [错误] 32356#32356: *110 connect() to unix:/run/gunicorn.sock 在连接到上游时失败(111:连接被拒绝),客户端:. . . ,服务器:****.com,请求:“GET / HTTP/1.1”,上游:“ http://unix:/run/gunicorn.sock:/ ”,主机:“ ... 80 ”

标签: djangonginxdeploymentgunicorn

解决方案


推荐阅读