I want to add the following cache control header codes in nginx configuration.
location ~* .(png|jpg|jpeg|gif)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
}
location ~* .(js|css|pdf|html|swf)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
I basically want to do this to eliminate the Google page speed warning: “Serve static assets with an efficient cache policy”
I would be extremely grateful if someone can tell me exactly where should I add the above codes in my existing nginx configuration?
My existing nginx configuration is as given below:
[root@serverl ~]# cat /etc/nginx/conf.d/users/homemadecircuits.conf proxy_cache_path
/var/cache/ea-nginx/proxy/homemadecircuits levels=1
:2 keys_zone=homemadecircuits:10m inactive=60m;
#### main domain for homemadecircuits ##
server {
server name homemade-circuits.com www.homemade-circuits.com mail .homemade-circuits.com 203.161.38.9;
listen 80;
listen [::]:80;
access_log /var/log/nginx/domains/homemade-circuits.com cp_combined;
access log /var/log/nginx/domains/homemade-circuits.com-bytes log cp_bytes_server;
include conf.d/includes-optional/cloudflare.conf;
set $CPANEL APACHE PROXY PASS $scheme://apache backend ${scheme} 203 161 38 9;
# For includes:
set $CPANEL APACHE PROXY IP 203.161.38.9;
set $CPANEL APACHE PROXY SSL IP 203.161.38.9;
set $CPANEL SERVICE SUBDOMAIN 0;
set $CPANEL PROXY CACHE homemadecircuits;
set $CPANEL SKIP PROXY CACHING 0;
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
ssl certificate /var/cpanel/ssl/apache tls/homemade-circuits.com /combined;
ssl_protocols TLSv1.2 TLSv1.3; proxy_ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-S
HA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDH
E-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305: DHE-RSA-AES128 -GCM-SHA256:DHE-RSA-AES256-GCM-SHA384: TLS AES 256 GCM SHA384:TLS CHA CHA20 POLY1305 SHA256: TLS AES 128 GCM SHA256;
proxy_ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128
-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA38
4:ECDHE-ECDSA-CHACHA20-POLY1305: ECDHE-RSA-CHACHA20-POLY1305: DHE-RSA- AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384: TLS AES 256 GCM SHA384:T
LS CHACHA20 POLY1305 SHA256: TLS AES 128 GCM SHA256;
root "/home/homemadecircuits/public_html";
location /cpanelwebcall {
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass http://127.0.0.1:2082/cpanelwebcall;
}
location /Microsoft-Server-ActiveSync{
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass http://127.0.0.1:2090/Microsoft-Server-ActiveSync;
}
location = /favicon.ico {
allow all;
log_not_found off;
access log off;
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL_APACHE PROXY PASS;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL APACHE_PROXY PASS;
}
location / {
proxy_cache $CPANEL PROXY CACHE;
proxy_no_cache $CPANEL_SKIP_PROXY_CACHING;
proxy_cache_bypass $CPANEL_SKIP_PROXY_CACHING;
proxy_cache_valid 200 301 302 60m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout http_429 http_500_http_5 02 http_503 http_504;
}
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_min uses 1;
proxy_cache_lock off;
add_header X-Cache $upstream_cache_status;
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL APACHE PROXY PASS;
}
include conf.d/server-includes/*.conf;
include conf.d/users/homemadecircuits/*.conf;
include conf.d/users/homemadecircuits/homemade-circuits.com/*.conf
}
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
ssl_certificate /var/cpanel/ssl/apache_tls/homemade-circuits.com
/combined;
ssl_certificate_key /var/cpanel/ssl/apache_tls/homemade-circuits
.com/combined;
server_name cpanel.homemade-circuits.com cpcalendars.homemade-c ircuits.com cpcontacts.homemade-circuits.com webdisk.homemade-circui ts.com webmail.homemade-circuits.com;
include conf.d/includes-optional/cloudflare.conf;
set $CPANEL APACHE_PROXY_PASS $scheme://apache_backend_${scheme } 203 161 38 9;
# For includes:
set $CPANEL APACHE PROXY IP 203.161.38.9;
set $CPANEL APACHE PROXY SSL IP 203.161.38.9;
set $CPANEL SERVICE SUBDOMAIN 1;
location /.well-known/cpanel-dcv {
root "/home/homemadecircuits/public_html"; disable symlinks if_not_owner;
# pass to Apache
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL_APACHE_PROXY PASS;
}
location /.well-known/pki-validation { root "/home/homemadecircuits/public_html"; disable_symlinks if_not_owner;
# pass to Apache
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL APACHE PROXY PASS;
}
location /.well-known/acme-challenge {
root "/home/homemadecircuits/public_html"; disable_symlinks if_not_owner;
# pass to Apache
include conf.d/includes-optional/cpanel-proxy.conf;
proxy_pass $CPANEL APACHE PROXY PASS;
}
location / {
# Force https for service subdomains
if ($scheme = http) {
return 301 https://$host$request_uri;
}
# no cache
proxy_cache off;
proxy_no_cache 1;
proxy_cache_bypass 1;
# pass to Apache
include conf.d/includes-optional/cpanel-proxy.conf; proxy_pass $CPANEL APACHE PROXY PASS;
I want to add browser caching code in my nginx configuration so that I can eliminate the google page speed warning: “Serve static assets with an efficient cache policy”