通過nginx實現瀏覽器播放視頻
阿新 • • 發佈:2018-04-19
nginx 播放 視頻 本文檔只適用於服務器沒有安裝nginx。如果已經安裝nginx,可能還需要其它方法。
確認本機IP地址:
[root@qa-gongneng-bj_10-0-129-98 ~]# ifconfig eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.129.98 netmask 255.255.255.0 broadcast 10.0.129.255 inet6 fe80::250:56ff:fe9d:17d8 prefixlen 64 scopeid 0x20<link> ether 00:50:56:9d:17:d8 txqueuelen 1000 (Ethernet) RX packets 69998305 bytes 4370336225 (4.0 GiB) RX errors 0 dropped 1992 overruns 0 frame 0 TX packets 32004 bytes 2665429 (2.5 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 384 bytes 20736 (20.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 384 bytes 20736 (20.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
當前機器沒有nginx服務:(至少沒有啟動nginx服務)
[root@qa-gongneng-bj_10-0-129-98 ~]# ps -ef|grep nginx root 28426 28404 0 10:23 pts/0 00:00:00 grep --color=auto nginx
創建軟件包下載目錄:
[root@qa-gongneng-bj_10-0-129-98 ~]# mkdir -p /home/huangyaoqi/tools/ [root@qa-gongneng-bj_10-0-129-98 ~]# cd /home/huangyaoqi/tools/
下載nginx軟件包:
[root@qa-gongneng-bj_10-0-129-98 tools]# wget http://nginx.org/download/nginx-1.6.3.tar.gz --2018-04-19 10:23:42-- http://nginx.org/download/nginx-1.6.3.tar.gz 正在解析主機 nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2606:7100:1:69::3f, ... 正在連接 nginx.org (nginx.org)|95.211.80.227|:80... 已連接。 已發出 HTTP 請求,正在等待回應... 200 OK 長度:805253 (786K) [application/octet-stream] 正在保存至: “nginx-1.6.3.tar.gz” 100%[====================================================================================================================================================================>] 805,253 87.8KB/s 用時 11s 2018-04-19 10:23:53 (74.8 KB/s) - 已保存 “nginx-1.6.3.tar.gz” [805253/805253])
下載nginx_rtmp_module包:
[root@qa-gongneng-bj_10-0-129-98 tools]# wget https://github.com/arut/nginx-rtmp-module/archive/master.zip --2018-04-19 10:24:31-- https://github.com/arut/nginx-rtmp-module/archive/master.zip 正在解析主機 github.com (github.com)... 13.250.177.223, 13.229.188.59, 52.74.223.119 正在連接 github.com (github.com)|13.250.177.223|:443... 已連接。 已發出 HTTP 請求,正在等待回應... 302 Found 位置:https://codeload.github.com/arut/nginx-rtmp-module/zip/master [跟隨至新的 URL] --2018-04-19 10:24:32-- https://codeload.github.com/arut/nginx-rtmp-module/zip/master 正在解析主機 codeload.github.com (codeload.github.com)... 13.229.189.0, 54.251.140.56, 13.250.162.133 正在連接 codeload.github.com (codeload.github.com)|13.229.189.0|:443... 已連接。 已發出 HTTP 請求,正在等待回應... 200 OK 長度:545945 (533K) [application/zip] 正在保存至: “master.zip” 100%[====================================================================================================================================================================>] 545,945 231KB/s 用時 2.3s 2018-04-19 10:24:36 (231 KB/s) - 已保存 “master.zip” [545945/545945])
確認下載的安裝包:
[root@qa-gongneng-bj_10-0-129-98 tools]# ll 總用量 1324 -rw-r--r-- 1 root root 545945 4月 19 10:24 master.zip -rw-r--r-- 1 root root 805253 4月 8 2015 nginx-1.6.3.tar.gz
解壓軟件包:
[root@qa-gongneng-bj_10-0-129-98 tools]# tar xf nginx-1.6.3.tar.gz [root@qa-gongneng-bj_10-0-129-98 tools]# unzip master.zip Archive: master.zip 791b6136f02bc9613daf178723ac09f4df5a3bbf creating: nginx-rtmp-module-master/ inflating: nginx-rtmp-module-master/AUTHORS inflating: nginx-rtmp-module-master/LICENSE inflating: nginx-rtmp-module-master/README.md inflating: nginx-rtmp-module-master/config creating: nginx-rtmp-module-master/dash/ inflating: nginx-rtmp-module-master/dash/ngx_rtmp_dash_module.c inflating: nginx-rtmp-module-master/dash/ngx_rtmp_mp4.c inflating: nginx-rtmp-module-master/dash/ngx_rtmp_mp4.h creating: nginx-rtmp-module-master/doc/ inflating: nginx-rtmp-module-master/doc/README.md creating: nginx-rtmp-module-master/hls/ inflating: nginx-rtmp-module-master/hls/ngx_rtmp_hls_module.c inflating: nginx-rtmp-module-master/hls/ngx_rtmp_mpegts.c inflating: nginx-rtmp-module-master/hls/ngx_rtmp_mpegts.h inflating: nginx-rtmp-module-master/ngx_rtmp.c inflating: nginx-rtmp-module-master/ngx_rtmp.h inflating: nginx-rtmp-module-master/ngx_rtmp_access_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_amf.c inflating: nginx-rtmp-module-master/ngx_rtmp_amf.h inflating: nginx-rtmp-module-master/ngx_rtmp_auto_push_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_bandwidth.c inflating: nginx-rtmp-module-master/ngx_rtmp_bandwidth.h inflating: nginx-rtmp-module-master/ngx_rtmp_bitop.c inflating: nginx-rtmp-module-master/ngx_rtmp_bitop.h inflating: nginx-rtmp-module-master/ngx_rtmp_cmd_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_cmd_module.h inflating: nginx-rtmp-module-master/ngx_rtmp_codec_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_codec_module.h inflating: nginx-rtmp-module-master/ngx_rtmp_control_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_core_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_eval.c inflating: nginx-rtmp-module-master/ngx_rtmp_eval.h inflating: nginx-rtmp-module-master/ngx_rtmp_exec_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_flv_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_handler.c inflating: nginx-rtmp-module-master/ngx_rtmp_handshake.c inflating: nginx-rtmp-module-master/ngx_rtmp_init.c inflating: nginx-rtmp-module-master/ngx_rtmp_limit_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_live_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_live_module.h inflating: nginx-rtmp-module-master/ngx_rtmp_log_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_mp4_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_netcall_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_netcall_module.h inflating: nginx-rtmp-module-master/ngx_rtmp_notify_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_play_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_play_module.h inflating: nginx-rtmp-module-master/ngx_rtmp_proxy_protocol.c inflating: nginx-rtmp-module-master/ngx_rtmp_proxy_protocol.h inflating: nginx-rtmp-module-master/ngx_rtmp_receive.c inflating: nginx-rtmp-module-master/ngx_rtmp_record_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_record_module.h inflating: nginx-rtmp-module-master/ngx_rtmp_relay_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_relay_module.h inflating: nginx-rtmp-module-master/ngx_rtmp_send.c inflating: nginx-rtmp-module-master/ngx_rtmp_shared.c inflating: nginx-rtmp-module-master/ngx_rtmp_stat_module.c inflating: nginx-rtmp-module-master/ngx_rtmp_streams.h inflating: nginx-rtmp-module-master/ngx_rtmp_version.h inflating: nginx-rtmp-module-master/stat.xsl creating: nginx-rtmp-module-master/test/ inflating: nginx-rtmp-module-master/test/README.md inflating: nginx-rtmp-module-master/test/dump.sh inflating: nginx-rtmp-module-master/test/ffstream.sh inflating: nginx-rtmp-module-master/test/nginx.conf extracting: nginx-rtmp-module-master/test/play.sh creating: nginx-rtmp-module-master/test/rtmp-publisher/ inflating: nginx-rtmp-module-master/test/rtmp-publisher/README.md inflating: nginx-rtmp-module-master/test/rtmp-publisher/RtmpPlayer.mxml extracting: nginx-rtmp-module-master/test/rtmp-publisher/RtmpPlayer.swf inflating: nginx-rtmp-module-master/test/rtmp-publisher/RtmpPlayerLight.mxml extracting: nginx-rtmp-module-master/test/rtmp-publisher/RtmpPlayerLight.swf inflating: nginx-rtmp-module-master/test/rtmp-publisher/RtmpPublisher.mxml extracting: nginx-rtmp-module-master/test/rtmp-publisher/RtmpPublisher.swf inflating: nginx-rtmp-module-master/test/rtmp-publisher/player.html inflating: nginx-rtmp-module-master/test/rtmp-publisher/publisher.html inflating: nginx-rtmp-module-master/test/rtmp-publisher/swfobject.js creating: nginx-rtmp-module-master/test/www/ inflating: nginx-rtmp-module-master/test/www/bg.jpg inflating: nginx-rtmp-module-master/test/www/index.html creating: nginx-rtmp-module-master/test/www/jwplayer/ inflating: nginx-rtmp-module-master/test/www/jwplayer/jwplayer.flash.swf inflating: nginx-rtmp-module-master/test/www/jwplayer/jwplayer.js creating: nginx-rtmp-module-master/test/www/jwplayer_old/ extracting: nginx-rtmp-module-master/test/www/jwplayer_old/player.swf inflating: nginx-rtmp-module-master/test/www/jwplayer_old/swfobject.js inflating: nginx-rtmp-module-master/test/www/record.html [root@qa-gongneng-bj_10-0-129-98 tools]# ll 總用量 1328 -rw-r--r-- 1 root root 545945 4月 19 10:24 master.zip drwxr-xr-x 8 testuser testuser 147 4月 7 2015 nginx-1.6.3 -rw-r--r-- 1 root root 805253 4月 8 2015 nginx-1.6.3.tar.gz drwxr-xr-x 6 root root 4096 11月 29 18:47 nginx-rtmp-module-master
給nginx安裝目錄創建軟鏈接:
[root@qa-gongneng-bj_10-0-129-98 tools]# ln -s /home/huangyaoqi/tools/nginx-1.6.3 /home/huangyaoqi/tools/nginx
配置nginx:
[root@qa-gongneng-bj_10-0-129-98 tools]# cd nginx [root@qa-gongneng-bj_10-0-129-98 nginx]# ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master checking for OS + Linux 3.10.0-123.el7.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) checking for gcc -pipe switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found checking for gcc variadic macros ... found checking for unistd.h ... found checking for inttypes.h ... found checking for limits.h ... found checking for sys/filio.h ... not found checking for sys/param.h ... found checking for sys/mount.h ... found checking for sys/statvfs.h ... found ……後面略……
確認這部分執行命令成功:
[root@qa-gongneng-bj_10-0-129-98 nginx]# echo $? 0
編譯nginx:
[root@qa-gongneng-bj_10-0-129-98 nginx]# make make -f objs/Makefile make[1]: 進入目錄“/home/huangyaoqi/tools/nginx-1.6.3” cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I../nginx-rtmp-module-master -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/nginx.o src/core/nginx.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I../nginx-rtmp-module-master -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_log.o src/core/ngx_log.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I../nginx-rtmp-module-master -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_palloc.o src/core/ngx_palloc.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I../nginx-rtmp-module-master -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_array.o src/core/ngx_array.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I../nginx-rtmp-module-master -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_list.o src/core/ngx_list.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I../nginx-rtmp-module-master -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_hash.o src/core/ngx_hash.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I../nginx-rtmp-module-master -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -o objs/src/core/ngx_buf.o src/core/ngx_buf.c ……後面略……
確認這部分執行命令成功:
[root@qa-gongneng-bj_10-0-129-98 nginx]# echo $? 0
安裝nginx:
[root@qa-gongneng-bj_10-0-129-98 nginx]# make install make -f objs/Makefile install make[1]: 進入目錄“/home/huangyaoqi/tools/nginx-1.6.3” test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx' test -d '/usr/local/nginx/sbin' || mkdir -p '/usr/local/nginx/sbin' test ! -f '/usr/local/nginx/sbin/nginx' || mv '/usr/local/nginx/sbin/nginx' '/usr/local/nginx/sbin/nginx.old' cp objs/nginx '/usr/local/nginx/sbin/nginx' test -d '/usr/local/nginx/conf' || mkdir -p '/usr/local/nginx/conf' cp conf/koi-win '/usr/local/nginx/conf' cp conf/koi-utf '/usr/local/nginx/conf' cp conf/win-utf '/usr/local/nginx/conf' test -f '/usr/local/nginx/conf/mime.types' || cp conf/mime.types '/usr/local/nginx/conf' cp conf/mime.types '/usr/local/nginx/conf/mime.types.default' test -f '/usr/local/nginx/conf/fastcgi_params' || cp conf/fastcgi_params '/usr/local/nginx/conf' cp conf/fastcgi_params '/usr/local/nginx/conf/fastcgi_params.default' test -f '/usr/local/nginx/conf/fastcgi.conf' || cp conf/fastcgi.conf '/usr/local/nginx/conf' cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default' test -f '/usr/local/nginx/conf/uwsgi_params' || cp conf/uwsgi_params '/usr/local/nginx/conf' cp conf/uwsgi_params '/usr/local/nginx/conf/uwsgi_params.default' test -f '/usr/local/nginx/conf/scgi_params' || cp conf/scgi_params '/usr/local/nginx/conf' cp conf/scgi_params '/usr/local/nginx/conf/scgi_params.default' test -f '/usr/local/nginx/conf/nginx.conf' || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf' cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default' test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/html' || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' || mkdir -p '/usr/local/nginx/logs' make[1]: 離開目錄“/home/huangyaoqi/tools/nginx-1.6.3”
確認這部分執行命令成功:
[root@qa-gongneng-bj_10-0-129-98 nginx]# echo $? 0
啟動nginx服務:
[root@qa-gongneng-bj_10-0-129-98 nginx]# /usr/local/nginx/sbin/nginx
檢查nginx服務版本:
[root@qa-gongneng-bj_10-0-129-98 nginx]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.6.3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) TLS SNI support enabled configure arguments: --with-http_ssl_module --add-module=../nginx-rtmp-module-master
修改後的nginx配置文件:(“###add 20180419”就是要添加的內容,就2個部分)
[root@qa-gongneng-bj_10-0-129-98 nginx]# cat /usr/local/nginx/conf/nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } ###add 20180419 rtmp { server { listen 1935; application vod { play /opt/video; #這是一個目錄的名稱,如果是linux,則寫具體位置如/opt/video } } } ############## http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } ###add 20180419 location /test.flv { root video; } location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root html; } ############# #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
語法檢查:
[root@qa-gongneng-bj_10-0-129-98 nginx]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
把視頻上傳到http服務的站點目錄:
[root@qa-gongneng-bj_10-0-129-98 nginx]# cd /usr/local/nginx/html/ [root@qa-gongneng-bj_10-0-129-98 html]# ll 總用量 8 -rw-r--r-- 1 root root 537 4月 19 10:31 50x.html -rw-r--r-- 1 root root 612 4月 19 10:31 index.html [root@qa-gongneng-bj_10-0-129-98 html]# rz -y rz waiting to receive. Starting zmodem transfer. Press Ctrl+C to cancel. Transferring sample.mp4... 100% 1030 KB 1030 KB/sec 00:00:01 0 Errors
平滑重啟nginx服務:(這一步可能不需要強制執行)
[root@qa-gongneng-bj_10-0-129-98 html]# ../sbin/nginx -s reload
在瀏覽器打開:測試沒有問題
通過nginx實現瀏覽器播放視頻