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:
Terminal window sudo 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 {listen 443 ssl http2;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 10 AppStream 仓库中提供的 Nginx 版本已更新:
| 版本 | EL 9 系统仓库 | EL 10 系统仓库 |
|---|---|---|
| Nginx | 1.22.x | 1.26.x |
安装命令完全相同,EL 10 会自动安装对应版本:
sudo dnf install -y nginxnginx -v # 确认版本如需固定版本,参考软件包版本锁定。