在 nginx 后面使用 Daphne
Using Daphne behind nginx
我有一个使用 uWSGI 和 Nginx 进行生产部署的 Django 应用程序。现在正在集成Channels 2.0,意识到Daphne是这里uwsgi的替代品。当 运行 在独立模式下连接它时,我能够让 Daphne 服务,但是当我尝试 运行 它在 nginx 代理后面时,我不断收到连接超时 (504)
我的 nginx 配置文件是我为 uWSGI 服务器配置的,我希望它能正常工作,因为套接字名称相同。
可能的问题:
- 从 nginx 日志中的情况来看,nginx 尝试连接到
uwsgi://unix:///path/to/nginx.sock
,但我认为它不应该以 uwsgi
为前缀,而应该只是 unix:///path/to/nginx.sock
。
后续问题:
- 是否建议同时使用 Daphne 来服务器 http 和 websocket?或者我应该将 uWSGI 用于 http 而 Daphne 仅用于 websockets。
- 如果两者都使用 Daphne,我保留同一个 uwsgi_params 文件是否正确?
settings.py
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_celery_results',
'home',
'corsheaders',
'rest_framework',
'channels'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'
]
WSGI_APPLICATION = 'home.wsgi.application'
ASGI_APPLICATION = "home.routing.application"
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(REDIS_HOST, REDIS_PORT)],
},
},
}
asgi.py
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "home.settings_dev")
django.setup()
application = get_default_application()
nginx.conf
upstream home {
server unix:///Users/pranavprakash/workspace/HomeApp/nginx.sock;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /Users/pranavprakash/workspace/HomeApp/media;
}
location /static {
alias /Users/pranavprakash/workspace/HomeApp/staticfiles;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass home;
include /Users/pranavprakash/workspace/HomeApp/uwsgi_params;
}
}
Daphne 服务器日志
daphne -u nginx.sock home.asgi:application
2018-06-11 07:09:27,062 INFO Starting server at unix:nginx.sock
2018-06-11 07:09:27,062 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-06-11 07:09:27,063 INFO Configuring endpoint unix:nginx.sock
Nginx 日志
2018/06/11 12:41:09 [error] 56711#0: *1 upstream timed out (60: Operation timed out) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///Users/pranavprakash/workspace/HomeApp/nginx.sock", host: "localhost"
2018/06/11 12:41:47 [info] 56711#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection
如果我理解正确,uwsgi 不再为 "home" 上游提供服务,请尝试将 uwsgi_pass home;
更改为 proxy_pass home;
我有一个使用 uWSGI 和 Nginx 进行生产部署的 Django 应用程序。现在正在集成Channels 2.0,意识到Daphne是这里uwsgi的替代品。当 运行 在独立模式下连接它时,我能够让 Daphne 服务,但是当我尝试 运行 它在 nginx 代理后面时,我不断收到连接超时 (504)
我的 nginx 配置文件是我为 uWSGI 服务器配置的,我希望它能正常工作,因为套接字名称相同。
可能的问题:
- 从 nginx 日志中的情况来看,nginx 尝试连接到
uwsgi://unix:///path/to/nginx.sock
,但我认为它不应该以uwsgi
为前缀,而应该只是unix:///path/to/nginx.sock
。
后续问题:
- 是否建议同时使用 Daphne 来服务器 http 和 websocket?或者我应该将 uWSGI 用于 http 而 Daphne 仅用于 websockets。
- 如果两者都使用 Daphne,我保留同一个 uwsgi_params 文件是否正确?
settings.py
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_celery_results',
'home',
'corsheaders',
'rest_framework',
'channels'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'
]
WSGI_APPLICATION = 'home.wsgi.application'
ASGI_APPLICATION = "home.routing.application"
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [(REDIS_HOST, REDIS_PORT)],
},
},
}
asgi.py
import os
import django
from channels.routing import get_default_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "home.settings_dev")
django.setup()
application = get_default_application()
nginx.conf
upstream home {
server unix:///Users/pranavprakash/workspace/HomeApp/nginx.sock;
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /Users/pranavprakash/workspace/HomeApp/media;
}
location /static {
alias /Users/pranavprakash/workspace/HomeApp/staticfiles;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass home;
include /Users/pranavprakash/workspace/HomeApp/uwsgi_params;
}
}
Daphne 服务器日志
daphne -u nginx.sock home.asgi:application
2018-06-11 07:09:27,062 INFO Starting server at unix:nginx.sock
2018-06-11 07:09:27,062 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-06-11 07:09:27,063 INFO Configuring endpoint unix:nginx.sock
Nginx 日志
2018/06/11 12:41:09 [error] 56711#0: *1 upstream timed out (60: Operation timed out) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///Users/pranavprakash/workspace/HomeApp/nginx.sock", host: "localhost"
2018/06/11 12:41:47 [info] 56711#0: *1 kevent() reported that client 127.0.0.1 closed keepalive connection
如果我理解正确,uwsgi 不再为 "home" 上游提供服务,请尝试将 uwsgi_pass home;
更改为 proxy_pass home;