django - 502 Bad Gateway 显示错误,发生超时
问题描述
我目前的 nginx 和 gunicorn 设置存在一些问题。一旦我启用了它们并转到由 digitalocean 建立的 IP 地址,我需要 5 分钟才能显示 502 Bad Gateway Nginx。之后我收到一个哨兵错误,内容如下:
“OperationalError 无法连接到服务器:连接超时服务器是否在主机“myproject-postgres-staging-db-do-user-6482921-0.db.ondigitalocean.com”(64.225.42.160)上运行并接受 TCP/IP端口 25060 上的连接?”
我一直在用我的 nginx 和 gunicorn 配置以及 settings.py 文件尝试各种不同的东西,但无济于事。我不确定我是否在这些方法方面做错了什么,因此为什么我没有得到任何结果,但我没有想法。这是我的代码供参考。
Nginx 配置:
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name myproject.app www.myproject.app IP ADDRESS;
error_log /home/myuser/nginxError.log warn;
access_log /home/myuser/nginx.log ;
server_tokens off;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
add_header Referrer-Policy: strict-origin-when-cross-origin;
add_header Feature-Policy: *;
add_header Content-Security-Policy: default-src ;
location /static {
alias /My-Project/backend/staticfiles;
}
location / {
include proxy_params;
proxy_pass http://unix:/My-Project/backend.sock;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;
}
# listen 443 ssl; # managed by Certbot
# ssl_certificate /etc/letsencrypt/live/myproject.app/fullchain.pem; # managed by Certbot
# ssl_certificate_key /etc/letsencrypt/live/myproject.app/privkey.pem; # managed by Certbot
# include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
# ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.myproject.app) {
return 301 http://$host$request_uri;
} # managed by Certbot
if ($host = myproject.app) {
return 301 http://$host$request_uri;
} # managed by Certbot
if ($host = IP ADDRESS) {
return 301 http://$host$request_uri;
} # managed by Certbot
server_name myproject.app www.myproject.app IP ADDRESS;
listen 80;
# return 404; # managed by Certbot
}
gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/My-Project/backend
ExecStart=/usr/local/bin/gunicorn --log-file /home/myuser/gunicorn.log --log-level error --timeout 600 --workers 10 --threads 9 --bind unix:/My-Project/backend.sock main.wsgi
[Install]
WantedBy=multi-user.target
尝试迁移时出错
root@mycontainer:/My-Project/backend# sudo python3 manage.py makemigrations
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/usr/local/lib/python3.6/dist-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 197, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.6/dist-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.6/dist-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out
Is the server running on host "myproject-postgres-staging-db-do-user-6482921-0.db.ondigitalocean.com" (64.225.42.160) and accepting
TCP/IP connections on port 25060?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/makemigrations.py", line 101, in handle
loader.check_consistent_history(connection)
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/loader.py", line 283, in check_consistent_history
applied = recorder.applied_migrations()
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/recorder.py", line 76, in applied_migrations
if self.has_table():
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/usr/local/lib/python3.6/dist-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 260, in cursor
return self._cursor()
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 236, in _cursor
self.ensure_connection()
File "/usr/local/lib/python3.6/dist-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/usr/local/lib/python3.6/dist-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/base.py", line 197, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.6/dist-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.6/dist-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection timed out
Is the server running on host "myproject-postgres-staging-db-do-user-6482921-0.db.ondigitalocean.com" (64.225.42.160) and accepting
TCP/IP connections on port 25060?
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit
来自 settings.py 的数据库配置:
DATABASES = {
'default': {
'ENGINE': os.getenv("DB_ENGINE"),
'NAME': os.getenv("DB_NAME"),
'USER': os.getenv("DB_USER"),
'PASSWORD': os.getenv("DB_PASSWORD"),
'HOST': os.getenv("DB_HOST"),
'PORT': os.getenv("DB_PORT"),
'OPTIONS': {'sslmode': 'require'},
}
}
解决方案
推荐阅读
- python - 结束 if 语句而不移动到下一行
- java - How do I track a computed field fetched by a custom JPA @Query?
- javascript - 对从外部 javascript 引用的 ES6 代码使用 gulp+browserify+babalify。
- docker - Docker friendlyhello 构建失败
- google-chrome-extension - 使用 Mozilla 的 webextension-polyfill 的 Chrome 扩展中未显示通知
- installshield - Installshield .Net 框架先决条件不为没有 .net 4.0 或更高版本的用户安装
- python - Pygame 在按键时移动物体
- apache-flink - Apache Flink - CoFlatMapFunction 不按事件时间顺序处理事件
- go - Gorilla/schema 是否原生支持 `json` 反射标签?
- android - 为什么 TextInput 上的 onKeyPress 在 React Native (Android) 中不起作用?