django - 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 ”
解决方案
推荐阅读
- xampp - 在 localhost 上安装新的 TYPO3:后端不起作用
- r - 如何在snakemake中调用列表样式参数
- python-3.x - 如何修复烧瓶应用程序中的“AttributeError: 'bool' object has no attribute 'quantity'”错误
- java - 为什么无限对象创建不抛出 OutOfMemoryError?
- powershell - PowerShell 邮箱过滤错误?
- python - numpy数组矩阵中的布尔掩码
- xml - 如何制作应该只验证mandaotry字段的通用xsd
- python - 条形图在数组中绘制负 x 值,尽管数组中没有负值
- jquery - 如何将ajax响应值传递到输入字段以进行自动完成
- spring - 有没有办法在所有存储库文件中一次使用 @Autowired ?