我正在嘗試使用 Nginx + uwsgi 運行我的 Django 應用程序,但504 Gateway Time-out
加載一分鐘後我收到訊息。
我的應用程式需要時間來完成所需的操作,因為它在多個網站上搜尋特定的內容。
我的 nginx 設定是下一個:
upstream uwsgi {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name server_ip;
root /opt/emails/subscriptions;
index index.html index.htm index.php;
location /emailsproject/ {
root /opt/emails/subscriptions/;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://uwsgi;
proxy_set_header Host $http_host;
uwsgi_read_timeout 18000;
}
}
我的 uwsgi 腳本:
description "uWSGI server"
env PYTHONPATH=/opt/emails/subscriptions
env DJANGO_SETTINGS_MODULE=emailsproject.settings
start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec uwsgi_python --http-socket 127.0.0.1:8000 -p 4 --wsgi-file /opt/emails/subscriptions/emailsproject/wsgi.py
我的 nginx 在 error.log 中給出以下錯誤訊息:
2015/09/28 02:15:57 [error] 4450#0: *19 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 37.235.53.246, server: my_server_ip, request: "POST /home/ HTTP/1.1", upstream: "http://127.0.0.1:8000/home/", host: "my_server_ip", referrer: "http://my_server_ip/home/"
有人知道我怎麼才能擺脫這個?我已經嘗試了大量的 stackoverflows 解決方案,但沒有一個對我有用。
答案1
我知道我遲到了,但在嘗試了許多這些建議(以及其他建議)之後,我最終發現我的超時是從我的DNS 發生的- 如果您使用亞馬遜負載平衡器,他們有一個“空閒超時”設置為預設 120 秒。
答案2
那是因為你需要設置proxy_read_timeout
,而不是uwsgi_read_timeout
。反過來,這是因為您實際上沒有使用 uwsgi,而是使用 HTTP 代理。 Uwsgi 後端是使用uwsgi_pass
指令宣告的。
答案3
許多人認為 504 與來自客戶端的請求有關,或者網站上存在一些 DDoS 攻擊,並且會話堆積量很大。 504 是雙向的,如果您的 wsgi 無法顯示代碼和/或伺服器因溢出而無法回應,您可能會看到錯誤。因此,請確保執行 manage.py 來仔細檢查您的程式碼是否可用。然後嘗試"curl -I yoursite.com"
命令看看它是否仍然給你錯誤。您需要關注兩個 nginx 相關文件,其中一個用於/etc/nginx/nginx.conf
預設全域設定。第二個是你在其中創造自己的東西/etc/nginx/sites-available
。在您的全域/etc/nginx/nginx.conf
安裝中新增以下行
proxy_connect_timeout 10;
proxy_send_timeout 15;
proxy_read_timeout 20;
該解決方案解決了 90% 不同場景下導致的 504 錯誤。您的專案配置應該與上面提到的相同。