以前论坛用的是第三方 pusher 推送消息,感觉很不踏实,流量一多还总是有邮件提醒去缴费,而且发帖速度会变慢,于是就停掉了推送。
忽然发现有个 websocket 免费推送项目,于是赶紧给安排上,测试了一晚上,算是了了一样心病。下面记录一下搭建步骤:
安装插件
cd /var/www/flarum
composer require kyrne/websocket -W
配置nginx反代
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /app {
proxy_pass http://127.0.0.1:8443;
proxy_read_timeout 60;
proxy_connect_timeout 60;
proxy_redirect off;
# Allow the use of websockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
systemctl restart nginx
flarum后台浏览器上配置

运行websocket server
浏览器上开启插件,回到服务器运行 websocket server
前台运行:
cd /var/www/flarum
php flarum websocket:serve
配置后台运行:
vi /etc/systemd/system/websocket.service
复制下面内容:
[Unit]
Description=flarum-websocket
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
WorkingDirectory=/var/www/flarum
ExecStart=/usr/bin/php flarum websocket:serve
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
运行:
systemctl daemon-reload
systemctl start websocket
systemctl enable websocket
效果

最后注意,Cloudflare是支持wss反代的,但是你得放行http版本1.1,而且服务器上不能只容许CF IP访问。