【问题标题】:Nginx loading images very slowNginx 加载图片很慢
【发布时间】:2016-06-01 21:58:00
【问题描述】:

我正在尝试加载一些 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;
    }
}

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

谢谢

【问题讨论】:

    标签: django ubuntu nginx


    【解决方案1】:

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

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

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

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

    【讨论】:

    • Sathik 在“proxy_cache_path”指令下的“/path/to/cache”中我应该在哪里设置我的路径以及确切的文件夹。我正在使用 Django,我所有的静态文件都位于不同的文件夹中,我的动态应用程序使用不同的位置从用户上传图像。
    • 如链接中所述,请将它们添加到顶部。以上到服务器 { 。由于您正在向浏览器提供静态文件,因此不需要在应用路径中。
    • 根据您的建议,我添加了浏览缓存,但问题仍然存在。我添加的方式是在“启用浏览缓存后:问题区域。请告知。
    • 这是一种方法,这将是第一次加载,但下一个请求会更快。但是,如果图像尺寸很大,则需要寻找替代方案。您能告诉我您尝试加载的图片尺寸吗?
    • 这些是高分辨率图像,预计访问该站点的大多数用户是 MAC 用户。所以我需要高分辨率的图像。图像为 5616 x 3744,具有 300dpi 和 24 位深度。格式为 Jpeg,所有图像均围绕这些规范。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    相关资源
    最近更新 更多