Nginx+Supervisor+WebSocket+Sanic+Redis -- 用Nginx和supervisor来部署WebSocket

环境: 阿里云Ubuntu18

Step 1:

sudo apt update
sudo apt install nginx
sudo apt install supervisor
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl enable supervisor

Step 2:

sudo vi /etc/nginx/sites-available/mysite.com

mysite.com文件内容如下:

upstream my_websocket {
   server 127.0.0.1:8528;   # 8528是websocket服务启动时绑定的端口号
}


map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
    server_name mysite.com;
    ...

    location /feed {
      proxy_pass http://my_websocket;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_set_header Host $host;
      proxy_read_timeout 86400;  # 24 * 60 * 60;
    }
    ...
}

Step 3:

sudo ln -s /etc/nginx/sites-availabl/mysite.com /etc/nginx/sites-enable/mysite.com
sudo nginx -t
sudo nginx -s reload

Step 4:

https://blog.csdn.net/jaket5219999/article/details/80689076 (添加https证书)

Step 5:

sudo vi /etc/supervisor/conf.d/mysite.conf

content of mysite.conf:

[program:mywebsocket]
;python需用虚拟环境里的绝对路径(激活虚拟环境后用which python查看)
command=/home/myname/.local/share/virtualenvs/project-IPGBstTu/bin/python /path/to/project/ws/app.py
numprocs=1
autostart=true
startsecs=10
autorestart=true
startretried=3
user=myname
redirect_stderr=true
stdout_logfile=/path/to/project/ws/ws.log
stdout_logfile_maxbytes=20MB
stdout_logfile_backups=20

Step 6:

sudo chown myname:myname /etc/supervisor/conf.d/mysite.conf
sudo supervisorctl update
sudo supervisorctl start mywebsocket
sudo supervisorctl status

Step 7:

// 在浏览器Console测试连接
ws = new WebSocket('wss://mysite.com/feed')

 

你可能感兴趣的:(Linux)