安装完Nginx服务器,需要修改其中的配置文件。以下就是我最终版的nginx.conf。大家可以参考。
user www www; #用户www 用户组www
worker_processes auto; #多进程 error_log /data/logs/nginx/nginx_error.log crit; #错误日志 pid /data/logs/nginx.pid; #里面存放的是当前nginx主进程的ID号,启动 nginx 时自动生成的 events { use epoll; # 高效的方法,使用于Linux内核2.6版本及以后的系统。 worker_connections 51200; #默认最大并发数 multi_accept on; #多事件 } rtmp { server { listen 1935; #给NGINX添加一个监听端口以接受RTMP链接 chunk_size 4000; ping 10s; ping_timeout 5s; application myapp{ live on; #rocord模块是将rtmp流录制保存为FLV文件 #record all; #record_path /data/dl/record; # record_unique on; #} #exec 将myapp中的流转发至hls,从而切片。推流地址:ip/myapp exec_push /usr/local/ffmpeg/bin/ffmpeg -i rtmp://localhost/myapp/$name -crf 32 -preset superfast -acodec libfaac -ar 24000 -b:a 96k -vcodec libx264 -r 25 -b:v 256k -f flv rtmp://localhost/hls/$name; #-c:v libx264 -c:a libfaac -b:v 512k -b:a 128k -f flv rtmp://127.0.0.1/hls/$name_hi #-c:v libx264 -c:a libfaac -b:v 256k -b:a 64k -f flv rtmp://127.0.0.1/hls/$name_mid #-c:v libx264 -c:a libfaac -b:v 128k -b:a 32k -f flv rtmp://127.0.0.1/hls/$name_low; } #hls切片设置,具体解释看hls文档 application hls{ live on; hls on; hls_path /data/www/test/hls ; #切片存放目录 hls_cleanup off; #HLS清理,默认开启.在这一模式下 nginx 缓存管理进程将老的 HLS 片段和播放列表由 HLS 清理掉 hls_nested on; #为每一个流分配一个文件夹 hls_fragment 10s; #每一个切片的切片大小 hls_playlist_length 120s; #m3u8播放列表的长度 hls_fragment_naming system; #以系统时间戳方式命名 } } #log_format new '$connection $remote_addr $app-$name $args $flashver $swfurl $tcurl $pageurl $command $bytes_sent $bytes_received $time_local $session_time $session_readable_time'; #access_log logs/rtmp_access.log new; #access_log logs/rtmp_access.log; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; #limit_conn_zone $binary_remote_addr zone=perip:10m; ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; #log format log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; server { listen 80 default; #listen [::]:80 ipv6only=on; server_name 119.80.56.103; index index.html index.html index.php; #加载页面位置 root /data/www; error_log /data/logs/nginx/nginx_error.log; #错误日志位置 error_page 404 /404.html; #404位置 #charset koi8-r; #access_log logs/host.access.log main; location ~ [^/]\.php(/|$) { # comment try_files $uri =404; to enable pathinfo try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; #include pathinfo.conf; } #rtmp统计模块,访问url:119.80.56.103/stat location /stat{ rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl{ root /data/www/; } location /control { rtmp_control all; } #hls模块 location /hls { types{ application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias /data/www/test/hls; expires -1; add_header Cache-Control no-cache; } location ~ \.flv{ flv; } location ~ \.mp4$ { mp4; } location /status { stub_status on; access_log off; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location / { rewrite "^/monitor" /api/moudle/datas/result/show.php; } access_log /data/logs/nginx/access.log access; } include vhost/*.conf; }