Nginx 504 Gateway Timeout Error for Django

2019-04-29 19:49发布

I am running a Django site on DigitalOcean using 1ClickInstallation image. Every thing worked fine but I got issue for 504 Gateway Timeout Error. I've tried multiple settings on blogs but not working. Following are my settings:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;

}


server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /home/django/django_project;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name www.mydomain.com;


    keepalive_timeout 5;

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|woff2|woff|ttf)$ {
        expires 365d;
    }



    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/media/;
    }

    # your Django project's static files - amend as required
    location static/static-only {
        alias /home/django/django_project/static-only/; 
    }
    # Django static images
    location /static/django_project/images {
        alias /home/django/django_project/static-only/django_project/images/;
    }


    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin;
    }

    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;

    }
}

I followed docs at following link http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

Following is the result of "wget 127.0.0.1:9000"

enter image description here

but couldn't make sense that where exactly to add directives. Kindly advise.

3条回答
做自己的国王
2楼-- · 2019-04-29 20:12

I know I'm late to the party here but after trying many of these suggestions (and others) I eventually found the timeout for me was occurring from my DNS - if you're using Amazon load balancers they have an "Idle timeout" set at 120s default.

查看更多
Fickle 薄情
3楼-- · 2019-04-29 20:18

If you are using uwsgi with django, then you might add uwsgi_read_timeout directive to nginx's config file at location place

location / { 
    uwsgi_read_timeout 120; 
}
查看更多
萌系小妹纸
4楼-- · 2019-04-29 20:24

I found the solution as I was trying to make changes in /etc/nginx/sites-available/django-project. But I needed to add following lines in /etc/ninx/nginx.conf the global settings for Nginx. Lines I've added are:

http {
    ...
    proxy_connect_timeout   10;
    proxy_send_timeout      15;
    proxy_read_timeout      20;
}

I have a small website hosted and this one above settings are enough. But others may set their settings according to their needs.

查看更多
登录 后发表回答