簡體   English   中英

將JSON從Nginx傳遞到Gunicorn

[英]Passing JSON from Nginx to Gunicorn

我正在使用nginx作為使用gunicorn的Django應用程序的代理服務器。 Django應用已綁定到http://127.0.0.1:8000 這是我在etc / nginx / sites-enabled / parkitbackend中設置的Nginx:

server {

    server_name AA.BB.CC.DD;

    access_log off;

    location /static/ {
        autoindex on;
        alias /home/zihe/parkitbackend/parkitbackend/common-static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

我正在使用python請求模塊:

requests.post("http://AA.BB.CC.DD/dashboard/checkin/", data=unicode(json.dumps(payload), "utf8"))

to process the JSON object. 將JSON對象發布到我的django應用中,名為dashboard,在dashboard / views.py中,我有一個稱為的函數來處理JSON對象。

我沒有從運行JSON發布腳本收到任何錯誤。 但是,Nginx似乎無法將請求傳遞給綁定在127.0.0.1:8000的gunicorn。 我應該怎么做才能使用Nginx將JSON傳遞給django應用程序? 謝謝!


附加說明:我非常確定JSON發布代碼和django應用程序正常工作,因為我通過將Django應用程序綁定到http://AA.BB.CC.DD:8000並在python中運行了此代碼進行了測試:

requests.post("http://AA.BB.CC.DD:8000/dashboard/checkin/", data=unicode(json.dumps(payload), "utf8"))

我的django應用程序收到了預期的JSON。

我檢查了位於/ var / log / nginx /的error.log。 事實證明,我發送的JSON太大,並出現此錯誤:

[error] 3450#0: *9 client intended to send too large body: 1243811 bytes, client: 127.0.0.1, server: _, request: "POST /dashboard/checkin/ HTTP/1.1", host: "127.0.0.1"

閱讀完此鏈接后: http : //gunicorn-docs.readthedocs.org/en/19.3/deploy.html#nginx-configuration

我減小了JSON的大小,並修改了etc / nginx / sites-enabled / parkitbackend,如下所示:

upstream app_server {
    server 127.0.0.1:8000;
}

server {
    listen AA.BB.CC.DD:80;
    server_name = _;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass http://app_server;
    }

    location /static/ {
        autoindex on;
        alias /home/username/parkitbackend/parkitbackend/common-static/;
    }

}

並在/etc/nginx/nginx.conf中替換此行:

include /etc/nginx/sites-enabled/*;

有了這個:

include /etc/nginx/sites-enabled/parkitbackend;

問題已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM