首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nginx后面使用Daphne

在nginx后面使用Daphne
EN

Stack Overflow用户
提问于 2018-06-11 07:29:30
回答 1查看 7K关注 0票数 8

我有一个Django应用程序,它将uWSGI与Nginx一起用于生产部署。现在,我正在集成通道2.0,并意识到Daphne是这里的uwsgi的替代品。我可以让Daphne在独立模式下运行它,但是当我试图在nginx代理之后运行它时,我总是得到连接超时(504)。

我的nginx文件是我为uWSGI服务器配置的文件,我希望它只运行otb,因为套接字名称是相同的。

可能出现的问题

  • 从nginx日志中的情况来看,nginx试图连接到uwsgi://unix:///path/to/nginx.sock,但我认为它不应该以uwsgi作为前缀,而应该是unix:///path/to/nginx.sock

后续问题

  • 是否建议将Daphne同时用于服务器http和websocket?或者,我应该只为websockets使用uWSGI用于http和Daphne。
  • 如果两者都使用Daphne,我是否正确地保留了相同的uwsgi_params文件?

settings.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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服务器记录

代码语言:javascript
复制
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日志

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-11 07:54:39

如果我的理解正确,上游的“家”不再由uwsgi提供服务,请尝试将uwsgi_pass home;改为proxy_pass home;

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50792452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档