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"
nginx高性能优化配置
user nginx; #将nginx进程设置为普通用户,为了安全考虑
worker_processes 4;auto;#当前启动的worker进程,官方建议是与系统核心数一致(买的4核8G服务器)
worker_cpu_affinity auto;#方式一,就是自动分配绑定
error_log /var/log/nginx/error.log warn; #日志配置成warn
pid /var/run/nginx.pid;
worker_rlimit_nofile 16384;#针对 nginx 句柄的文件限制--应该至少等于 worker_processes * worker_connections
events {
use epoll;
worker_connections 4096; #每一个进程可以处理多少个连接,如果是多核可以将连接数调高 worker_processes * 1024
multi_accept on; #一个工作进程在一个事件循环中可以接受多少个新的连接请求
}#事件模型
http {
include mime.types;# 包含MIME类型定义文件
default_type application/octet-stream; # 设置默认的MIME类型
charset utf-8; #设置字符集
#设置日志输出格式,根据自己的情况设置
log_format main '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log /var/log/nginx/access.log main;
sendfile on; # 启用 sendfile 系统调用以提高文件传输效率
tcp_nopush on; # 启用 TCP_CORK 选项,减少网络包的数量
tcp_nodelay on; # 启用 TCP_NODELAY 选项,禁用 Nagle 算法,提高响应速度
server_names_hash_bucket_size 128; # 设置 server_names 哈希桶大小为 128 字节
server_names_hash_max_size 512; # 设置 server_names 哈希表最大大小为 512 字节
keepalive_timeout 65; # 设置 HTTP 保持连接的超时时间为 65 秒
client_header_timeout 15M; # 设置读取客户端请求头的超时时间为 15 秒
client_body_timeout 15M; # 设置读取客户端请求体的超时时间为 15 秒
send_timeout 60s; # 设置向客户端发送响应的超时时间为 60 秒
limit_conn_zone $binary_remote_addr zone=perip:10m; # 创建一个基于客户端 IP 地址的连接限制区域,大小为 10MB
limit_conn_zone $server_name zone=perserver:10m; # 创建一个基于服务器名称的连接限制区域,大小为 10MB
limit_conn perip 2; # 每个客户端 IP 地址最多允许 2 个并发连接
limit_conn perserver 20; # 每个服务器名称最多允许 20 个并发连接
limit_rate 300k; # 限制客户端的下载速度为 300KB/s
proxy_cache_path /data/nginx-cache levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h; # 设置代理缓存路径及相关参数
client_body_buffer_size 512k; # 设置读取客户端请求体缓冲区的大小为 512KB
client_header_buffer_size 4k; # 设置读取客户端请求头缓冲区的大小为 4KB
client_max_body_size 1G; # 设置允许客户端请求的最大主体大小为 512KB
large_client_header_buffers 2 8k; # 设置大客户端请求头缓冲区的数量和大小
proxy_connect_timeout 5s; # 设置与后端服务器建立连接的超时时间为 5 秒
proxy_send_timeout 120s; # 设置向后端服务器发送请求的超时时间为 120 秒
proxy_read_timeout 120s; # 设置从后端服务器读取响应的超时时间为 120 秒
proxy_buffer_size 16k; # 设置用于读取后端服务器响应头的缓冲区大小
proxy_buffers 4 64k; # 设置用于读取后端服务器响应体的缓冲区数量和大小
proxy_busy_buffers_size 128k; # 设置用于处理忙碌状态下的缓冲区大小
proxy_temp_file_write_size 128k; # 设置临时文件写入缓冲区的大小
proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header; # 设置在哪些情况下尝试下一个上游服务器
gzip on; # 启用 gzip 压缩
gzip_min_length 1k; # 设置最小压缩文件大小为 1KB
gzip_buffers 4 16k; # 设置 gzip 压缩缓冲区的数量和大小
gzip_http_version 1.1; # 仅对 HTTP/1.1 请求进行 gzip 压缩
gzip_comp_level 4; # 设置 gzip 压缩级别为 4(平衡压缩速度和压缩比)
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; # 指定需要进行 gzip 压缩的 MIME 类型
gzip_vary on; # 在响应头中添加 Vary: Accept-Encoding,提示代理服务器根据请求头缓存不同版本的资源
fastcgi_buffers 64 4K; # 配置 FastCGI 的缓冲区
open_file_cache max=2000 inactive=10m; # 增加最大缓存条目至 2000,非活动时间为 10 分钟
open_file_cache_valid 4m; # 缓存有效性检查间隔增加至 4 分钟
open_file_cache_min_uses 2; # 最小使用次数增加至 2,以保持缓存条目
open_file_cache_errors on; # 缓存错误信息
aio on; # 启用异步 I/O
proxy_set_header Host $host; # 设置 Host 头为请求的主机名,不包括端口
proxy_set_header X-Real-IP $remote_addr; # 将客户端的真实 IP 地址传递给后端服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 用于传递客户端的真实 IP 地址链
proxy_set_header X-Forwarded-Proto $scheme; # 传递请求的协议(HTTP 或 HTTPS)给后端服务器
# 针对*.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/lingyanspace/GeneralFullChainCert.pem;
ssl_certificate_key /etc/nginx/ssl/lingyanspace/GeneralPrivateKey.pem;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
#配置SSL会话缓存
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
#ocsp stapling加速证书访问
ssl_stapling on;
ssl_stapling_verify on;
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;#阿里、腾讯、百度、中国互联网罗DNS、公共、cloudflare
resolver_timeout 5s;
}
#包含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 工作进程的文件描述符限制是否已更改
ps aux | grep nginx | grep worker | awk '{print $2}' | xargs -I {} cat /proc/{}/limits | grep "Max open files"