Django with nginx - Waitingtime is more How to reduce

26 views
Skip to first unread message

Anandhakumar Radhakrishnan

unread,
Jun 1, 2015, 2:21:21 PM6/1/15
to django...@googlegroups.com
Am using Django 1.7, nginx

My Django view page having dictionary to render to html.

    Content = { ......}
    r
= render_to_response('tr/my-content.html',Content, context_instance=RequestContext(request)) ; t2 = datetime.now() ; print "Elapsed: %.3f" % (t2-t1).total_seconds() ;
   
print (len(r.content))    
   
return r


While calling this view it prints

Elapsed: 1.993

23254

So it take's 2 sec to render a dict to my template . But the page was loading after 15seconds only. So where is the problem how could I rectify that.


Sample template.



    {% extends "base.html" %}
   
{% load staticfiles %}
   
{% load thumbnail %}

   
{% block title %}<title> My title</title>{% endblock title %}

    {% block extra-css %}
    <link href="{% static "css/
jquery.mCustomScrollbar.css" %}" rel="stylesheet">
   
<link href="{% static "css/tooltipster.css" %}" rel="stylesheet">
   
{% endblock extra-css %}

   
{% block content %}
       
<div id="{{ mainClass }}" class="main-container">
       
<!-- Header Starts Here -->
         
{% include "pages/header-sub.html" %}
           
<!-- Header ends Here -->
           
<!-- Album View -->
           
<!-- Main cintent area begin -->
           
<section class="d-mobile">
           
{% if trac %}
               
<ul class="a-contents">
                   
{{ trac | safe}}
               
</ul>
            {% else %}
            <div class="container text-center ">
                <p>Your content section is empty. Please use the "upload file" button at the bottom to begin adding tracks.</
p>
           
</div>
            {% endif %}
            </
section>
       
</div>
             
.........
             
.........
   
{% endblock content %}



In my view file , Am rendering the dict to

<!-- language: lang-python -->

Sample Dict:


    sample = { 'List': u'<li><a href="/dashboard/performance/?store=Play_list">Play_list</a></li><li><a href="/drd/page/?sre=Road">Road</a></li><li><a href="/drd/page/?sre=Sify">Sify</a></li><li><a href="/drd/page/?sre=Youtube">Youtube</a></li>', 'Date' :  u'<li><a href="/dashboard/performance/?store=Play_list">Play_list</a></li><li><a href="/drd/page/?sre=Road">Road</a></li><li><a href="/drd/page/?sre=Sify">Sify</a></li><li><a href="/drd/page/?sre=Youtube">Youtube</a></li>' ..........}




from Inspect element:

Stalled time 4.2 ms

DNS Look UP : 0

Initial Connection : 4.1 ms

Request Sent : 0.02 ms

Waiting(TTFB) : 15.5 s

Content Download: 184 ms




My nginx setup is

  
  user root;
    worker_processes
4;
   
    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
;
        gzip_comp_level
2;
        gzip_proxied any
;
        gzip_types    text
/plain application/javascript application/x-javascript text/javascript text/xml text/css;
        gzip_vary on
;
        server_tokens off
;
   
        include
/etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }



In the sites-enabled/mysite.com

which has

    upstream test
{
            server
127.0.0.1:8000;
            keepalive
500;
   
}
   
   
    server
{
   
        listen  
80;
        server_name www
.mysitee.com;
        client_max_body_size
4G;
   
        error_page
502 =200 @maintenance;
   
        location
@maintenance {
            root
/path/to/static/offline/files;
            try_files $uri
/index.html =503;
       
}
        location
/static/ {
   
           
alias  /home/dev/live/staticfiles/;
           expires
30d;
       
}
   
        location
/media/ {
           
alias  /home/dev/live/myproject/myproject/site_media/media/;
           expires
30d;
       
}
        location
/ {
            proxy_set_header X
-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header
Host $http_host;
            proxy_redirect off
;
           
if (!-f $request_filename) {
                     proxy_pass http
://test;
                     
break;
           
}
   
       
}
   
}





Could you please tell how to reduce the waiting time.

Gagaro

unread,
Jun 1, 2015, 2:31:05 PM6/1/15
to django...@googlegroups.com
Hi,

You can use the Django Debug Toolbar to see actual time taken by your queries/templates/... Depending on what is taking time for you, there may be several solutions. If you have issues with your queries, you should take a look at select_related and prefetch_related.
...

Tom Evans

unread,
Jun 1, 2015, 2:41:09 PM6/1/15
to django...@googlegroups.com
On Mon, Jun 1, 2015 at 3:01 PM, Anandhakumar Radhakrishnan
<rkanand...@gmail.com> wrote:
> Am using Django 1.7, nginx

You're only using nginx to proxy to a backend webserver and serve
static files. What is the backend webserver that is hosting django?

> ...
>
> In the sites-enabled/mysite.com
>
> which has
>
> upstream test {
> server 127.0.0.1:8000;
> keepalive 500;
> }
>
> ...
> location / {
> proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
> proxy_set_header Host $http_host;
> proxy_redirect off;
> if (!-f $request_filename) {
> proxy_pass http://test;
> break;
> }

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages