Django 与 websockets - Uvicorn + Nginx
Django with websockets - Uvicorn + Nginx
我正在尝试 运行 一个 Django 网站,其频道已激活以使用 websocket。
使用 运行 服务器时一切正常,但切换到 Nginx + Uvicorn 时事情变得很棘手。
这是我的 /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/home/myapp/
ExecStart=/usr/local/bin/gunicorn -w 1 -k uvicorn.workers.UvicornWorker --timeout 300 --bind unix:/home/myapp/myapp.sock myapp.asgi:application
[Install]
WantedBy=multi-user.target
这是我的 /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/home/myapp/myapp.sock
[Install]
WantedBy=sockets.target
这是我的 /etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.box 10.42.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location ~ ^/static {
autoindex on;
root /home/myapp;
}
location ~ ^/ {
include proxy_params;
proxy_pass http://unix:/home/myapp/myapp.sock;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://unix:/home/myapp/myapp.sock;
}
}
Nginx 正在 运行ning,套接字和 gunicorn 出现问题,gunicorn 显示以下错误消息。
Jul 07 11:14:11 hostname gunicorn[1825]: File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 76, in __getattr__
Jul 07 11:14:11 hostname gunicorn[1825]: self._setup(name)
Jul 07 11:14:11 hostname gunicorn[1825]: File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 57, in _setup
Jul 07 11:14:11 hostname gunicorn[1825]: raise ImproperlyConfigured(
Jul 07 11:14:11 hostname gunicorn[1825]: django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the envir>
Jul 07 11:14:11 hostname gunicorn[1825]: [2021-07-07 11:14:11 +0800] [1825] [INFO] Worker exiting (pid: 1825)
Jul 07 11:14:12 hostname gunicorn[1822]: [2021-07-07 11:14:12 +0800] [1822] [INFO] Shutting down: Master
Jul 07 11:14:12 hostname gunicorn[1822]: [2021-07-07 11:14:12 +0800] [1822] [INFO] Reason: Worker failed to boot.
当我尝试访问我的网站时,出现“欢迎使用 nginx!”页面并且无法从我的站点获取页面(合乎逻辑,因为 Gunicorn 无法获取 Django 运行ning)
我还附上了我的 /home/myapp/asgi.py 文件
"""
ASGI config for myapp project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import myapp.routing
import MyApp.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
django.setup()
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack( URLRouter( MyApp.routing.websocket_urlpatterns ) ),
# Just HTTP for now. (We can add other protocols later.)
})
有人会知道发生了什么以及我遗漏了什么吗?
谢谢,
好的,我已设法修复它。
在 asgi.py 中,我正在导入一些外部模块,并且 settings.py 必须先加载。
我在 asgi.py
的顶部添加了以下两行
import django
django.setup()
在 qll 导入之前,它似乎到目前为止有效
我正在尝试 运行 一个 Django 网站,其频道已激活以使用 websocket。 使用 运行 服务器时一切正常,但切换到 Nginx + Uvicorn 时事情变得很棘手。
这是我的 /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/home/myapp/
ExecStart=/usr/local/bin/gunicorn -w 1 -k uvicorn.workers.UvicornWorker --timeout 300 --bind unix:/home/myapp/myapp.sock myapp.asgi:application
[Install]
WantedBy=multi-user.target
这是我的 /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/home/myapp/myapp.sock
[Install]
WantedBy=sockets.target
这是我的 /etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.box 10.42.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location ~ ^/static {
autoindex on;
root /home/myapp;
}
location ~ ^/ {
include proxy_params;
proxy_pass http://unix:/home/myapp/myapp.sock;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://unix:/home/myapp/myapp.sock;
}
}
Nginx 正在 运行ning,套接字和 gunicorn 出现问题,gunicorn 显示以下错误消息。
Jul 07 11:14:11 hostname gunicorn[1825]: File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 76, in __getattr__
Jul 07 11:14:11 hostname gunicorn[1825]: self._setup(name)
Jul 07 11:14:11 hostname gunicorn[1825]: File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 57, in _setup
Jul 07 11:14:11 hostname gunicorn[1825]: raise ImproperlyConfigured(
Jul 07 11:14:11 hostname gunicorn[1825]: django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the envir>
Jul 07 11:14:11 hostname gunicorn[1825]: [2021-07-07 11:14:11 +0800] [1825] [INFO] Worker exiting (pid: 1825)
Jul 07 11:14:12 hostname gunicorn[1822]: [2021-07-07 11:14:12 +0800] [1822] [INFO] Shutting down: Master
Jul 07 11:14:12 hostname gunicorn[1822]: [2021-07-07 11:14:12 +0800] [1822] [INFO] Reason: Worker failed to boot.
当我尝试访问我的网站时,出现“欢迎使用 nginx!”页面并且无法从我的站点获取页面(合乎逻辑,因为 Gunicorn 无法获取 Django 运行ning)
我还附上了我的 /home/myapp/asgi.py 文件
"""
ASGI config for myapp project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/
"""
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import myapp.routing
import MyApp.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
django.setup()
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack( URLRouter( MyApp.routing.websocket_urlpatterns ) ),
# Just HTTP for now. (We can add other protocols later.)
})
有人会知道发生了什么以及我遗漏了什么吗?
谢谢,
好的,我已设法修复它。 在 asgi.py 中,我正在导入一些外部模块,并且 settings.py 必须先加载。 我在 asgi.py
的顶部添加了以下两行import django
django.setup()
在 qll 导入之前,它似乎到目前为止有效