Hello.

There is a need to cache data that is taken from external resources, namely https://maps.googleapis.com/ and https://www.google-analytics.com :

You can do this:

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 24h; } 

but the application static and media files are in ../static and ../media

and now I'm doing something like:

 location /static { root /var/www/project/static; gzip on; gzip_min_length 256; gzip_proxied expired no-cache no-store private auth; gzip_types *; gzip_comp_level 9; expires 15d; } 

and about the same for media

But if you use the first option, then the one that does not work with root and static does not work.

How to implement it correctly?

Thank you in advance!

    1 answer 1

    As a result, the configuration is as follows:

     proxy_cache_path /var/lib/nginx/cache levels=2:2 keys_zone=cache:30m max_size=1G; proxy_temp_path /var/lib/nginx/proxy 1 2; proxy_ignore_headers Expires Cache-Control; proxy_cache_use_stale error timeout invalid_header http_502; proxy_cache_bypass $cookie_session; proxy_no_cache $cookie_session; upstream project_server { server unix:/var/www/project/project.sock fail_timeout=0; } map $sent_http_content_type $expires { default off; text/html epoch; text/css max; application/javascript max; ~image/ max; } server { server_name example.com; listen 80; return 301 https:example.com--90ais$request_uri; } server { server_name xn------dddfnxoenlfghchl4bitc.xn--90ais; root /var/www/project/; listen 443 ssl http2; # <- expires $expires; ssl on; # <- ssl_certificate /var/www/project/certificate.crt; # <- ssl_certificate_key /var/www/project/private.key; # <- client_max_body_size 4G; access_log /var/log/nginx/project-access.log; error_log /var/log/nginx/project-error.log; location / { proxy_pass_header X-CSRFToken; proxy_cache cache; proxy_cache_valid 10m; proxy_cache_valid 404 1m; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; # <- proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://project_server; break; } } location /static { alias /var/www/project/static; etag on; if_modified_since exact; gzip on; gzip_min_length 1; gzip_proxied expired no-cache no-store private auth; gzip_types *; gzip_comp_level 9; gzip_vary on; expires 15d; gzip_buffers 16 8k; gzip_http_version 1.1; } location /media { alias /var/www/project/media; etag on; if_modified_since exact; gzip on; gzip_min_length 1; gzip_proxied expired no-cache no-store private auth; gzip_types *; gzip_comp_level 9; gzip_vary on; expires 15d; gzip_buffers 16 8k; gzip_http_version 1.1; } location /robots.txt { root /var/www/project; } } 

    But it is not clear why PageSpeed ​​Insights continues to swear at the Leverage browser caching regarding https://maps.googleapis.com/ and https://www.google-analytics.com .