nginx + rtmp 推流
阿新 • • 發佈:2018-12-30
環境
安裝Homebrow
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝nginx
下載nginx專案到本地
brew tap homebrew/nginx
執行安裝
brew install nginx-full -with-rtmp-module
到這一步,nginx和rtmp模組已經安裝好了。
檢視nginx資訊:
brew info nginx-full
終端資訊:
homebrew/nginx/nginx-full : stable 1.12.2, devel 1.13.6, HEAD
HTTP(S) server, reverse proxy, IMAP/POP3 proxy server
https://nginx.org/
Conflicts with:
nginx (because nginx-full symlink with the name for compatibility with nginx)
/usr/local/Cellar/nginx-full/1.12.2 (8 files, 1.2MB) *
Built from source on 2017-11-09 at 11:42:29 with: -- with-rtmp-module
From: https://github.com/Homebrew/homebrew-nginx/blob/master/Formula/nginx-full.rb
==> Dependencies
Required: pcre ✔, openssl ✔
Optional: passenger ✘, geoip ✘, gperftools ✘, gd ✘, imlib2 ✘
==> Options
--with-accept-language-module
Build with Accept Language support
--with-accesskey -module
Build with HTTP Access Key support
--with-addition
Build with HTTP Addition support
--with-ajp-module
Build with AJP-protocol support
--with-anti-ddos-module
Build with Anti-DDoS support
--with-array-var-module
Build with Array Var support
--with-auth-digest-module
Build with Auth Digest support
--with-auth-ldap-module
Build with Auth LDAP support
--with-auth-pam-module
Build with Auth PAM support
--with-auth-req
Build with HTTP Auth Request support
--with-auto-keepalive-module
Build with Auto Disable KeepAlive support
--with-autols-module
Build with Flexible Auto Index support
--with-cache-purge-module
Build with Cache Purge support
--with-captcha-module
Build with Captcha support
--with-counter-zone-module
Build with Realtime Counter Zone support
--with-ctpp2-module
Build with CT++ support
--with-dav-ext-module
Build with HTTP WebDav Extended support
--with-debug
Build with debug log
--with-degredation
Build with HTTP Degredation support
--with-dosdetector-module
Build with detecting DoS attacks support
--with-echo-module
Build with Echo support
--with-eval-module
Build with Eval support
--with-extended-status-module
Build with Extended Status support
--with-fancyindex-module
Build with Fancy Index support
--with-flv
Build with FLV support
--with-gd
Build with gd support
--with-geoip
Build with geoip support
--with-geoip2-module
Build with GeoIP2 support
--with-google-perftools
Build with Google Performance Tools support
--with-gperftools
Build with gperftools support
--with-gunzip
Build with Gunzip support
--with-gzip-static
Build with Gzip static support
--with-headers-more-module
Build with Headers More support
--with-healthcheck-module
Build with Healthcheck support
--with-homebrew-libressl
Include LibreSSL instead of OpenSSL via Homebrew
--with-http-accounting-module
Build with HTTP Accounting support
--with-http-flood-detector-module
Build with Var Flood-Threshold support
--with-http-remote-passwd-module
Build with Remote Basic Auth Password support
--with-http2
Build with HTTP/2 support
--with-image-filter
Build with Image Filter support
--with-imlib2
Build with imlib2 support
--with-log-if-module
Build with Log-if support
--with-lua-module
Build with LUA support
--with-mail
Build with Mail support
--with-mail-ssl
Build with Mail SSL/TLS support
--with-mod-zip-module
Build with HTTP Zip support
--with-mogilefs-module
Build with HTTP MogileFS support
--with-mp4
Build with MP4 support
--with-mp4-h264-module
Build with HTTP MP4/H264 support
--with-mruby-module
Build with MRuby support
--with-naxsi-module
Build with Naxsi support
--with-nchan-module
Build with Nchan support
--with-no-pool-nginx
Build without nginx-pool (valgrind debug memory)
--with-notice-module
Build with HTTP Notice support
--with-passenger
Build with passenger support
--with-pcre-jit
Build with JIT in PCRE
--with-perl
Build with Perl support
--with-php-session-module
Build with Parse PHP Sessions support
--with-push-stream-module
Build with HTTP Push Stream support
--with-random-index
Build with Random Index support
--with-realip
Build with Real IP support
--with-realtime-req-module
Build with Realtime Request support
--with-redis-module
Build with Redis support
--with-redis2-module
Build with Redis2 support
--with-rtmp-module
Build with RTMP support
--with-secure-link
Build with Secure Link support
--with-set-misc-module
Build with Set Misc support
--with-slice
Build with Slice support
--with-small-light-module
Build with Small Light support
--with-status
Build with Stub Status support
--with-stream
Build with TCP/UDP proxy support
--with-stream-geoip
Build with Stream GeoIP support
--with-stream-realip
Build with Stream RealIP support
--with-stream-ssl
Build with Stream SSL/TLS support
--with-stream-ssl-preread
Build with Stream without terminating SSL/TLS support
--with-sub
Build with HTTP Sub support
--with-subs-filter-module
Build with Substitutions Filter support
--with-tarantool-module
Build with Tarantool upstream support
--with-tcp-proxy-module
Build with TCP Proxy support
--with-txid-module
Build with Sortable Unique ID support
--with-unzip-module
Build with UnZip support
--with-upload-module
Build with Upload support
--with-upload-progress-module
Build with Upload Progress support
--with-upstream-order-module
Build with Order Upstream support
--with-ustats-module
Build with Upstream Statistics (HAProxy style) support
--with-var-req-speed-module
Build with Var Request-Speed support
--with-vod-module
Build with VOD on-the-fly MP4 Repackager support
--with-webdav
Build with WebDAV support
--with-websockify-module
Build with Websockify support
--with-xslt
Build with XSLT support
--with-xsltproc-module
Build with XSLT Transformations support
--devel
Install development version 1.13.6
--HEAD
Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /usr/local/etc/nginx/servers/.
- Tips -
Run port 80:
$ sudo chown root:wheel /usr/local/opt/nginx-full/bin/nginx
$ sudo chmod u+s /usr/local/opt/nginx-full/bin/nginx
Reload config:
$ nginx -s reload
Reopen Logfile:
$ nginx -s reopen
Stop process:
$ nginx -s stop
Waiting on exit process
$ nginx -s quit
To have launchd start homebrew/nginx/nginx-full now and restart at login:
brew services start homebrew/nginx/nginx-full
Or, if you don't want/need a background service you can just run:
nginx
nginx安裝所在位置:
/usr/local/Cellar/nginx-full/1.12.2/bin/nginx
nginx配置檔案所在位置:
/usr/local/etc/nginx/nginx.conf
執行nginx
nginx
內容如下:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
代表nginx安裝成功
如果終端提示:
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
則表示8080埠被佔用了,檢視埠的pid
lsof -i tcp:8080
結果如下:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
xxx 7056 ygdx_lk 7u IPv4 0xf49473a52270b0b 0t0 TCP *:http-alt (LISTEN)
kill掉佔用8080埠的pid
kill 7056(佔用8080埠的pid)
然後重新執行nginx
nginx常用方法:
命令 | 含義 |
---|---|
nginx -s reload | 重新載入配置檔案 |
nginx -s reopen | 重新載入日誌 |
nginx -s stop | 停止nginx |
nginx -s quit | 退出nigix |
配置rtmp
修改nginx.conf這個配置檔案(/usr/local/etc/nginx/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;
}
rtmp {
server{
listen 1066;
#直播流配置
application rtmplive{
live on;
#為rtmp設定最大連結數。預設為off
max_connections 1024;
}
application hls{
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 1s;
}
}
}
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 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
# }
#}
include servers/*;
}
主要添加了這段配置:
rtmp {
server{
listen 1066;
#直播流配置
application rtmplive{
live on;
#為rtmp設定最大連結數。預設為off
max_connections 1024;
}
application hls{
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 1s;
}
}
}
安裝ffmpeg工具
brew install ffmpeg
推流
視訊檔案地址:/Users/xu/Desktop/input.mp4
推流拉流地址:rtmp://localhost:1935/rtmplive/home
acc:RTMP的音訊格式
flv: RTMP的視訊格式
ffmpeg -re -i /Users/xxx/Desktop/input.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1066/rtmplive/home
拉流
開啟VLC播放器
File->Open Network…
設定拉流地址:rtmp://localhost:1066/rtmplive/home
推流以後,點選open開始播放。
注意點:
開啟網路偏好設定->狀態->“Wi-Fi”已連線至“XXX”,其 IP 地址為 192.168.2.85
rtmp://192.168.2.85:1066/rtmplive/home則與上面的localhost是等價的。後續用手機進行推流,就不能用localhost推流了,只能改成對應的ip推流。