先安装依赖:(PCRE 作用是让 Nginx 支持 Rewrite 功能)
yum -y install make zlib zlib-devel gcc gcc-c++ libtool openssl openssl-devel pcre pcre-devel
下载源码包:
cd /opt
wget http://nginx.org/download/nginx-1.9.9.tar.gz
解压:tar -zxvf nginx-1.9.9.tar.gz
cd nginx-1.9.9
配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module
安装:
make
覆盖安装命令:make install
添加模块或者删除模块时 make完成后需要替换nginx文件:
cp objs/nginx /usr/local/nginx/sbin/nginx
vi /etc/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
开启:systemctl start nginx
刷新:systemctl reload nginx
停止:systemctl stop nginx
开机启:systemctl enable nginx
取消开机自启: systemctl disable nginx
附:
./nginx -s reload 刷新nginx
./nginx -s quit 优雅停止nginx
./nginx -s stop 快速停止nginx
./nginx -s reopen 重启日志
./nginx -t 校验配置文件是否正确
配置Nginx(http访问)
vi /usr/local/nginx/conf/nginx.conf
server {
client_max_body_size 10m; # 客户端上传文件大小限制
client_body_buffer_size 512k;
client_header_buffer_size 2k;
limit_rate_after 1m;# 文件下载大小限制
limit_rate 50k;
limit_conn_zone $binary_remote_addr zone=perip:500m; # 限制连接数
limit_conn perip 10; # 单个IP限制连接数
listen 80;
server_name localhost;
gzip on;
# 需要http_gzip_static_module 模块
gzip_static on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/xml text/css;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# 前端打包好的dist目录文件
root /opt/xxxx/;
# 仿gateway网关
location ~* ^/(code|auth|admin|monitor|gen|job|tx|act|mp|pay|xxx) {
client_max_body_size 10m;
proxy_pass http://bear-gateway:9998;
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
}
location / {
alias /opt/xxx/h5/;
index index.html index.htm;
}
location /xxx{
alias /opt/xxx/;
index index.html index.htm;
}
location /images/ {
alias /opt/xxxxxxx/images/;
autoindex on;
}
}
配置SSL(https访问)
server{
#监听80和443端口
listen 80;
listen 443 ssl http2;
#对应的域名
server_name blog.johngene.cn;
#SSL-START SSL相关配置
#HTTP_TO_HTTPS_START
#强制使用https
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
#注释掉ssl on; 是为了让此server同时处理http和https
#ssl on;
#获取到的第一个文件的全路径
ssl_certificate "/etc/ssl/xxxxxxx.pem";
#获取到的第二个文件的全路径
ssl_certificate_key "/etc/ssl/xxxxxxx.key";
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
#SSL-END
#主页访问地址
location / {
alias /opt/pigx/bear-h5/;
index index.html index.htm;
}
#管理平台访问地址
location /web {
alias /opt/pigx/bear-ui/;
index index.html index.htm;
}
#图片访问地址
location /images/ {
#valid_referers none blocked blog.johngene.cn;
#加入防盗功能,需去掉none blocked
valid_referers blog.johngene.cn;
#注意:if 和 ( 中间必须有空格,别写错了
if ($invalid_referer){
#return 403;
rewrite ^/ https://blog.johngene.cn/404.png;
}
alias /opt/pigx/bear/bear-appmanager/images/;
autoindex on;
}
#404默认图片
location ~ .*\.(png)$ {
root /opt/pigx/;
index 404.png;
}
#GZIP-START
gzip on;
# 需要http_gzip_static_module 模块
gzip_static on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_proxied any;
gzip_types text/plain text/xml text/css;
gzip_vary on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#GZIP-END
# 前端打包好的dist目录文件
root /opt/pigx/;
# 模仿pig网关
location ~* ^/(code|auth|admin|monitor|gen|job|tx|act|mp|pay|xxx) {
proxy_pass http://bear-gateway:9998;
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
}
}
配置流媒体nginx-vod-module(三方模块 )
下载地址:https://github.com/kaltura/nginx-vod-module/archive/refs/tags/1.28.tar.gz
传到服务器后解压:
tar -zxvf nginx-vod-module-1.28.tar.gz
重新配置:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module--with-file-aio --with-threads --with-cc-opt="-O3" --add-module=../nginx-vod-module-1.28
--with-file-aio
- 启用异步I / O支持,强烈建议,仅与本地和映射模式相关
--with-threads
(nginx 1.7.11+) - 使用线程池(也需要vod_open_file_thread_pool
在nginx.conf中)启用异步文件,仅与本地和映射模式相关
--with-cc-opt="-O3"
- 启用额外的编译器优化(与nginx默认值相比,mp4解析时间和帧处理时间减少了大约8%-
覆盖安装:
make insall
拷贝覆盖:
cp objs/nginx /usr/local/nginx/sbin/nginx
配置server:
location ^~/video {
vod hls; # 协议使用hls模式
vod_mode local; # 访问模式指定为local模式
vod_align_segments_to_key_frames on; # 每个切片以关键帧开头
vod_manifest_segment_durations_mode accurate; # 精确显示每个切片的长度
# 解决浏览器跨域问题
add_header Access-Control-Allow-Headers '*';
add_header Access-Control-Expose-Headers 'Server,range,Content-Length,Content-Range';
add_header Access-Control-Allow-Methods 'GET, HEAD, OPTIONS';
add_header Access-Control-Allow-Origin '*';alias /home/media; # 视频文件路径
}
上传mp4文件到/home/media文件夹下。
播放地址: http://ip地址/video/xxx.mp4/index.m3u8
浏览器播放需要插件,谷歌浏览器为例:Play HLS M3u8
或者用VLC media player:https://www.videolan.org/vlc/
更多信息参考:使用nginx搭建音视频点播服务——基于HLS协议_SeeDoubleU的博客-优快云博客_nginx 点播