我当前的烧瓶应用程序设置遇到问题,非常感谢您提供一些建议。谢谢!
流程
用户-> nginx-> uwsgi-> flask应用程序->对外部系统的https调用(已处理响应并将相关数据返回给客户端)
工作流程
[Intent我的烧瓶视图/路由调用另一个类,在该类中对外部系统进行https(GET)调用以检索数据。然后处理(分析)该数据,并将适当的响应发送给用户。
Actual调用基于Flask的终结点时,用户从Web服务器收到502 Bad Gateway。仅当将Nginx和uwsgi服务器放在我的flask应用程序之前时,才会发生这种情况。直接在flask的内置服务器上对服务器进行的初始测试似乎可以正常工作。
注意:该分析位确实要花费一些时间,所以我增加了所有相关的超时时间(无济于事)
配置
Nginx(尝试使用TLS和不使用TLS)
worker_processes 4;
error_log /path/to/error.log;
pid /path/to/nginx.pid;
events {
worker_connections 1024;
}
http {
default_type application/json;
access_log /path/to/access.log;
sendfile on;
keepalive_timeout 0; [multiple values tried]
# HTTPS server
server {
listen 5555 ssl;
server_name my_host.domain.com;
ssl_certificate /path/to/server.crt;
ssl_certificate_key /path/to/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location /my_route {
uwsgi_connect_timeout 60s;
uwsgi_read_timeout 300s;
client_body_timeout 300s;
include uwsgi_params;
uwsgi_pass unix:/path/to/my/app.sock;
}
}
}
uWSGI(作为故障排除尝试的一部分,线程减少为1)
[uwsgi]
module = wsgi:app
harakiri = 300 [also added as part of troubleshooting steps]
logto = /path/to/logs/uwsgi_%n.log
master = true
processes = 1
threads = 1
socket = app.sock
chmod-socket = 766
vacuum = true
socket-timeout = 60
die-on-term = true
代码段
主要烧瓶类(视图)
@app.route(my_route, methods=['POST'])
def my_view():
request_json = request.json
app.logger.debug(f"Request Received: {request_json}")
schema = MySchema()
try:
schema.load(request_json)
var1 = request_json["var1"]
var2 = request_json["var2"]
var3 = request_json["var3"]
var4 = request_json["var4"]
# begin
execute = AnotherClass(client_home, config, var1, var2, var3, var4, mime_type)
return jsonify(execute.result)
except ValidationError as exception:
error_message = json.dumps(exception.messages)
abort(Response(error_message, 400, mimetype=mime_type))
在外部系统上执行HTTPS GET的类
custom_adapter = HTTPAdapter(max_retries=3)
session = requests.Session()
session.proxies = self.proxies
session.mount("https://", custom_adapter)
try:
json_data = json.loads(session.get(process_endpoint, headers=self.headers, timeout=(3, 6)).text)
错误
Nginx
错误] 22680#0:* 1上游过早关闭连接,而从上游读取响应头,客户端:client_ip,服务器:server_name,请求:“ POST / my_route HTTP / 1.1”,上游:“ uwsgi:// unix:/path/to/my/app.sock:”,主机:“ server_name:5555”
用户在其末端收到502(错误的网关)
uWSGI
2020-04-24 16:57:23,873-app.module.module_class-调试-端点:https://external_system.com/endpoint_details2020-04-24 16:57:23,876-urllib3.connectionpool-调试-开始新的HTTPS连接(1):external_system.com:443 !!! uWSGI进程####出现分段错误!!!* #### ]的回溯<> / path / to / anaconda3 / bin / uwsgi(uwsgi_backtrace + 0x2e)[0x610e8e]/ path / to / anaconda3 / bin / uwsgi(uwsgi_segfault + 0x21)[0x611221]/usr/lib64/libc.so.6(+0x363f0)[0x7f6c22b813f0]/path/to/anaconda3/lib/python3.7/lib-dynload/../../libssl.so.1.0.0(ssl3_ctx_ctrl+0x170)[0x7f6c191b77b0]/path/to/anaconda3/lib/python3.7/site-packages/cryptography/hazmat/bindings/_openssl.abi3.so(+0x5a496)[0x7f6c16de2496]....回溯结束* DAMN! worker 1(pid:####)死了:(正在尝试重生...重新生成uWSGI worker 1(new pid:####)
我当前的烧瓶应用程序设置遇到问题,非常感谢您提供一些建议。谢谢!流用户-> nginx-> uwsgi-> flask应用程序->对外部系统的https调用(响应...
已解决