【发布时间】:2019-05-22 04:11:22
【问题描述】:
我正在使用基于 https://github.com/tiangolo/uwsgi-nginx-flask-docker/tree/master/python3.6 的 dockerimage。我在里面运行一个接受 POST 的 python 应用程序,对 json 主体进行一些处理,然后返回一个简单的 json 响应。像这样的帖子:
curl -H "Content-Type: application/json" -X POST http://10.4.5.168:5002/test -d '{"test": "test"}'
工作正常。但是,如果我发布一个更大的 json 文件,我会得到一个 504: Gateway Timeout。
curl -H "Content-Type: application/json" -X POST http://10.4.5.168:5002/test -d @some_6mb_file.json
我感觉 Nginx 和 Uwsgi 之间的通信存在问题,但我不知道如何解决。
编辑:我跳入 docker 容器并手动重新启动 nginx 以获得更好的日志记录。我收到以下错误:
2018/12/21 20:47:45 [错误] 611#611: *1 上游超时 (110: 连接超时)同时从上游读取响应头, 客户端:10.4.3.168,服务器:,请求:“POST /model/refuel_verification_model/predict HTTP/1.1”,上游: “uwsgi://unix:///tmp/uwsgi.sock”,主机:“10.4.3.168:5002”
从容器内部,我启动了我的 Flask 应用程序的第二个实例,在没有 Nginx 和 Uwsgi 的情况下运行,它运行良好。响应需要大约 5 秒才能返回(由于数据的处理时间。)
配置:
/etc/nginx/nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
daemon off;
/etc/nginx/conf.d/nginx.conf:
server {
listen 80;
location / {
try_files $uri @app;
}
location @app {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
location /static {
alias /app/static;
}
}
/etc/nginx/conf.d/upload.conf:
client_max_body_size 128m;
client_body_buffer_size 128m;
【问题讨论】:
-
愚蠢的问题,但你检查过 nginx 和 Uwsgi 的配置文件吗?也许他们只是有一个你需要增加的超时参数?
-
我可能应该提到,虽然超时需要 60 秒才能显示,但应用程序会在一秒左右内停止处理。我认为问题在于 uwsgi 没有响应 nginx。
-
你是如何启动你的容器的?
-
你的 nginx 设置中的
client_max_body_size是什么?默认为 1mb。 -
尝试在 NGINX 配置中设置 uwsgi_read_timeout 和 uwsgi_send_timeout (nginx.org/en/docs/http/…)。