'Unable to set Cache-Control in NGINX Ubuntu

I have a website on an EC2 instance with NGINX and Ubuntu 18.0.4. I have setup the server block for the site the site is loading correctly. I have also enabled caching for images, javascript and css files.

I want to exclude certain paths from getting cached and I tried some config examples available on the internet. None of them are working and I am getting a 404 error for the locations that I am trying to exclude.

My server block is as follows:

# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/html                  epoch;
    text/css                   max;
    application/javascript     max;
    ~image/                    max;
    ~font/                     max;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    expires $expires;

 location /path1/ {
    add_header Cache-Control "private";
 }

 location /path2/subpath1/ {
    add_header Cache-Control "private";
 }

 location /path2/subpath2/ {
    add_header Cache-Control "private";
 }

 location /path2/subpath3/ {
    add_header Cache-Control "private";
 }
}

The paths (path1, path2) which I have tried to exclude from caching are returning a 404 not found error. Can someone please help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source