中文

在 Linux VPS 上配置 Nginx 反向代理

1 min
VPS/VDS 主机

反向代理在 80 和 443 端口接收公网流量,并转发给只在本地监听的应用。它能提供 HTTPS、在同一 IP 上托管多个站点,并集中管理日志与限制。本教程在您的 VPS Linux Infrawire 上为 Node.js、Python 或 Docker 应用完成配置。

📋 前提条件

  • 一台可通过 SSH 以 root 登录的 Linux VPS(Ubuntu 24.04 / Debian 12 或 AlmaLinux 9 / Rocky 9)。IP 和 root 密码在客户中心。
  • 一个域名 example.com,其 A/AAAA 记录指向 VPS_IP。
  • 一个监听本地端口的应用,例如 127.0.0.1:3000。

📦 安装 Nginx

Bash
# Debian / Ubuntu sudo apt update && sudo apt install -y nginx # AlmaLinux / Rocky sudo dnf install -y nginx sudo systemctl enable --now nginx

在 AlmaLinux/Rocky 上,SELinux 默认禁止 Nginx 连接后端。只需允许一次:

Bash
sudo setsebool -P httpd_can_network_connect 1

🚀 第一个被代理的应用

应用必须监听 127.0.0.1 而不是 0.0.0.0,这样只能通过 Nginx 访问。无论使用哪种语言,模式都一样:在 3000 端口运行 node server.js,Python 用 gunicorn -b 127.0.0.1:8000 app:app,或用 docker run -d -p 127.0.0.1:3000:80 IMAGE 发布容器。

创建 /etc/nginx/sites-available/example.com(AlmaLinux/Rocky 使用 /etc/nginx/conf.d/example.com.conf):

Nginx
1server { 2 listen 80; 3 listen [::]:80; 4 server_name example.com; 5 6 location / { 7 proxy_pass http://127.0.0.1:3000; 8 proxy_http_version 1.1; 9 proxy_set_header Host $host; 10 proxy_set_header X-Real-IP $remote_addr; 11 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 12 proxy_set_header X-Forwarded-Proto $scheme; 13 } 14}

在 Debian/Ubuntu 上启用,然后校验并重载:

Bash
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t && sudo systemctl reload nginx

X-Forwarded-* 头让应用知道客户端真实 IP 以及访客是否使用了 HTTPS。没有它们,应用只能看到 127.0.0.1,还可能生成 http:// 链接。许多框架还需要明确信任代理(例如 Express 中的 app.set('trust proxy', 1))。

🔌 WebSocket 支持

WebSocket 需要 HTTP 升级头。在 http 上下文顶部添加一个 map(例如放在 /etc/nginx/conf.d/websocket.conf):

Nginx
map $http_upgrade $connection_upgrade { default upgrade; '' close; }

然后在 location 块中加入:

Nginx
proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade;

⏱️ 超时与请求体大小

默认值是 60 秒和 1 MB。针对慢接口或文件上传进行调整:

Nginx
proxy_connect_timeout 5s; proxy_read_timeout 120s; proxy_send_timeout 120s; client_max_body_size 50m;

对于长连接的 WebSocket,请调大 proxy_read_timeout(例如 3600s),或让应用定期发送 ping。

🔒 使用 Certbot 启用 HTTPS

先在防火墙中放行 80 和 443 端口,然后:

Bash
# Debian / Ubuntu sudo apt install -y certbot python3-certbot-nginx sudo certbot --nginx -d example.com

在 AlmaLinux/Rocky 上,先执行 sudo dnf install -y epel-release,再执行 sudo dnf install -y certbot python3-certbot-nginx 安装 Certbot。Certbot 会把证书写入您的 server 块,提供 HTTP 到 HTTPS 的跳转,并安装自动续期。用下面的命令验证:

Bash
sudo certbot renew --dry-run

🗂️ 一台 VPS 上运行多个应用

每个域名创建一个 server 块,各自使用独立的 server_name 和本地端口:

Nginx
1server { 2 listen 80; 3 server_name app1.example.com; 4 location / { proxy_pass http://127.0.0.1:3000; } 5} 6 7server { 8 listen 80; 9 server_name app2.example.com; 10 location / { proxy_pass http://127.0.0.1:8000; } 11}

在每个块中重复代理头设置,然后运行 sudo certbot --nginx -d app1.example.com -d app2.example.com。也可以按路径分流(location /api/ { proxy_pass http://127.0.0.1:8000/; });末尾的斜杠会去掉前缀。

✅ 测试

Bash
curl -I https://example.com ss -ltnp | grep -E ':(80|443|3000)' sudo tail -f /var/log/nginx/error.log

后端端口应只绑定到 127.0.0.1,而 Nginx 监听 80 和 443。

🛠️ 故障排查

502 Bad Gateway。 应用已停止或在别的端口。用 ss -ltnp 检查并启动它。在 AlmaLinux/Rocky 上,错误日志中出现 Permission denied 说明缺少上面的 SELinux 布尔值。

504 Gateway Timeout。 后端耗时超过 proxy_read_timeout。调大该值或优化接口。

WebSocket 断开或返回 400/426。 缺少 Upgrade 和 Connection 头,或没有 proxy_http_version 1.1;。

日志中的客户端 IP 不对。 X-Forwarded-For 已发送,但应用不信任代理。请启用框架的信任代理设置。


🎉 您的应用现在运行在 Nginx 之后,具备 HTTPS、规范的请求头,并可容纳 VPS 能承载的任意多个站点。

及时了解最新的优惠、折扣和特别促销活动。

查看促销

我们使用Cookie

我们使用分析性Cookie来改善您在我们网站上的体验。数据不会出售给第三方。了解更多请查看我们的 隐私政策.