在網上看到一個好東西,於是決定來使用以下。
這個東西之所以好是因爲,在自己的項目中嘗試了關於sse的東西,可是sse本身就很難控制,加上uwsgi+nginx超時以後會繼續佔用服務器的線程,而我也搞不清楚服務器要怎麼樣纔可以優雅的關閉連接,折騰了好久,打算直接用這個好用的工具了。
安裝
# 克隆 http-stream-push 項目到本地
git clone https://github.com/wandenberg/nginx-push-stream-module.git
# 下載nginx源碼自行編譯,在編譯的時候可以加上這個插件, nginx1.2.0以上的版本
wget http://nginx.org/download/nginx-1.16.1.tar.gz
# unzip
tar zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
# 配置,並添加nginx-push-stream-module
./configure --add-module=../nginx-push-stream-module --with-http_ssl_module
make
# 安裝
sudo make install
# 檢查
sudo /usr/local/nginx/sbin/nginx -v
# 測試配置文件
sudo /usr/local/nginx/sbin/nginx -c ../nginx-push-stream-module/misc/nginx.conf -t
# 運行
sudo /usr/local/nginx/sbin/nginx -c ../nginx-push-stream-module/misc/nginx.conf
配置:
找到安裝路徑(默認 /usr/local/nginx/), 然後編輯目錄下的nginx.conf
// 在 #user nobody; 下面添加:
user root;
// 修改爲2
worker_processes 2;
events {
worker_connections 1024;
# 添加下行
use epoll;
}
後續:
具體使用方式在下方第二個鏈接裏面有!
使用中遇到了一個問題,由於項目用了https,而這個模塊裏面的js生成的默認是http的,瀏覽器會有警告。
通過觀察源碼發現了可以在js代碼裏面開啓ssl:
在html頁面中添加的js代碼片段中的 new pushstream(...) 中的 modes: 'eventsource' 後面加入
useSSL = true
這樣瀏覽器就會發送https的請求。
參考:
https://www.nginx.com/resources/wiki/modules/push_stream/#push-stream-installation