cakephp – 如何使nginx缓存重写资产?

前端之家收集整理的这篇文章主要介绍了cakephp – 如何使nginx缓存重写资产?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用的是cakePHP1.3,有些资产是主题资产(js,img等),有些则不是主题资产.

主题资产正确缓存;但主题资产未获得正确的到期标头.我在位置块中使用重写来帮助查找主题资产,但由于某种原因,Nginx然后不应用正确的缓存头.

我在下面附上了我的Nginx配置的副本.有没有人解决过如何缓存重写资产?

@H_502_9@user Nginx; worker_processes 2; error_log /var/log/Nginx/error.log; pid /var/run/Nginx.pid; events { worker_connections 1024; } #----------------------------------------------------------------------------------------------------- http { include /etc/Nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/Nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; fastcgi_cache_path /var/cache/Nginx levels=1:2 keys_zone=my_cache:100m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; # Load modular configuration files from the /etc/Nginx/conf.d directory. # See http://Nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/Nginx/conf.d/*.conf; server { listen 80; server_name ag_production; access_log /var/log/Nginx/ag.access.log; error_log /var/log/Nginx/ag.error.log; rewrite_log on; root /var/www/html/app/webroot/; index index.PHP index.html index.htm; set $no_cache 0; location / { try_files $uri $uri/ @rules; } location @rules { rewrite ^(.+)$/index.PHP?url=$1 last; } # Pass the PHP scripts to FastCGI server # listening on 127.0.0.1:9000 location ~ \.PHP${ #try_files $uri =404; fastcgi_cache my_cache; fastcgi_cache_valid 200 60m; # Only cache 200 responses,cache for 60 minutes fastcgi_cache_methods GET HEAD; # Only GET and HEAD methods apply add_header X-Fastcgi-Cache $upstream_cache_status; fastcgi_cache_bypass $no_cache; # Don't pull from cache based on $no_cache fastcgi_no_cache $no_cache; # Don't save to cache based on $no_cache fastcgi_buffer_size 128k; fastcgi_buffers 256 4k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; # fastcgi_pass unix:/tmp/PHP-fastcgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.PHP; fastcgi_intercept_errors on; # to support 404s for PHP files not found fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~* \.(?:manifest|appcache|html?|xml|json)${ try_files $uri $uri/ @rules; expires -1; access_log logs/static.log; # I don't usually include a static log } # Feed location ~* \.(?:RSS|atom)${ try_files $uri $uri/ @rules; expires 1h; add_header Pragma "public"; add_header Cache-Control "public,must-revalidate,proxy-revalidate"; } # Media: images,icons,video,audio,HTC location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)${ expires 1M; access_log off; add_header Pragma "public"; add_header Cache-Control "public,proxy-revalidate"; try_files $uri $uri/ @rules; } # CSS and Javascript location ~* \.(?:css|js)${ try_files $uri $uri/ @rules; expires 1y; access_log off; add_header Pragma "public"; add_header Cache-Control "public,proxy-revalidate"; } # Deny access to .htaccess files,# git & svn repositories,etc location ~ /(\.ht|\.git|\.svn) { deny all; } } }
最佳答案
是的,CakePHP中的主题资产较慢,它在官方文档中也被描述为here.

解决方案是使用try_files通过Nginx提供这些主题资产.

以下是用于上述目的的示例Nginx配置:

@H_502_9@# Serve CakePHP plugin assets directly location ~ /(.+)/(img|css|js|files)/(.*) { access_log off; expires 10d; add_header Cache-Control public; try_files $uri $uri/ /../plugins/$1/webroot/$2/$3 /../../plugins/$1/webroot/$2/$3 /index.PHP?url=$uri; }

(source)

猜你在找的Nginx相关文章