Nginx: How to cache static files served by uwsgi.

The django project is deployed using uwsgi as application server, it also serves static files from a specified directory(as shown in the below command) and nginx is used as reverse proxy server.  I would like to cache the uwsgi served static files into the nginx.

Why i want to serve static files throgh uwsgi?

It is because this application will be built in docker, where two container i.e backend(django, uwsgi) and frontend (nginx). The static files will be gathered in `project/static` backend container, so that is served through uwsgi to frontend(nginx). In the frontend(nginx) the caching of static files needs to be achived.


Description of the issue:

The uwsgi command to run the server is as follows:

`uwsgi -b 65535 --socket :4000 --workers 100 --cpu-affinity 1 --module wui.wsgi --py-autoreload 1 --static-map /static=/project/static;`

The application is working fine at this point. I would like to cache the static files into nginx server. So i have referred blog  and i have included following configuration in my nginx.conf :
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
   expires max;
   log_not_found off;
   access_log off;
After adding this into my Nginx conf, the static files are not loading in the web browser.

Is this how uwsgi served static files can be cached into nginx? If yes please suggest me what is gone wrong here. 

My complete nginx.conf is as follows:
events {
  worker_connections  1024;  ## Default: 1024

http {
    include     conf/mime.types;

    # the upstream component nginx needs to connect to
    upstream uwsgi {
        server backend:4000; # for a web port socket (we'll use this first)

    # configuration of the server
    server {
        # the port your site will be served on
        listen      8443 ssl http2 default_server;

        # the domain name it will serve for
        server_name _; # substitute your machine's IP address or FQDN
        charset     utf-8;

        ssl_certificate     /secrets/server.crt;
        ssl_certificate_key /secrets/server.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        add_header Strict-Transport-Security "max-age=31536000" always;

        # Redirect HTTP to HTTPS
        error_page 497 https://$http_host$request_uri;

        # max upload size
        client_max_body_size 75M;   # adjust to taste
        uwsgi_read_timeout 600s;

        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  uwsgi;
            include     /config/uwsgi_params; # the uwsgi_params file you installed

        location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        expires max;
        log_not_found off;
        access_log off;

Nginx version: 1.16

tuckeranddale's picture

Did you try to use Varnish?

I run a web project with Nginx as a gateway for SSL and apache configured with varnish to serve the static files. This is fast, reliable and wide-spread popular server-stack.


But I dont know if the uwsgi  application server can be configured to use varnish as a proxi. I think you will run into trouble if you try to use Nginx as proxi and as Gateway to server SSL at the same time. But maybe this is also possible.

Vote the answer: 
No votes yet