1

My setup uses a map to define different Expires headers for various static files (based on their mime type). For proxied content (uwsgi_pass), I use a static Expires header:

map $sent_http_content_type $expires {
    default                    off;
    text/html                  7d;
    text/css                   180d;
    application/font-woff2      1y;
    application/pdf             1y;
    ~image/                     1y;
}

server {
    listen       443 ssl http2;
    server_name  example.com;
    
    location / {
            uwsgi_pass django;
            expires 7d;
    }
    
    location ~ ^/(css|fonts|images|pdf|html)/ {
            root /var/www/django/static/;
            expires $expires;
    }
}

Using the map (via the $expires variable) for proxied content does not work. Is there any way for nginx to identify the mime type of proxied content in order to make this work?

location / {
        uwsgi_pass django;
        expires $expires;
}

(Edit) As Alexey suggested, I did this:

map $upstream_http_content_type $expires2 {
         default                    off;
         text/html                  2d;
         text/plain                  20d;
         text/css                   180d;
}

location / {
        uwsgi_pass      django;
        expires $expires2;
}

Unfortunately, there is still no Expires header added to the content delivered via uwsgi.

4
  • Try map $upstream_http_content_type $expires2 { ... } with expires $expires2;
    – Alexey Ten
    Commented Nov 15, 2023 at 11:30
  • Thank you – this is what I am looking for, but unfortunately, this configuration still creates no Expires header for proxied content (added the code to my question).
    – janeden
    Commented Nov 15, 2023 at 11:47
  • You need to check the Content-Type header using curl -I. If it's like Content-Type text/html; charset=utf-8 it will not match your map, and you may want to use a regex instead. Commented Nov 15, 2023 at 14:58
  • That did the trick! The Content-Type header did contain the encoding, and changing the map entry to ~text/html 2d; solved my problem. Thank you!
    – janeden
    Commented Nov 16, 2023 at 19:04

1 Answer 1

0

The solution was provided by Alexey and Richard in the comments. I had to use a different nginx variable, and a regex for (at least) the map entry for HTML files:

map $upstream_http_content_type $expires2 {
         default                    off;
         ~text/html                  2d;
         text/plain                  20d;
         text/css                   180d;
}

location / {
        uwsgi_pass      django;
        expires $expires2;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .