Nginx 反向代理
各发行版中的版本
Section titled “各发行版中的版本”安装 Nginx
Section titled “安装 Nginx”sudo dnf install -y nginx创建 /etc/yum.repos.d/nginx.repo:
[nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true然后安装:
sudo dnf install -y nginx启动并设置开机自启:
sudo systemctl enable --now nginx验证:
sudo systemctl status nginxcurl -I http://localhost开放防火墙端口:
sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reload配置文件结构
Section titled “配置文件结构”Nginx 在 EL 系统上的配置目录结构如下:
/etc/nginx/├── nginx.conf # 主配置文件├── conf.d/ # 站点配置目录(*.conf 自动加载)│ └── default.conf # 默认站点配置├── mime.types # MIME 类型定义└── fastcgi_params # FastCGI 参数主配置文件 /etc/nginx/nginx.conf 的核心结构:
user nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;
events { worker_connections 1024;}
http { include /etc/nginx/mime.types; 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 /var/log/nginx/access.log main;
sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on;
include /etc/nginx/conf.d/*.conf;}Server Block(站点配置)
Section titled “Server Block(站点配置)”每个站点配置为独立文件放在 /etc/nginx/conf.d/ 目录下。
静态文件站点
Section titled “静态文件站点”创建 /etc/nginx/conf.d/static.example.com.conf:
server { listen 80; server_name static.example.com;
root /var/www/static.example.com; index index.html;
# 静态资源缓存 location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff2?)$ { expires 30d; add_header Cache-Control "public, immutable"; }
# 禁止访问隐藏文件 location ~ /\. { deny all; }
error_page 404 /404.html;}创建网站根目录:
sudo mkdir -p /var/www/static.example.comecho "<h1>Hello from RunEntLinux</h1>" | sudo tee /var/www/static.example.com/index.html测试并重载配置:
sudo nginx -tsudo systemctl reload nginx反向代理配置
Section titled “反向代理配置”基本反向代理
Section titled “基本反向代理”创建 /etc/nginx/conf.d/app.example.com.conf:
server { listen 80; server_name app.example.com;
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; proxy_set_header X-Forwarded-Proto $scheme; }}带负载均衡的反向代理
Section titled “带负载均衡的反向代理”upstream app_backend { least_conn; server 127.0.0.1:8001; server 127.0.0.1:8002; server 127.0.0.1:8003;}
server { listen 80; server_name app.example.com;
location / { proxy_pass http://app_backend; 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;
# 超时设置 proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; }}WebSocket 反向代理
Section titled “WebSocket 反向代理”server { listen 80; server_name ws.example.com;
location /ws { proxy_pass http://127.0.0.1:8000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_read_timeout 86400; }}SSL/TLS 配置(Let’s Encrypt + Certbot)
Section titled “SSL/TLS 配置(Let’s Encrypt + Certbot)”-
安装 Certbot(来自 EPEL,需先启用 EPEL;最小化安装直接装会
No match):Terminal window sudo dnf install -y epel-releasesudo dnf install -y certbot python3-certbot-nginx -
申请证书(Certbot 会自动修改 Nginx 配置):
Terminal window sudo certbot --nginx -d app.example.com或者仅获取证书,不修改配置:
Terminal window sudo certbot certonly --nginx -d app.example.com -
手动配置 SSL 站点。创建
/etc/nginx/conf.d/app.example.com.conf:server {listen 80;server_name app.example.com;return 301 https://$host$request_uri;}server {# nginx 1.25.1 起 listen 的 http2 参数已弃用,改用独立的 http2 指令listen 443 ssl;http2 on;server_name app.example.com;ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;# 安全的 SSL 参数ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;ssl_prefer_server_ciphers off;# HSTSadd_header Strict-Transport-Security "max-age=63072000" always;# OCSP Staplingssl_stapling on;ssl_stapling_verify on;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;proxy_set_header X-Forwarded-Proto $scheme;}} -
设置证书自动续期:
Terminal window sudo systemctl enable --now certbot-renew.timer# 或手动测试续期sudo certbot renew --dry-run
SELinux 相关配置
Section titled “SELinux 相关配置”在启用 SELinux 的系统上,Nginx 受到 httpd_t SELinux 策略的约束。
常见问题与解决
Section titled “常见问题与解决”Nginx 无法读取自定义目录下的文件
# 查看当前 SELinux 上下文ls -laZ /var/www/static.example.com/
# 设置正确的上下文sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/static.example.com(/.*)?"sudo restorecon -Rv /var/www/static.example.com/Nginx 无法连接后端服务(反向代理 502 错误)
# 允许 httpd 发起网络连接sudo setsebool -P httpd_can_network_connect 1Nginx 无法连接特定端口的后端
# 查看 httpd 允许连接的端口sudo semanage port -l | grep http_port
# 添加自定义端口sudo semanage port -a -t http_port_t -p tcp 8000Nginx 无法绑定非标准端口
# 允许 Nginx 监听自定义端口sudo semanage port -a -t http_port_t -p tcp 8443常用运维命令
Section titled “常用运维命令”# 测试配置语法sudo nginx -t
# 重载配置(不中断服务)sudo systemctl reload nginx
# 查看活跃连接状态(需启用 stub_status 模块)# 在某个 server block 中添加:# location /nginx_status {# stub_status on;# allow 127.0.0.1;# deny all;# }curl http://127.0.0.1/nginx_status
# 查看错误日志sudo tail -f /var/log/nginx/error.log
# 查看访问日志sudo tail -f /var/log/nginx/access.logEL 10 注意事项
Section titled “EL 10 注意事项”EL 9 与 EL 10 的 nginx 打包方式不同,不能只看一个版本号:
| EL 9 | EL 10 | |
|---|---|---|
| 打包方式 | AppStream 模块流(nginx:1.20/1.22/1.24/1.26) | 模块化已弃用,普通包 |
| 默认装到的版本 | 随小版本变化:Stream 9 基础仓库是 1.20.1;RHEL/Alma 9.8 已是 1.26.3 | 1.26.3 |
| 固定版本 | dnf module list nginx 看可用流,dnf module enable nginx:<ver> 后再装 | 直接装(仓库里是哪个就是哪个) |
# EL 9:先确认当前默认流与可用流sudo dnf module list nginx# 需要特定流时(示例:1.26)sudo dnf module enable nginx:1.26 -ysudo dnf install -y nginx
# EL 10:没有模块流,直接安装sudo dnf install -y nginx
nginx -v # 两代都用它确认实际版本上表的「默认版本」是 2026-10 的快照(AlmaLinux 9.8 = 1.26.3、CentOS Stream 9 基础仓库 = 1.20.1、EL 10 = 1.26.3);升级小版本后请以
dnf module list/nginx -v的实测为准。
如需固定版本,参考软件包版本锁定。