0

I have a server on Nginx. It is written with Django and connected to Nginx through Waitress. I have set up a cache in Nginx. My server has both dynamic and static pages.

After starting a server, it caches only static pages(correct behaviour for me), but I also want it to have a Cache-Control header in HTTP-request(I need Cache-Control no-store, to not cache anything at all), but it doesn't.

I don't understand how to set up Cache-Control(no-store) header, because when I try to write it into Nginx's config file, then although this header appears in HTTP-request(in Postman), Nginx ignores it and continues caching into my cache folder(i.e. proxy_cache_path). So, I think, that I need somehow to put Cache-Control header into Django itself.

My nginx.conf file:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    include C:/Users/User/Desktop/BDUTS/nginx_server/sites-enabled/locallibrary.conf;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       10;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

Included locallibrary.conf file:

# CACHE
proxy_cache_path cache/ levels=1:2 keys_zone=cache:32m;

# cache-proxy
server {
    listen 80;
    location / {
        proxy_pass http://127.0.0.1:8080/;
        proxy_cache_key "$host$request_uri:u:$cookie_user";
        proxy_cache_valid any 10s;
        proxy_cache cache;
    }
}

server{
    listen 8080;

    server_name 127.0.0.1;
    charset utf-8;

    client_max_body_size 100M;

    location / {
        proxy_pass http://localhost:8080;
        proxy_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;
    }
}

It's runserver.py file to run server with Waitress:

#runserver.py
from waitress import serve

from locallibrary.wsgi import application
print("Starting server...")
if __name__ == '__main__':
    serve(application, host='127.0.0.1', port='8080')

So, I have a main question. How to correctly add Cache-Control header to my server and make it be taken into account by Nginx?

I've tried next:

  1. To put add_header Cache-Control no-store; in locallibrary.conf after proxy_cache cache; line in #cahce-proxy. It doesn't work, because, in my opinion, it added Cache-Control header only after sending answer to client already. So, it doesn't go to the Django at all.
  2. I think, that I need somehow to control headers with Django, but found nothing in Django's source folder and project folder.
Kenion
  • 27
  • 4
  • 1
    Use the HttpResponse object https://docs.djangoproject.com/en/4.1/ref/request-response/#httpresponse-objects – Aleksey Vaganov Feb 22 '23 at 05:51
  • [This](https://stackoverflow.com/questions/36099244/how-to-add-an-http-header-to-all-django-responses) topic answered my question. – Kenion Feb 28 '23 at 18:22

0 Answers0