如何在 unix 套接字上绑定 Daphne?

How to bind Daphne on a unix socket?

到目前为止,我已经使用 Gunicorn 和这个配置文件 systemd:

[Unit]
Description=gunicorn daemon
After=network.target


[Service]
EnvironmentFile=/var/www/user/.envvars
User=user
Group=www-data
WorkingDirectory=/var/www/user
ExecStart=/var/www/venv/bin/gunicorn --workers 3 --bind unix:/var/www/user/user.sock config.wsgi:application --env DJANGO_SETTINGS_MODULE='config.settings.production'

[Install]
WantedBy=multi-user.target

并且 Nginx 为这个 unix 套接字代理请求。

尝试Daphne,我发现我无法指定--env DJANGO_SETTINGS_MODULE='config.settings.production'

但是我在asgi.py里指定了这个变量,就一切正常了。

一旦我 运行 gunicorn service,套接字本身立即创建,来自 nginx.

的请求

当我尝试用达芙妮做同样的把戏时,我得到一个错误:

Jun 26 11:48:32 p435061.kvmvps daphne[8447]: ValueError: invalid literal for int() with base 8: '/var/www/user/user.sock'

但是当我尝试 运行 Daphne 并创建我的 systemd 文件时,我不知道如何使用 unix.sock

更新

我更改了设置文件,它给出了 st运行ge,但它有效:

[Unit]
Description=daphne daemon
After=network.target


[Service]
EnvironmentFile=/var/www/gglobal/.envvars
User=gglobal
Group=www-data
WorkingDirectory=/var/www/gglobal
ExecStart=/var/www/venv/bin/daphne -b unix:/var/www/gglobal/gglobal.sock  config.asgi:channel_layer -v2

[Install]
WantedBy=multi-user.target

systemctl 状态达芙妮 returns:

Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,722 DEBUG    get 'RDS_HOSTNAME' casted as 'None' with default '<NoValue>'
Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,722 DEBUG    get 'RDS_PORT' casted as 'None' with default '<NoValue>'
Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,722 DEBUG    get 'REDIS_ENDPOINT_ADDRESS' casted as 'None' with default '<NoValue>'
Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,722 DEBUG    get 'REDIS_PORT' casted as 'None' with default '<NoValue>'
Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,723 DEBUG    get 'DJANGO_SENTRY_DSN' casted as 'None' with default 'https://56bb387ee6ef4e7b
Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,723 DEBUG    get 'DJANGO_SENTRY_CLIENT' casted as 'None' with default 'raven.contrib.django.
Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,723 DEBUG    get 'DJANGO_SENTRY_LOG_LEVEL' casted as '<class 'int'>' with default '20'
Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,724 DEBUG    get 'DJANGO_SENTRY_LOG_LEVEL' casted as '<class 'int'>' with default '20'
Jun 26 17:53:46 p435061.kvmvps daphne[14183]: 2017-06-26 17:53:46,724 DEBUG    get 'DJANGO_ADMIN_URL' casted as 'None' with default '<NoValue>'
Jun 26 17:53:48 p435061.kvmvps daphne[14183]: DEBUG 2017-06-26 17:53:48,186 base 14183 140286354425600 Configuring Raven for host: <raven.conf.remote.RemoteCo

但是nginx发誓缺少*.sock文件:

2017/06/26 17:47:51 [crit] 12682#12682: *59 connect() to unix:/var/www/project/project.sock failed (2: No such file or directory) while connecting to upstream, client: 5.152.1.201, server: mydomain.com, request: "GET / HTTP/2.0", upstream: "http://unix:/var/www/project/project.sock:/", host: "mydomain.com"

我花了一整天的时间来解决这个问题,但什么也没发生。

答案很简单:

问题是我指出了-b unix:unix:前缀干扰了工作,当然要设置flag -u

[Unit]
Description=daphne daemon
After=network.target


[Service]
EnvironmentFile=/var/www/gglobal/.envvars
User=gglobal
Group=www-data
WorkingDirectory=/var/www/gglobal
ExecStart=/var/www/venv/bin/daphne -u /var/www/gglobal/gglobal.sock  config.asgi:channel_layer -v2

[Install]
WantedBy=multi-user.target