介紹
Nginx是一款輕量級的Web伺服器反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,并在一個BSD-like 協定下發行。其特點是占有記憶體少,并發能力強。今天來分享一下如何安裝配置nginx伺服器,并應用于直播推流中。
開發環境
Ubuntu 16 64位+Windows10
安裝
1.root使用者登入Ubuntu,使用如下指令下載下傳nginx
wget http://nginx.org/download/nginx-1.12.1.tar.gz
2.nginx依賴于一些開源庫,這些我們需要下載下傳下來
apt install libssl-dev
注意zlib1g g前邊是個數字1不是字母l
apt install zlib1g-dev
apt install libpcre3-dev
apt install gcc
apt install g++
3.接下來将nginx解壓
tar -xvf nginx-1.12.1.tar.gz
4.下載下傳rtmp子產品
使用git下載下傳,沒有下載下傳git先安裝
apt install git
在github上找到nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
将這個module添加到nginx上
./configure --add-module=/root/nginx-1.12.1/nginx-rtmp-module
5.開始編譯
make
可能在系統中已經安裝了nginx,為了防止無法覆寫,先rm
這是個預設安裝目錄
rm -r /usr/local/nginx/
6.安裝
make install
然後進入到usr/local/nginx目錄
注意這是root根目錄下的
cd usr/local/nginx/sbin/
7.執行nginx
./nginx
8.檢視是否已運作
ps -ef|grep nginx
在浏覽器上輸入ip檢視如果顯示welcome to nginx表示已經運作成功
9.rtmp配置
進入nginx安裝目錄:usr/local/nginx/conf修改nginx.conf檔案(注意是安裝目錄,不要傻傻的去解壓包中修改)
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
}
}
}
添加了這個之後,就可以推流了,我們使用ffmepg推一把試試.注意live是什麼,端口是什麼
ffmpeg -i C:\Users\renzhenming\Desktop\一首《秋意濃》打動阿琴冷酷的心,星爺太會泡妞了.mp4 -f flv -c copy rtmp://192.168.1.108/live
然後通過vcl播放器,輸入rtmp://192.168.1.108/live可以看到播放成功
然後在設定一把通過浏覽器監聽nginx狀态,還是在nginx.conf中添加
server {
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
//樣式表的位置,這個表是nginx-rtmp-module中的
root /root/nginx-1.12.1/nginx-rtmp-module;
}
}
這樣一來,我們通過浏覽器就可以看到推流的狀态了
在浏覽器中輸入
http://192.168.xx.xx:8080/stat
完整的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 1935;
chunk_size 4096;
application live {
live on;
}
}
}
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;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /root/nginx-1.12.1/nginx-rtmp-module;
}
}
server {
listen 80;
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;
# }
#}
}
至此,nginx配置完畢