Nginx篇
//预安装
yum install prel
yum install pcre cpre-devel
sudo yum install libxml2 libxml2-devel libxslt libxslt-devel
sudo yum install gd gd-devel
//查看功能模块配置
nginx -V
//清理之前的编译结果
make clean
//检查系统的编译器、库和其他依赖是否存在,并配置编译选项
./configure \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-compat \
--with-debug \
--with-file-aio \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_degradation_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_mp4_module \
--with-http_perl_module=dynamic \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-http_xslt_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-pcre \
--with-pcre-jit \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream \
--with-threads \
--with-cc-opt='-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' \
--with-ld-opt='-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,-E' \
--add-module=/home/nginx-http-flv-module-master \
--add-module=/home/nginx-vod-module-master
//使用生成的Makefile来编译源代码。它会调用编译器和链接器,生成可执行文件和库
make
//编译安装
make install
生成dhparam.pem
文件
//当Nginx服务器与客户端建立SSL/TLS连接时,会使用DH参数进行密钥交换。
//这个过程允许双方在不安全的网络环境下协商出一个共享的密钥,而不需要直接传输密钥本身,从而保证了密钥的安全性。
//例如,假设一个Web服务器(使用Nginx)要与一个Web浏览器建立安全连接。
//在SSL/TLS握手阶段,服务器和客户端会根据ssl_dhparam指定的参数文件中的DH参数进行复杂的数学运算,以生成共享的加密密钥。
//这个共享密钥随后将用于加密和解密在连接中传输的数据
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
nginx通用前置配置
http {
# 包含MIME类型定义文件
include mime.types;
# 设置默认的MIME类型
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功能,提高文件传输性能
sendfile on;
# 设置客户端连接的keep - alive超时时间
keepalive_timeout 65;
# 设置默认的索引文件
index index.html index.htm;
# 设置客户端请求体的最大大小
client_max_body_size 1G;
# 配置FastCGI的缓冲区,使用64个4KB的缓冲区来缓存FastCGI的响应
fastcgi_buffers 64 4K;
# 设置客户端请求头的超时时间
client_header_timeout 15m;
# 设置客户端请求体的超时时间
client_body_timeout 15m;
# 设置代理连接的超时时间
proxy_connect_timeout 15s;
# 设置代理读取的超时时间
proxy_read_timeout 30m;
# 设置代理发送的超时时间
proxy_send_timeout 30m;
# 增加最大缓存条目至 2000,非活动时间为 10 分钟
open_file_cache max=2000 inactive=10m;
# 缓存有效性检查间隔增加至 4 分钟
open_file_cache_valid 4m;
# 最小使用次数增加至 2,以保持缓存条目
open_file_cache_min_uses 2;
# 缓存错误信息
open_file_cache_errors on;
# 启用异步 I/O
aio on;
#设置 Host 头为请求的主机名,不包括端口
proxy_set_header Host $host;
#将客户端的真实IP地址传递给后端服务器
proxy_set_header X-Real-IP $remote_addr;
#用于传递客户端的真实IP地址链,这对于需要追踪请求来源的后端服务尤其重要
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#传递请求的协议(HTTP或HTTPS)给后端服务器
proxy_set_header X-Forwarded-Proto $scheme;
# 针对*.lingyanspace.com的HTTP配置
server {
listen 80;
listen [::]:80;
#请填写绑定证书的域名
server_name *.lingyanspace.com;
#把http的域名请求转成https
return 301 https://$host$request_uri;
}
# 针对*.lingyanspace.com的HTTPS配置
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name *.lingyanspace.com;
# SSL证书相关配置
ssl_certificate /etc/nginx/ssl/letsencrypt_cert/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/letsencrypt_cert/certkey.pem;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
# 配置SSL会话缓存
ssl_session_cache shared:SSL:10m;
# 配置SSL会话超时时间
ssl_session_timeout 10m;
# 配置SSL协议版本
ssl_protocols TLSv1.2 TLSv1.3;
# 配置SSL加密算法
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# 关闭访问日志(可根据需求修改)
# access_log off;
# 此处可根据具体需求添加更多配置,如网站根目录、反向代理等
}
# 包含conf.d目录下的所有.http.conf文件
include conf.d/*.http.conf;
}
stream {
include conf.d/*.tcp.conf;
}
rtmp {
#log 模块在 access.log 中记录日志的间隔时间,对调试非常有用
log_interval 5s;
#log 模块用来记录日志的缓冲区大小
log_size 1m;
include conf.d/*.rtmp.conf;
}
nginx-http-flv-module模块
#播放流http块的server块配置
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name rtmp.lingyanspace.com;
location /live {
flv_live on;
#支持'Transfer-Encoding: chunked'方式回复
chunked_transfer_encoding on;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
}
location /vod {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/lingyanassets/vods;
expires -1;
}
# control控制器
location /control {
rtmp_control all;
}
location /stat {
#推流播放和录制统计数据的配置
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /home/nginx-http-flv-module-master/;
}
}
#rtmp块的server配置
server {
listen 1935;
chunk_size 4096;
application live {
# 启用直播功能
live on;
# 允许所有用户发布流
allow publish all;
# 允许所有用户播放流
allow play all;
# 禁用录制功能
record off;
# 启用HLS
hls on;
# HLS切片存储路径
hls_path /home/lingyanassets/lives;
# 每个切片时长为10秒
hls_fragment 10s;
# 播放列表时长为60分钟
hls_playlist_length 60m;
# 启用连续切片
hls_continuous on;
# 禁用自动清除切片
hls_cleanup off;
}
# 第三个应用程序 'video'
application vod {
play /home/lingyanassets/vods; # 视频文件存储路径
}
}
nginx-vod-module-master模块
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name hls.lingyanspace.com;
# 增加元数据缓存大小至 512MB
vod_metadata_cache metadata_cache 512m;
# 增加响应缓存大小至 256MB
vod_response_cache response_cache 256m;
# VOD 模式和分段设置
vod_mode local;
# 分段持续时间 2 秒
vod_segment_duration 2000;
# 对齐分段到关键帧
vod_align_segments_to_key_frames on;
#课程权限文件夹
location /LimitVideo/ {
auth_request /test_auth;
alias /home/lingyanspace/LimitVideo/;
vod hls;
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 '*';
expires 100d;
}
location =/test_auth {
proxy_pass http://127.0.0.1:7777/LimitVideo;
proxy_pass_request_body off;
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-Method $request_method;
proxy_set_header X-Forwarded-Proto $scheme;
}
access_log /var/log/nginx/hls.lingyanspace.com_access.log;
error_log /var/log/nginx/hls.lingyanspace.com_error.log;
}
nginx守护进程
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
nginx配置signalr等长连接
location ~* /*(lingyanchat)$ {
proxy_pass http://lingyanasp;
#设置Host头为请求的主机名和端口
proxy_set_header Host $host:$server_port;
#指定与后端服务器通信时使用HTTP/1.1版本,如持久连接和分块传输编码
proxy_http_version 1.1;
#这些头部用于支持WebSocket和HTTP/2等协议的升级,允许HTTP连接从普通的HTTP请求升级到WebSocket通信
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
ocsp证书加速访问优化
#ocsp stapling加速证书访问
ssl_stapling on;
ssl_stapling_verify on;
#阿里、腾讯、百度、中国互联网罗DNS、公共、cloudflare
resolver 223.5.5.5 223.6.6.6 119.29.29.29 119.28.28.28 180.76.76.76 1.2.4.8 114.114.114.114 1.1.1.1 1.0.0.1 valid=300s;
resolver_timeout 5s;
#验证
openssl s_client -connect lingyanspace.com:443 -servername lingyanspace.com -status -tlsextdebug < /dev/null 2>&1 | grep -i "OCSP response"
GZIP
gzip on;
#表示开启压缩功能
gzip_min_length 1k;
#表示允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,表示不管页面多大都进行压缩,建议设置成大于1K。如果小于1K可能会越压越大
gzip_buffers 4 32k;
#压缩缓存区大小
gzip_http_version 1.1;
#压缩版本
gzip_comp_level 6;
#压缩比率, 一般选择4-6,为了性能gzip_types text/css text/xml application/javascript;
#指定压缩的类型 gzip_vary on; #vary header支持
nginx高性能优化配置
#将nginx进程设置为普通用户,为了安全考虑
user nginx;
#当前启动的worker进程,官方建议是与系统核心数一致
worker_processes auto;
#方式一,就是自动分配绑定
worker_cpu_affinity auto;
#日志配置成warn
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;