0

I deployed my Django project using Gunicorn, but now can't use Nginx caching.

How to start caching on a project that use Gunicorn and which caching method is standard for Django.

I try to use Ngnix caching but it won't work.

Here is an example of my Ngnix conf and Caching Conf

Ngnix Conf

 /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;
}
}

Cache Config

/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 Socket

/etc/systemd/system/gunicorn.socket

  [Unit]
  Description=gunicorn socket

  [Socket]
  ListenStream=/run/gunicorn.sock

  [Install]
  WantedBy=sockets.target

Gunicorn Service

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
SAM
  • 58
  • 6

1 Answers1

0

I am also using gunicorn to host a django webserver on Nginx, and I did not run into this problem. I don't believe it should matter that you are hosting with gunicorn. As long as you configure caching for the directory that contains all of your static files it should work.

Here's an example where the static files are cached for a year:

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

If this doesn't solve the problem please add your cache settings for Nginx to your question

If you are trying to cache django views or templates, look into django-cahceops or a similar package

  • it does not clear anything. can you write with more information, please? – SAM Dec 29 '21 at 11:54
  • I could be mistaken but I dont believe gunicorn should be related to whether nginx caching works or not, but since I'm not familiar enough with nginx to tell you what the issue is, we will have to wait for someone more knowledgeable to answer. What I know is that it immediately worked for me when I added the above caching settings to nginx and restarted everything – John-Hugh Hedrick Dec 29 '21 at 17:56