Nginx是一个短小精悍,且性能超强的Web服务器,同时也可以用作反向代理服务器以及邮件服务器。除此之外Nginx也可以用于搭建一个简易的流媒体服务器。基于此,本文描述了在Nginx 1.8之上构建一个简易的流媒体服务器,同时通过反向代理的方式将其代理至流媒体服务器。
有关Nginx相关的基础知识
Linux 6下安装编译安装Nginx
Nginx 概述及日常管理
Nginx基于IP,端口,域名配置虚拟主机
Linux 下配置nginx到php的连接
一、当前的运行环境及安装文件
1、当前的运行环境
# more /etc/redhat-release
CentOS release 6.5 (Final)
# /u01/app/nginx/sbin/nginx -v
nginx version: nginx/1.8.1
2、安装文件下载
http://download.csdn.net/detail/robinson_0612/9738948
3、安装文件位置
# pwd
/u01/soft
# ls
yamdi-1.9.tar.gz nginx_mod_h264_streaming-2.2.7.tar.gz nginx-1.8.1
二、安装及重新编译Nginx
1、安装yamdi
# tar -xf yamdi-1.9.tar.gz
# cd yamdi-1.9
# make && make install
gcc -O2 -Wall yamdi.c -o yamdi
install -m 0755 -o root yamdi /usr/local/bin
2、安装nginx_mod_h264_streaming
# cd /u01/soft
# tar -xf nginx_mod_h264_streaming-2.2.7.tar.gz
# vim /u01/soft/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
###注释掉158-161行
157 /* TODO: Win32 */
158 /* if (r->zero_in_uri)
159 {
160 return NGX_DECLINED;
161 }
162 */
###否则出现如下错误
# make
In file included from /usr/local/src/nginx_mod_h264_streaming-2.2.7/src/ngx_http_h264_streaming_module.c:2:
/usr/local/src/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c: 在函数‘ngx_streaming_handler’中:
/usr/local/src/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158: 错误:
‘ngx_http_request_t’没有名为‘zero_in_uri’的成员
make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] 错误 1
make[1]: Leaving directory `/usr/local/src/nginx-1.8.1'
make: *** [build] 错误 2
3、查看nginx当前已编译的模块以及添加模块
# /u01/app/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments:
--prefix=/u01/app/nginx --sbin-path=/u01/app/nginx/sbin/nginx --conf-path=/u01/app/nginx/conf/nginx.conf
--error-log-path=/u01/app/nginx/logs/error.log --http-log-path=/u01/app/nginx/logs/access.log
--pid-path=/u01/app/nginx/nginx.pid --lock-path=/u01/app/nginx/nginx.lock
--http-client-body-temp-path=/u01/app/nginx/tempcache/client_temp
--http-proxy-temp-path=/u01/app/nginx/tempcache/proxy_temp
--http-fastcgi-temp-path=/u01/app/nginx/tempcache/fastcgi_temp
--http-uwsgi-temp-path=/u01/app/nginx/tempcache/uwsgi_temp
--http-scgi-temp-path=/u01/app/nginx/tempcache/scgi_temp --user=nginx --group=nginx
--with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module
--with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module
--with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module
--with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module
--with-file-aio --with-http_spdy_module --with-ipv6
###将nginx_mod_h264_streaming模块添加到nginx
# cd /u01/soft/nginx-1.8.1
# ./configure \
--prefix=/u01/app/nginx --sbin-path=/u01/app/nginx/sbin/nginx \
--conf-path=/u01/app/nginx/conf/nginx.conf --error-log-path=/u01/app/nginx/logs/error.log \
--http-log-path=/u01/app/nginx/logs/access.log --pid-path=/u01/app/nginx/nginx.pid \
--lock-path=/u01/app/nginx/nginx.lock --http-client-body-temp-path=/u01/app/nginx/tempcache/client_temp \
--http-proxy-temp-path=/u01/app/nginx/tempcache/proxy_temp \
--http-fastcgi-temp-path=/u01/app/nginx/tempcache/fastcgi_temp \
--http-uwsgi-temp-path=/u01/app/nginx/tempcache/uwsgi_temp \
--http-scgi-temp-path=/u01/app/nginx/tempcache/scgi_temp --user=nginx --group=nginx \
--with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module \
--with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module \
--with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module \
--with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module \
--with-file-aio --with-http_spdy_module --with-ipv6 \
--add-module=/u01/soft/nginx_mod_h264_streaming-2.2.7 ###使用add-module添加模块
# make
# /u01/app/nginx/sbin/nginx -s stop
# cp /u01/app/nginx/sbin/nginx /u01/app/nginx/sbin/nginx.bk.0116
### 替换nginx 二进制文件
# cp /u01/soft/nginx-1.8.1/objs/nginx /u01/app/nginx/sbin/nginx
# service nginx start
Starting nginx: [ OK ]
# /u01/app/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/u01/app/nginx
................
--add-module=/u01/soft/nginx_mod_h264_streaming-2.2.7
三、Nginx配置文件摘录(供参考)
### Nginx配置文件
# more /u01/app/nginx/conf/nginx.conf
worker_processes 8;
user nobody;
error_log /u01/app/nginx/logs/error.log;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
log_format main '$remote_addr [$time_local] "$host $request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server_names_hash_bucket_size 128;
client_header_buffer_size 64k;
client_max_body_size 50m;
client_body_buffer_size 512k;
large_client_header_buffers 4 64k;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 5m;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /u01/app/nginx/tempcache/proxy_temp;
proxy_cache_path /u01/app/nginx/tempcache/proxy_cache_dir levels=1:2 keys_zone=cache_one:2000m
inactive=10d max_size=30g;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript
application/xml application/xml+rss;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 2;
open_file_cache_valid 30s;
open_file_cache_errors on;
include conf.d/*.conf;
}
# more /u01/app/nginx/conf/conf.d/stream.conf
server {
listen 9091;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /u02/upload_video;
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;
}
}
###反向代理端的配置
# /opt/nginx/sbin/nginx -v
nginx version: nginx/1.5.13
# more /opt/nginx/conf/conf.d/http-www.conf
upstream stream_srv {
server 10.24.249.21:9091 max_fails=3 fail_timeout=30s;
keepalive 32;
}
server {
listen 80;
server_name www.abc.com;
access_log /log/nginx/http-access.log;
error_log /log/nginx/http-error.log;
location /stream {
proxy_set_header Host $host;
proxy_set_header x-for $remote_addr;
proxy_set_header x-server $host;
proxy_set_header x-agent $http_user_agent;
proxy_pass http://stream_srv;
}
}
###测试
http://www.abc.com/stream/test.mp4
四、Nginx模块动态增加
NGINX 1.9.11开始增加加载动态模块支持,从此不再需要替换nginx文件即可增加第三方扩展。
目前官方只有几个模块支持动态加载,第三方模块需要升级支持才可编译成模块。
# ./configure --help | grep dynamic
--with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module
--with-http_image_filter_module=dynamic enable dynamic ngx_http_image_filter_module
--with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module
--with-mail=dynamic enable dynamic POP3/IMAP4/SMTP proxy module
--with-stream=dynamic enable dynamic TCP proxy module
--add-dynamic-module=PATH enable dynamic external module
如上可看出官方支持5个动态模块编译,需要增加第三方模块,使用参数--add-dynamic-module=即可。
NGINX动态模块语法,此配置方法和Apache httpd大同小异
load_module
Default: —
配置段: main
说明:版本必须>=1.9.11
示例:
load_module modules/ngx_mail_module.so;