Nginx加载图片非常慢

时间:2016-02-20 07:22:15

标签: django ubuntu nginx

我正在尝试使用Jpeg格式加载一些高分辨率图像,并且在Django制作中规格是300dpi,5000 x 5000分辨率。 以下是我在digitalocean上的nginx设置:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

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

    client_max_body_size 4G;
    server_name localhost;


    keepalive_timeout 5;

    # 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/django_project/ {
        alias /home/django/django_project/static/django_project/; 
    }
    # 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;
    }
}

启用浏览缓存后:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

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

    client_max_body_size 4G;
    server_name mysite.com;


    keepalive_timeout 5;

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        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/django_project/ {
        alias /home/django/django_project/static/django_project/; 
    }
    # 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;
    }
}

我在活动网站上的图片加载速度非常慢。请建议或参考一些有用的资源来解决这个问题。

由于

1 个答案:

答案 0 :(得分:-1)

请使用nginx缓存,这将解决您的问题..

https://www.nginx.com/blog/nginx-caching-guide/

您也可以通过启用浏览器缓存来缓存图像...

https://www.howtoforge.com/make-browsers-cache-static-files-on-nginx