如何在CentOS 9上安装和配置Nginx?
美国、香港服务器
如何在CentOS 9上安装和配置Nginx?
06-18 来源:
一、两种安装方式(推荐官方稳定源,版本最新安全)
方式 1:官方 Nginx 源安装(推荐生产使用)
1. 添加官方 Nginx YUM 源
bash
运行
tee /etc/yum.repos.d/nginx.repo << "EOF"
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
2. 安装 Nginx
bash
运行
dnf clean all && dnf makecache
dnf install -y nginx
方式 2:系统自带 EPEL 源极简安装(版本略旧,新手快速用)
bash
运行
dnf install epel-release -y
dnf install nginx -y
二、启动服务 + 设置开机自启
bash
运行
# 立即启动+开机自启
systemctl enable --now nginx
# 常用管理命令
systemctl status nginx # 查看运行状态
systemctl stop nginx # 停止
systemctl restart nginx # 重启
nginx -t # 校验配置语法
systemctl reload nginx # 平滑重载配置(不中断业务)
三、放行防火墙 firewalld(80/443 端口必须开放)
bash
运行
# 永久放行HTTP、HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
# 重载生效
firewall-cmd --reload
# 查看放行规则
firewall-cmd --list-services
四、SELinux 权限放行(CentOS9 默认开启,反向代理必配置)
1. 开启 Nginx 反向代理网络权限
bash
运行
setsebool -P httpd_can_network_connect on
2. 静态网站目录权限(可选)
bash
运行
chcon -Rt httpd_sys_content_t /你的网站目录
五、验证安装
浏览器访问 服务器公网IP,出现 Nginx 欢迎页即安装成功。
Nginx默认欢迎页
六、Nginx 核心目录说明
plaintext
# 主配置文件
/etc/nginx/nginx.conf
# 站点虚拟主机配置目录(推荐所有站点放这里)
/etc/nginx/conf.d/
# 默认网页根目录
/usr/share/nginx/html
# 日志目录
/var/log/nginx/
# 可执行程序
/usr/sbin/nginx
七、实战配置 1:HTTP 反向代理(大模型中转 3000 端口示例)
新建站点配置文件:
bash
运行
vi /etc/nginx/conf.d/api.conf
写入配置:
nginx
server {
listen 80;
server_name api.你的域名.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
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 $scheme;
# 大模型流式WebSocket必须配置
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 长连接超时
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
}
}
校验 + 重载:
bash
运行
nginx -t
systemctl reload nginx
八、实战配置 2:HTTPS 反向代理(强制 HTTP 跳转 HTTPS,生产推荐)
1. 安装证书工具 certbot(自动申请免费 SSL 证书)
bash
运行
dnf install certbot python3-certbot-nginx -y
2. 一键申请证书并自动配置 Nginx
bash
运行
certbot --nginx -d api.你的域名.com --redirect -m 你的邮箱@xxx.com --agree-tos -n
证书会自动部署,自动配置 80 跳转 443,90 天自动续期。
3. 手动 HTTPS + 反向代理完整模板(适配 API 流式接口)
nginx
# 80强制跳转HTTPS
server {
listen 80;
server_name api.你的域名.com;
return 301 https://$host$request_uri;
}
# HTTPS反向代理配置
server {
listen 443 ssl http2;
server_name api.你的域名.com;
# SSL证书路径(certbot自动生成)
ssl_certificate /etc/letsencrypt/live/api.你的域名.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.你的域名.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# 安全头部加固
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options DENY;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
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 $scheme;
# WebSocket流式支持
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
}
重载生效:
bash
运行
nginx -t && systemctl reload nginx
九、安全优化补充(API 中转服务器必做)
请求限流防 CC 攻击
在nginx.conf的http{}块添加:
nginx
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=20r/s;
在站点location内添加:
nginx
limit_req zone=api_limit burst=50 nodelay;
隐藏 Nginx 版本号
nginx.conf内设置:
nginx
server_tokens off;
定期自动续期 SSL 证书
bash
运行
# 测试续期
certbot renew --dry-run
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快