[英]Google App Engine Produces 502 Error on Python/Flask API
I've been all over google, and stack and read EVERY article I could find but have not been able to solve me issue. 我到过Google遍地,堆砌并阅读我能找到但无法解决我问题的每篇文章。 I recently wanted to take my project that is still in development and get it up on GAE so I could test it and make sure it would work as I assumed.
我最近想把仍在开发中的项目带到GAE上,这样我就可以对其进行测试并确保它可以按照我的设想工作。 After some trail and error I was able to get a Postgres database setup and push my project using
经过几经周折,我能够获得Postgres数据库的设置,并使用
gcloud app deploy app.yaml
My Flask app is using Swagger and Flask-Restplus for the API. 我的Flask应用将Swagger和Flask-Restplus用于API。 I can access the swagger page and perform some requests, however all of those requests come back with
我可以访问招摇页面并执行一些请求,但是所有这些请求都会返回
502 Bad Gatway
While reading the live log in the console I can see the data is coming in as I would expect. 在控制台中读取实时日志时,我可以看到数据如预期的那样进入。 There are no exceptions, and the GAE error console does not even log the 502 error.
没有例外,GAE错误控制台甚至都不会记录502错误。 In the console I can also see..
在控制台中,我也可以看到。
[CRITICAL] WORKER TIMEOUT (pid:17)
So I went into the logs and see this NGINX error.. 所以我进入了日志,看到了这个NGINX错误。
textPayload: "[error] 32#32: *4086 upstream prematurely closed connection while reading response header from upstream, client: 130.211.2.231, server: , request: "POST /auth/login_user HTTP/1.1", upstream: "http://172.17.0.1:8080/auth/login_user", host: "blah-191811.appspot.com", referrer: "http://blah-191811.appspot.com/""
I don't think it is an error connecting to my database because I can connect to the Postgres DB hosted on GAE from my local computer. 我认为连接到数据库不是错误,因为我可以从本地计算机连接到GAE上托管的Postgres DB。 I also have another API endpoint that just uses a google API to retrieve some info and that also produces a 502 error.
我也有另一个API端点,该端点仅使用google API来检索一些信息,并且还会产生502错误。
I am at my wits end, and honestly about to throw in the towel and try to get it going on AWS if I can't figure this out. 我尽力了,老实说,如果我不知道这件事,我将投入工作并尝试使其在AWS上运行。 Any help would be much appreciated.
任何帮助将非常感激。
EDIT WITH MORE INFO: 使用更多信息进行编辑:
The problem still persists. 问题仍然存在。 However, I have two routes on my API that require a valid JWT header.
但是,我的API上有两条路由需要有效的JWT标头。 When using these routes with an invalid header token, or no token the API returns the correct response, a 401. When sending the correct token the API again returns a 502. This makes me believe that there is nothing wrong with my code, but that somewhere the response is not getting sent back.
当使用带有无效标头令牌或没有令牌的这些路由时,API返回正确的响应,即401。当发送正确的令牌时,API再次返回502。这使我相信代码没有错,但是那某个地方的响应没有被发回。
I should add that these are not data intensive calls. 我应该补充一点,这些不是数据密集型调用。 The login_user is just two strings, an email and a password.
login_user只是两个字符串,一个电子邮件和一个密码。 The database has only one entry in it.
数据库中只有一个条目。
When I run locally but CONNECT to the remote Postgres database the API works as expected. 当我在本地运行但连接到远程Postgres数据库时,API可以正常工作。 Ie if I run a Flask server locally and do;
即如果我在本地运行Flask服务器并执行; 127.0.0.1:5000/auth/login_user and send the correct information, it is able to read back from my Postgres database on my GAE project.
127.0.0.1:5000/auth/login_user并发送正确的信息,它可以从我的GAE项目上的Postgres数据库中回读。 So I don't believe the database is the issue either.
因此,我也不认为数据库是问题所在。
Still looking for any wisdom because this seems to be a very common issue with little resolve. 仍在寻找任何智慧,因为这似乎是一个非常普遍的问题,没有什么决心。
For anyone else experiencing this issue..I found the solution after much digging. 对于遇到此问题的其他人。.经过大量挖掘,我找到了解决方案。 It was in the way I was connecting to the potgres SQL instance on GAE.
这是我连接到GAE上的potgres SQL实例的方式。
I was using this; 我在用这个
SQLALCHEMY_DATABASE_URI = 'postgresql://user:password@remote.ip.addr/database_name'
This code works fine when you are accessing the SQL instance from a whitelisted IP. 从列入白名单的IP访问SQL实例时,此代码可以正常工作。 However this code will not work inside an instance that also runs on GAE.
但是,此代码在也可以在GAE上运行的实例中无法运行。 You need to change it to this;
您需要将其更改为此。
SQLALCHEMY_DATABASE_URI = 'postgresql://user:password@/database_name?
host=/cloudsql/database_instance_id'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.