介绍
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配置完毕