Nginx是一個短小精悍,且效能超強的Web伺服器,同時也可以用作反向 Proxy伺服器以及郵件伺服器。除此之外Nginx也可以用於搭建一個簡易的流媒體伺服器。基於此,本文描述了在Nginx 1.8之上構建一個簡易的流媒體伺服器,同時通過反向 Proxy的方式將其代理至流媒體伺服器。
有關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 -vnginx version: nginx/1.8.12、安裝檔案下載http://download.csdn.net/detail/robinson_0612/97389483、安裝檔案位置# pwd/u01/soft# lsyamdi-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/bin2、安裝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] 錯誤 1make[1]: Leaving directory `/usr/local/src/nginx-1.8.1'make: *** [build] 錯誤 23、查看nginx當前已編譯的模組以及添加模組# /u01/app/nginx/sbin/nginx -Vnginx version: nginx/1.8.1built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure 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 startStarting nginx: [ OK ]# /u01/app/nginx/sbin/nginx -Vnginx version: nginx/1.8.1built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure 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; }}###反向 Proxy端的配置# /opt/nginx/sbin/nginx -vnginx version: nginx/1.5.13# more /opt/nginx/conf/conf.d/http-www.confupstream 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;