连接GCP SQL实例时Nginx出现502 Bad Gateway问题求助
问题描述
将Node.js应用部署在搭载Nginx的GCP VM实例上,无需数据库操作的接口可正常响应,但访问需要连接GCP Postgres SQL实例的API路由时,出现502 Bad Gateway错误。
环境配置信息
Nginx 配置
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
PM2 应用日志
/root/.pm2/logs/edvive-out.log last 15 lines: 0|edvive | 14/Dec/2022:09:50:22 +0000 "GET /api/v1/users/gcp-data" 0|edvive | 14/Dec/2022:09:50:23 +0000 "GET /api/v1/users/gcp-data" 0|edvive | user api hitted 0|edvive | The server is running at port: 5000 0|edvive | user api hitted 0|edvive | The server is running at port: 5000 0|edvive | 14/Dec/2022:09:51:01 +0000 "GET /api/v1/users/gcp-data" 0|edvive | 14/Dec/2022:09:51:30 +0000 "GET /api/v1/users/gcp-data" 0|edvive | user api hitted 0|edvive | The server is running at port: 5000 0|edvive | user api hitted 0|edvive | The server is running at port: 5000 0|edvive | user api hitted 0|edvive | The server is running at port: 5000 0|edvive | 14/Dec/2022:10:04:13 +0000 "GET /"
GCP Postgres 授权网络
Authorized networks You can specify CIDR ranges to allow IP addresses in those ranges to access your instance. Learn more production (35.***.**.***) local (118.***.***.**)
Node.js 数据库连接配置
DATABASE_URL="postgresql://postgres:password@34.***.***.***:5432/postgres"
Nginx 错误日志
2022/12/14 10:03:20 [error] 5650#5650: *201 upstream prematurely closed connection while reading response header from upstream, client: 118.***.***.***, server: _, request: "GET /api/v1/users/gcp-data HTTP/1.1", upstream: "http://localhost:5000/api/v1/users/gcp-data", host: "35.***.**.***" 2022/12/14 10:03:20 [error] 5650#5650: *201 connect() failed (111: Connection refused) while connecting to upstream, client: 118.***.***.**, server: _, request: "GET /api/v1/users/gcp-data HTTP/1.1", upstream: "http://localhost/api/v1/users/gcp-data", host: "35.***.**.***"
排查与解决步骤
1. 验证VM到Postgres的网络连通性
虽然VM IP已加入授权网络,仍需在VM内部直接测试连接:
- 执行telnet检查端口连通:
telnet 34.***.***.*** 5432 - 或用psql直接测试数据库连接:
psql postgresql://postgres:password@34.***.***.***:5432/postgres
如果连接失败:
- 确认GCP VM防火墙规则未拦截出站5432端口
- 检查Postgres授权网络中添加的是VM的公网IP(公网连接场景)或私网IP(VPC内网连接场景),避免混淆
- 若Postgres用私有IP,确保VM与Postgres在同一VPC,或配置了VPC peering
2. 查看Node.js应用的数据库错误日志
PM2的out.log仅记录请求到达,需查看应用错误日志定位数据库连接问题:
cat /root/.pm2/logs/edvive-error.log
若日志出现ECONNREFUSED、连接超时等报错:
- 核对
DATABASE_URL中的Postgres IP、端口、密码(注意密码特殊字符需转义) - 重新生成Prisma客户端:
npx prisma generate - 重启PM2应用确保环境变量生效:
pm2 restart edvive
3. 修复Nginx配置潜在问题
从错误日志看存在上游地址缺失端口的异常,需确保Nginx配置生效:
- 检查配置语法:
sudo nginx -t - 重载配置:
sudo nginx -s reload
同时建议添加超时配置,避免数据库查询超时导致502:
location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; # 添加超时配置 proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; }
4. 验证应用本地访问
在VM内部直接调用API,确认问题是否出在应用本身:
curl http://localhost:5000/api/v1/users/gcp-data
若curl返回错误或超时,说明问题核心在应用与数据库的连接,而非Nginx反向代理,需重点排查应用数据库逻辑和网络连通性。
内容的提问来源于stack exchange,提问作者Md Firoz Mia
相关产品推荐
相关产品推荐

