我正在尝试使用daphne和nginx部署需要http和websocket连接的Django应用程序。 nginx和daphne之间的连接是使用Linux套接字建立的。当nginx收到请求时,daphne将退出,错误为“地址已在使用中”
我确保同时运行nginx和daphne的两个用户都具有对sock文件的读写访问权限
nginx conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {worker_connections 1024;}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;}
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
upstream websocket{server unix:/run/daphne/bot.sock;}
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name 10.xx.xx.65;
location /static/js { alias /var/botstatic/js;}
location /static/img { alias /var/botstatic/img;}
location /static/css { alias /var/botstatic/css;}
location /static/webfonts {alias /var/botstatic/webfonts;}
location / {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;}
}
达芙妮服务文件
[Unit]
Description=daphne server script
After=network.target
[Service]
User=sattam
Group=sattam
WorkingDirectory=/home/sattam/sattambot
#Environment=DJANGO_SETTINGS_MODULE=myproject.settings
ExecStartPre=/home/sattam/ExecStartPre.sh
ExecStart=/usr/bin/daphne -u /run/daphne/bot.sock --access-log /var/log/daphne/log SattamBot.asgi:application
Restart=always
[Install]
WantedBy=multi-user.target
ExecStartPre
#!/bin/bash
if [ ! -f /run/daphne/bot.sock ];
then
mkdir -p /run/daphne;
touch /run/daphne/bot.sock;
fi
chmod g+rw /run/daphne/bot.sock;
if [ ! -f /var/log/django/log.log ];
then
touch /var/log/django/log.log;
fi
达芙妮日志
-- The start-up result is done.
Jul 15 15:06:34 chatbot daphne[7524]: 2019-07-15 15:06:34,606 INFO Starting server at unix:/run/daphne/bot.sock
Jul 15 15:06:34 chatbot daphne[7524]: 2019-07-15 15:06:34,606 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Jul 15 15:06:34 chatbot daphne[7524]: 2019-07-15 15:06:34,606 INFO Configuring endpoint unix:/run/daphne/bot.sock
Jul 15 15:06:34 chatbot daphne[7524]: 2019-07-15 15:06:34,608 CRITICAL Listen failure: Couldn't listen on any:b'/run/daphne/bot.sock': [Errno 98] Address already in use.
Jul 15 15:06:34 chatbot systemd[1]: daphne.service holdoff time over, scheduling restart.
Jul 15 15:06:34 chatbot systemd[1]: Stopped daphne server script.
-- Subject: Unit daphne.service has finished shutting down
期望nginx和daphne通过Linux套接字连接 实际结果nginx给出502 Bad Gateway错误,daphne退出并显示此错误
CRITICAL Listen failure: Couldn't listen on any:b'/run/daphne/bot.sock': [Errno 98] Address already in use.