NGINX Leverage browser caching

IAMwebdesign

New member
Joined
Dec 5, 2017
Messages
4
Hi All,

I'm running NGINX + Apache and trying to get leverage browser caching working.
Somehow the guides I found just don't do what they are supposed to do ;)

Could anyone explain me a bit?

Regards!
 
Do you need it to be in nginx or can you use apache since you are running nginx + apache? I use mine in a .htaccess file.
Code:
 # Time cheat sheet in seconds
# A86400 = 1 day
# A172800 = 2 days
# A2419200 = 1 month
# A4838400 = 2 months
# A29030400 = 1 year
# Remove the ETag (entity tag) response header field
# This is most likely the optimum choice to use.
Header unset ETag
FileETag none

<IfModule mod_expires.c>
ExpiresActive on
# ExpiresByType overrides the ExpiresDefault...
# cache expiration time of 2 days|A172800.
ExpiresDefault A172800
ExpiresByType image/jpg A4838400
ExpiresByType image/jpeg A4838400
ExpiresByType image/gif A4838400
ExpiresByType image/png A4838400
ExpiresByType image/bmp A4838400
ExpiresByType image/x-icon A4838400
ExpiresByType image/svg+xml A4838400
ExpiresByType text/javascript A4838400
ExpiresByType text/x-javascript A4838400 
ExpiresByType text/css A4838400
ExpiresByType text/html A4838400
ExpiresByType application/x-font-ttf A4838400
ExpiresByType application/x-font-woff A4838400
ExpiresByType font/opentype A4838400
ExpiresByType application/x-shockwave-flash A4838400
ExpiresByType application/x-javascript A4838400
ExpiresByType application/javascript A4838400
ExpiresByType video/mp4 A4838400
ExpiresByType video/ogg A4838400
ExpiresByType video/webm A4838400
</IfModule>

<IfModule mod_headers.c>
<FilesMatch "\.(js|css|flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|gif|jpg|jpeg|png|swf|webm)$">
Header append Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(txt|html)$">
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
<FilesMatch "\.(php|cgi|pl|htm|xml)$">
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>
</IfModule>

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css text/javascript
AddOutputFilterByType DEFLATE application/javascript application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/xml-dtd
AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE font/otf font/opentype application/font-otf application/x-font-otf
AddOutputFilterByType DEFLATE font/ttf font/truetype application/font-ttf application/x-font-ttf
AddOutputFilterByType DEFLATE image/svg+xml

# Drop problematic browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
 
Can you please explain a little more ? What I am using passes all browser cache tests I have found.

Is it the mod_headers part ?

I'm confused !
 
Use the link which I posted in order to see how to use the ngx_http_headers_module module of Nginx which allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.

The example section from the linked page shows a simple usage of the directive which you need to add either into nginx templates or in a custom httpd.conf section of Nginx for a particular domain.
 
Hate to be a bother, I'm new to Nginx and learning curve is large.

The nginx.ru pages are very hard to understand for me, they seem to expect I know a whole lot more than I do.

Please review this and tell me if its correct.

My /etc/nginx/nginx-vhosts.conf
Code:
location / {
                access_log off;
                proxy_pass http://xxx.xxx.xxx.xxx:8080;
                proxy_set_header X-Client-IP      $remote_addr;
                proxy_set_header X-Accel-Internal /nginx_static_files;
                proxy_set_header Host             $host;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_hide_header Upgrade;
                if ($request_uri ~* ".(?:ico|css|js|gif|jpe?g|png)\?[0-9]+$") {
                   expires 30d;
                   access_log off;
                   add_header Cache-Control "private";
                   break;
                   }

        }

        location /nginx_static_files/ {
                access_log  /var/log/nginx/access_log_proxy;
                alias       /var/www/html/;
                internal;
                expires 1M;
                access_log off;
                add_header Cache-Control "private";
        }
and
Code:
location / {
                access_log off;
                proxy_pass https://xxx.xxx.xxx.xxx:8081;
                proxy_set_header X-Client-IP      $remote_addr;
                proxy_set_header X-Accel-Internal /nginx_static_files;
                proxy_set_header Host             $host;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_hide_header Upgrade;
                if ($request_uri ~* ".(?:ico|css|js|gif|jpe?g|png)\?[0-9]+$") {
                   expires 30d;
                   access_log off;
                   add_header Cache-Control "private";
                   break;
                   }
        }

        location /nginx_static_files/ {
                access_log  /var/log/nginx/access_log_proxy;
                alias       /var/www/html/;
                internal;
                expires 1M;
                access_log off;
                add_header Cache-Control "private";
        }

Is this anywhere near correct ?
 
I'd rather remove

Code:
                if ($request_uri ~* ".(?:ico|css|js|gif|jpe?g|png)\?[0-9]+$") {
                   expires 30d;
                   access_log off;
                   add_header Cache-Control "private";
                   break;
                   }
 
Back
Top