使用 openresty/nginx 搭建mp4視訊伺服器
阿新 • • 發佈:2019-02-05
首先,環境中必然要有gcc-c++環境
yum -y install gcc-c++
1 使用openresty
這裡使用的是1.11.2.1版本的openresty和1.0.2版本的openssl
yum install readline-devel pcre-devel openssl-devel gcc
tar -zxvf openresty-1.11.2.1.tar.gz
tar -zxvf openssl-1.0.2h.tar.gz
cd openresty-1.11.2.1
./configure --prefix=/app/openresty --user=xxx -- group=xxx --with-http_v2_module --with-openssl=/home/appdeploy/nginx/openssl-1.0.2h --with-select_module --with-poll_module --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_sub_module --with-http_stub_status_module -- with-http_perl_module --with-http_mp4_module --with-http_flv_module
可以在這裡設定user和group,也可以稍後使用下面的命令進行設定
chown -R [user]:[group] 資料夾名
上面的命令執行之後,編譯並安裝openrestry,安裝目錄就是配置中指定的/app/openrestry
make && make install
如果安裝的時候沒有許可權,可以用su切到root,注意安裝之後的openrestry目錄的許可權即可。
此時openrestry已經安裝好,到安裝目錄中修改openrestry下的nginx資料夾下的nginx.conf配置檔案
worker_processes 1; #工作程序數,一般設定為1就可以了
#error_log /usr/local/nginx/logs/error.log crit;
#pid /usr/local/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
keepalive_timeout 60;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
access_log off;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server {
listen 8080;
server_name 10.202.94.16;
root /app/openresty/nginx/html/;
limit_rate_after 30m;
limit_rate 700k; #這裡根據需要設定,意思是視訊緩衝30M之後,限速為700k/s
index index.html;
charset utf-8;
location ~ \.flv$ {
flv;
}
location ~ \.mp4$ {
mp4;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
修改之後,啟動nginx伺服器
/app/openresty/nginx/sbin/nginx -c /app/openresty/nginx/conf/nginx.conf
2 使用nginx
建議採用nginx 1.1.3版本之後的nginx,預設支援mp4,就無需再安裝一堆繁瑣的外掛。這裡使用的是1.3.14版本。
tar -zxvf nginx-1.3.14.tar.gz
cd nginx-1.3.14
./configure --prefix=/app/nginx --user=xxx --group=xxx --with-select_module --with-poll_module --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_sub_module --with-http_stub_status_module --with-http_perl_module --with-http_mp4_module --with-http_flv_module
make&& make install
然後同樣的,去/app/nginx做和openrestry中的nginx一樣的修改即可,注意修改目錄。
效果如下所示,此時的視訊是橫跨整個螢幕的,如果想要修改,比如做頁面的內嵌視訊,可以把視訊放到html5頁面中,再通過nginx伺服器訪問html檔案即可。有個開源的video.js很好用,這裡就不再贅述了。