RockyLinux 9快速部署Nginx+HTTPS(基于DNF安装)
针对RockyLinux 9用户,通过DNF包管理器快速搭建Nginx服务并配置HTTPS加密,以下是优化后的实战流程,兼顾效率与安全性。
一、1分钟极速安装Nginx
1. 一键安装Nginx及依赖
RockyLinux 9官方仓库已集成稳定版Nginx,无需编译直接安装:
sudo dnf update -y # 更新系统
sudo dnf install -y nginx # 安装Nginx
2. 启动服务并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
3. 验证安装状态
sudo systemctl status nginx # 查看运行状态
curl -I http://localhost # 测试默认页面(应返回200 OK)
二、SSL证书配置(Let’s Encrypt)
1. 安装Certbot工具
通过EPEL仓库获取证书管理工具:
sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-nginx
2. 申请免费SSL证书
替换域名your_domain.com后执行:
sudo certbot --nginx -d your_domain.com # 按提示完成域名验证
证书将自动部署到
/etc/letsencrypt/live/your_domain.com/目录。
三、Nginx反向代理与HTTPS优化
1. 配置AI模型服务代理
编辑站点配置文件
/etc/nginx/conf.d/ai_service.conf:
server {
listen 443 ssl;
server_name your_domain.com;
ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3; # 启用TLS 1.3提升性能
# 反向代理到AI模型服务(假设本地端口8000)
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# HTTP强制跳转HTTPS
server {
listen 80;
server_name your_domain.com;
return 301 https://$host$request_uri;
}
2. 重载配置生效
sudo nginx -t # 语法检查
sudo systemctl reload nginx
四、防火墙与安全加固
1. 开放必要端口
sudo firewall-cmd --permanent --add-service={http,https} # 开放80/443 TCP
sudo firewall-cmd --permanent --add-port=443/udp # 若需HTTP/3需开放UDP 443
sudo firewall-cmd --reload
2. 禁用默认测试页面
sudo rm -rf /usr/share/nginx/html/* # 删除默认静态文件
五、性能调优建议
1. 进程与连接数优化
修改/etc/nginx/nginx.conf主配置:
worker_processes auto; # 自动匹配CPU核心数
events {
worker_connections 4096; # 单进程连接数上限
use epoll; # 高性能事件模型
}
2. 启用Gzip压缩
在http{}块内添加:
gzip on;
gzip_types text/plain application/json;
gzip_min_length 1024; # 超过1KB才压缩
六、关于HTTP/3的说明
当前通过DNF安装的Nginx暂不支持HTTP/3协议。如需启用:
- 方案一:从Nginx官方仓库安装包含QUIC模块的版本(需手动配置)
- 方案二:参考Nginx官方文档编译集成Cloudflare Quiche库
七、验证与监控
# HTTPS服务验证
curl -I https://your_domain.com # 检查SSL协议版本
journalctl -u nginx -f # 实时查看日志
# 性能监控
sudo dnf install -y htop
htop # 查看CPU/内存占用
通过以上步骤,30分钟内即可在RockyLinux 9上完成Nginx+HTTPS的高效部署,为AI模型服务提供安全可靠的前端入口。