繁体   English   中英

尝试连接 GCP sql 数据库实例时 Nginx 中的 502 Bad Gateway 执行

[英]502 Bad Gateway in Nginx when trying to connecting GCP sql instance for database perform

我有一个节点应用程序托管在一个 GCP VM 实例上 Nginx 这是我的 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 /"

我在这条路线下打了 API http://35.244.58.155/api/v1/users/服务器需要执行一个托管在 GCP Postgres SQL 实例上的数据库,我将 VM 实例 IP 地址添加到 881259828 connection615

Authorized networks
You can specify CIDR ranges to allow IP addresses in those ranges to access your instance. Learn more

production (35.244.58.155)
local (118.179.175.117)

此 SQL 实例连接到 node.js 应用程序,并在 env 文件中使用数据库 URL 进行 Prisma 连接

DATABASE_URL="postgresql://postgres:password@34.170.132.86:5432/postgres"

我可以从我的本地机器访问这个数据库。 如果我尝试获取数据,我可以从 postman 获取数据,但使用 GCP URL http://35.244.58.155/api/v1/users/它显示 502 错误错误网关。 但是任何不需要执行数据库的操作都会给我一个响应,并且没有 Nginx 错误。 这是 Nginx 错误日志

2022/12/14 10:03:20 [error] 5650#5650: *201 upstream prematurely closed connection while reading response header from upstream, client: 118.179.175.117, server: _, reque>
2022/12/14 10:03:20 [error] 5650#5650: *201 connect() failed (111: Connection refused) while connecting to upstream, client: 118.179.175.117, server: _, request: "GET /a>
<a HTTP/1.1", upstream: "http://localhost/api/v1/users/gcp-data", host: "35.244.58.155"

我认为这可能是数据库 URL 的错误,Nginx 找不到

我该如何解决? 谢谢

您好,正如您提到的,这与您的 nginx 配置无关。 您收到此错误是因为您的数据库连接失败。 我曾尝试使用类似的连接字符串进行部署,但它不起作用。 因此,我参考了有关 postgres 连接示例的 google 文档,他们提到了如何使用 env 变量和 knex 模块连接到 cloudsql prostgres。 有关knex模块和postgres 连接器的更多信息,请参阅这两个链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM