I like add cache control header with nginx for some extensions such as .jpg, etc but so far some of the solutions I found on the net, I couldn't get it to work. I will tell you what I have tried.
I have tried variations of the following in different place in the .conf file of my site and when I tried the site become blank and I found a lot of 404 errors on the console. The site is developed in React.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1d;
add_header Cache-Control "public, no-transform";
}
My conf file looks like the following. The thing is I have to do reverse proxy as the sites are actually hosted in Docker containers.
server {
server_name mysite.net;
root /usr/share/nginx/html;
location / {
proxy_pass http://web:3005/;
}
location /api/ {
rewrite ^/api(/.*)$ $1 break;
proxy_pass http://api:5005/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
fastcgi_read_timeout 1200;
proxy_read_timeout 1200;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mysite.net-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mysite.net-0001/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = mysite.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name mysite.net;
return 404; # managed by Certbot
}