nginx + gunicorn抛出截断的反应体

时间:2015-02-25 23:59:16

标签: django nginx gunicorn

我通过某个网址请求一些json数据,有时它工作正常,有时却没有...我在这里看了另一个相关的猜测,但似乎建议不要通过中间件更改内容长度...我的json数据不完整如下图所示:

enter image description here

我的app nginx配置:

server {
    listen 80;
    server_name myapp.com;

    access_log /var/log/nginx/myapp_access.log;
    error_log /var/log/nginx/myapp_error.log;

    location / {
    client_max_body_size 400M;
    proxy_read_timeout 120;
        proxy_connect_timeout 120;
    proxy_set_header Host $http_host; 
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Client-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:8888;
    }
}

gunicorn脚本:

#!/bin/bash
set -e
DJANGODIR=/home/ubuntu/apps/myapp

LOGFILE=/var/log/gunicorn/myapp.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=3
# user/group to run as
USER=ubuntu
GROUP=ubuntu
cd /home/ubuntu/apps/myapp
source /home/ubuntu/.venv/myapp/bin/activate

export PYTHONPATH=$DJANGODIR:$PYTHONPATH

export NEW_RELIC_CONFIG_FILE=/home/ubuntu/newrelic/newrelic.ini

test -d $LOGDIR || mkdir -p $LOGDIR
exec /usr/local/bin/newrelic-admin run-program /home/ubuntu/.venv/myapp/bin/gunicorn_django -w $NUM_WORKERS \
  --user=$USER --group=$GROUP --log-level=debug \
  --log-file=$LOGFILE -b 127.0.0.1:8888 2>>$LOGFILE

1 个答案:

答案 0 :(得分:5)

嗯......我找到了一个相关问题here,我在配置文件中添加了proxy_buffering off;,这解决了我的问题。

文件如下:

server {
    listen 80;
    server_name myapp.com;

    access_log /var/log/nginx/myapp_access.log;
    error_log /var/log/nginx/myapp_error.log;

    location / {
        client_max_body_size 400M;
        proxy_read_timeout 120;
        proxy_connect_timeout 120;
        proxy_set_header Host $http_host; 
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Client-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:8888;
        proxy_buffering off;
    }
}