【问题标题】:DJango + nginx + gunicorn: how to start caching and which way is more standard?DJango + nginx + gunicorn:如何开始缓存,哪种方式更标准?
【发布时间】:2022-01-27 14:47:20
【问题描述】:

我使用 Gunicorn 部署了我的 Django 项目,但现在无法使用 Nginx 缓存。

如何在使用 Gunicorn 的项目上开始缓存以及哪种缓存方法是 Django 的标准。

我尝试使用 Ngnix 缓存,但它不起作用。

这是我的 Ngnix conf 和 Caching Conf 的示例

Ngnix 会议

 /etc/nginx/sites-available/my-project-config

upstream daphne_server {
  server unix:/var/www/my-project-config/env/run/daphne.sock fail_timeout=0;
}

upstream gunicorn_server {
  server unix:/var/www/my-project-config/env/run/gunicorn.sock fail_timeout=0;
}


server {
    listen 8000;
    server_name my_server_ip_address or domain name;

    client_max_body_size 100M;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/my_project_dir/public_html;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

   location /ws/ {
    proxy_pass http://localhost:8001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
}
}

缓存配置

/etc/nginx/sites-available/my-project-cache-config

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=custom_cache:10m inactive=60m;
upstream origin_server {
    server 127.0.0.1:8000;
}
server {
    listen 80;
    server_name _;
    location / {
        include proxy_params;
        proxy_pass http://origin_server;
        proxy_cache custom_cache;
        proxy_cache_valid any 10m;
        add_header X-Proxy-Cache $upstream_cache_status;
    }
}

Gunicorn 插槽

/etc/systemd/system/gunicorn.socket

  [Unit]
  Description=gunicorn socket

  [Socket]
  ListenStream=/run/gunicorn.sock

  [Install]
  WantedBy=sockets.target

Gunicorn 服务

sudo nano /etc/systemd/system/gunicorn.service

[Unit]
  Description=gunicorn daemon
  Requires=gunicorn.socket
  After=network.target

  [Service]
  User=ubuntu
  Group=www-data
  WorkingDirectory=/home/ubuntu/my_project_dir
  ExecStart=/home/ubuntu/my_project_dir/venv/bin/gunicorn \
            --access-logfile - \
            --workers 3 \
            --bind unix:/run/gunicorn.sock \
            my_project.wsgi:application

  [Install]
  WantedBy=multi-user.target

【问题讨论】:

  • 你能举一个你的缓存配置的例子吗?

标签: django nginx caching gunicorn django-deployment


【解决方案1】:

我也在使用 gunicorn 在 Nginx 上托管一个 django 网络服务器,我没有遇到这个问题。我认为您使用 gunicorn 托管并不重要。只要您为包含所有静态文件的目录配置缓存,它就可以工作。

这里是一个静态文件被缓存一年的例子:

    location /static {
        root [location of /static folder];
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }

如果这不能解决问题,请将您的 Nginx 缓存设置添加到您的问题中

如果您尝试缓存 django 视图或模板,请查看 django-cahceops 或类似包

【讨论】:

  • 它不会清除任何东西。你能写更多的信息吗?
  • 我可能弄错了,但我不认为 gunicorn 应该与 nginx 缓存是否有效有关,但由于我对 nginx 不够熟悉,无法告诉你问题是什么,我们将不得不等有知识的人来回答。我所知道的是,当我将上述缓存设置添加到 nginx 并重新启动一切时,它立即为我工作
猜你喜欢
  • 2018-06-15
  • 2019-10-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2013-03-25
  • 2013-03-01
  • 2014-06-28
  • 2013-03-11
相关资源
最近更新 更多