Score:0

Nginx static images are not caching

mf flag

I have a Magento website that is running on apache2 and the frontend is in PWA and running on Nginx and I'm using Cloudflare. Now want to cache the images but it's always showing BYPASS. Apache configuration:

<VirtualHost 127.0.0.1:8081>
        SSLEngine On
        SSLCertificateFile /etc/nginx/ssl/cloudflare.crt
        SSLCertificateKeyFile /etc/nginx/ssl/cloudflare.key

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        ServerName stg.mydomain.com
        ServerAlias stg.mydomain.com
        Alias /backend "/var/www/backend/public_html"
        DocumentRoot "/var/www/backend/public_html"
        <Directory "/var/www/backend/public_html">
                DirectoryIndex index.php index.html index.htm
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>

</VirtualHost>

Nginx configuration:

server {
    listen 80;
    listen 443 ssl http2;
    ssl_certificate /etc/nginx/ssl/cloudflare.crt;
    ssl_certificate_key /etc/nginx/ssl/cloudflare.key;

    server_name stg.mydomain.com;   
    proxy_buffer_size          128k;
    proxy_buffers              4 256k;
    proxy_busy_buffers_size    256k;
    proxy_read_timeout 720s;
    proxy_connect_timeout 720s;
    proxy_send_timeout 720s;
        proxy_redirect        off; 
    location / {
     proxy_pass http://127.0.0.1:9000;
        }
    location /backend {
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass https://127.0.0.1:8081;  
    }   
}

I have added the following codes to cache the images on browsers

server {
    listen 80;
    listen 443 ssl http2;
    ssl_certificate /etc/nginx/ssl/cloudflare.crt;
    ssl_certificate_key /etc/nginx/ssl/cloudflare.key;

    server_name stg.mydomain.com;   
    proxy_buffer_size          128k;
    proxy_buffers              4 256k;
    proxy_busy_buffers_size    256k;
    proxy_read_timeout 720s;
    proxy_connect_timeout 720s;
    proxy_send_timeout 720s;
        proxy_redirect        off; 
    location / {
     proxy_pass http://127.0.0.1:9000;
        }
        location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|webp)$ {
      expires 1d;
        add_header Cache-Control "public";
    }
    location /backend { 
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass https://127.0.0.1:8081;  
    }   
}
jp flag
Please post all your configuration in text instead of screenshots.
jp flag
Where you want to cache the images? Nginx as in the title? Cloudflare as in the text?
Arunendra avatar
mf flag
@EsaJokinen I have updated the question please check, I want images should cache on user's browser
jp flag
You should follow the headers: what is Nginx really responding? If it is responding correctly, does Cloudflare drop those headers?
Score:1
za flag

First - it does not cache because you didn't tell nginx to cache anything: proxy_cache is set to off by default. You should define a proxy zone and use it accordingly.

Second - there's absolutely no point in handling static object through Apache. What you should do is to point all the static object directly to file system by adding something like

location ~*.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|webp)$ {
      expires @30d;
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.