php – Nginx快速CGI缓存ON error_page 404

前端之家收集整理的这篇文章主要介绍了php – Nginx快速CGI缓存ON error_page 404前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想缓存(fast_cgi)404响应.

  1. error_page 404 = /url_rewriting.PHP;

在我的url_rewriting.PHP中,我使用PHP生成图像:

  1. if(strpos($_SERVER['REQUEST_URI'],'render/framed/file') !== FALSE) {
  2. $urlBlocks = ['VR','sizePixels','image','ver','frame','borderSize','mat','matSize','maxSize','frameGlass','minSize'];
  3. foreach($urlBlocks as $oneBlock) {
  4. if($pos = array_search($oneBlock,$urlParts)) {
  5. if(isset($urlParts[($pos+1)]) && $urlParts[($pos+1)] != '') {
  6. $_GET[$oneBlock] = urldecode($urlParts[($pos+1)]);
  7. }
  8. }
  9. }
  10. chdir('include/PHP/render/framed');
  11. header('Status: 200 OK',false,200);
  12. require ('include/PHP/render/framed/render_img.PHP');
  13. }

通过这种方式,我可以在HTML中拥有图像src URL,如下所示:

https://mywebsite.com/include/PHP/render/framed/file/VR/1/size/300/image/U3dpwK/the-cat.jpg

哪个jpg文件不存在但由PHP生成.

但我没有找到任何方法Nginx缓存这些PHP生成的图像.

我试过这个:

  1. set $no_cache 0;
  2. location ~ /render/ {
  3. include snippets/fastcgi-PHP.conf;
  4. #fastcgi_pass unix:/run/PHP/PHP7.0-fpm.sock;
  5. fastcgi_pass 127.0.0.1:9000;
  6. fastcgi_buffers 8 16k; # increase the buffer size for PHP-FTP
  7. fastcgi_buffer_size 32k; # increase the buffer size for PHP-FTP
  8. fastcgi_cache_key $scheme$host$request_uri$request_method;
  9. fastcgi_cache PROD;
  10. fastcgi_cache_valid any 20d;
  11. fastcgi_cache_valid 404 20d;
  12. fastcgi_cache_use_stale updating error timeout invalid_header http_500 http_503;
  13. fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
  14. fastcgi_hide_header "Set-Cookie";
  15. fastcgi_cache_bypass $no_cache;
  16. fastcgi_no_cache $no_cache;
  17. expires 10M;
  18. access_log off;
  19. add_header Cache-Control "public";
  20. add_header X-Cache-Status $upstream_cache_status;
  21. }

它适用于图像URL

  1. https://mywebsite.com/include/PHP/render/framed/img.PHP?VR=1&size=300&image=U3pmwKi

但不适用于图片网址

  1. https://mywebsite.com/include/PHP/render/framed/file/VR/1/size/300/image/U3dpwK/the-cat.jpg

然而我把fastcgi_cache_valid 404 20d;

那么,如何缓存mys_page上的脚本404 = /url_rewriting.PHP;?

编辑

这是2卷曲-I输出

非工作URL(用于缓存)

  1. curl -I "https://mywebsite.com/include/PHP/render/framed/file/VR/1/sizePixels/300/image/SzDuehqyda%3D/ver//frame/black-e91-2/borderSize/1.70/mat/zkadhtcoz/matSize/10/maxSize/800/minSize/600/freedom.jpg"
  2. HTTP/1.1 200 OK
  3. Server: Nginx/1.14.0 (Ubuntu)
  4. Date: Fri,04 May 2018 14:59:24 GMT
  5. Content-Type: image/jpeg
  6. Connection: keep-alive
  7. Set-Cookie: Mywebsite=vmegg0qk7udtmkmcathd329kkp; expires=Sun,03-Jun-2018 14:59:24 GMT; Max-Age=2592000; path=/
  8. Cache-Control: private,max-age=31536000,pre-check=31536000
  9. Pragma: private
  10. Last-Modified: Sat,01 Apr 2000 13:13:45 GMT
  11. Content-transfer-encoding: binary
  12. Expires: Sat,08 Jun 19 15:59:24 +0100
  13. Strict-Transport-Security: max-age=31536000

工作URL(用于缓存)

  1. curl -I "https://mywebsite.com/include/PHP/render/framed/render_img.PHP?VR=1&sizePixels=360&image=SzDuehqyda%3D&ver=&frame=black-e91-2&borderSize=1.70&mat=zkadhtcoz&matSize=10&maxSize=800&minSize=600"
  2. HTTP/1.1 200 OK
  3. Server: Nginx/1.14.0 (Ubuntu)
  4. Date: Fri,04 May 2018 14:59:59 GMT
  5. Content-Type: image/jpeg
  6. Connection: keep-alive
  7. Cache-Control: max-age=25920000
  8. Pragma: private
  9. Last-Modified: Sat,01 Apr 2000 13:13:45 GMT
  10. Content-transfer-encoding: binary
  11. Expires: Thu,28 Feb 2019 14:59:59 GMT
  12. Cache-Control: public
  13. X-Cache-Status: HIT

EDIT2

输出Nginx -t

  1. root@mywebsite-london-01:/var/www/mywebsite.com/prod# Nginx -T
  2. Nginx: the configuration file /etc/Nginx/Nginx.conf Syntax is ok
  3. Nginx: configuration file /etc/Nginx/Nginx.conf test is successful
  4. # configuration file /etc/Nginx/Nginx.conf:
  5. user www-data;
  6. worker_processes auto;
  7. pid /run/Nginx.pid;
  8. include /etc/Nginx/modules-enabled/*.conf;
  9. events {
  10. worker_connections 768;
  11. # multi_accept on;
  12. }
  13. http {
  14. ##
  15. # Adding GeoIP for Matomo
  16. ##
  17. geoip_country /var/www/geoip/GeoIP.dat;
  18. geoip_city /var/www/geoip/GeoLiteCity.dat;
  19. # Max File upload
  20. client_max_body_size 100m;
  21. ##
  22. # Basic Settings
  23. ##
  24. sendfile on;
  25. tcp_nopush on;
  26. tcp_nodelay on;
  27. keepalive_timeout 65;
  28. types_hash_max_size 2048;
  29. # server_tokens off;
  30. # server_names_hash_bucket_size 64;
  31. # server_name_in_redirect off;
  32. include /etc/Nginx/mime.types;
  33. default_type application/octet-stream;
  34. ##
  35. # Logging Settings
  36. ##
  37. log_format compression '$remote_addr - $remote_user [$time_local] '
  38. '"$request" $status $bytes_sent '
  39. '"$http_referer" "$http_user_agent" "$gzip_ratio"';
  40. access_log /var/log/Nginx/access.log;
  41. error_log /var/log/Nginx/error.log;
  42. ##
  43. # Gzip Settings
  44. ##
  45. # gzip on;
  46. gzip on;
  47. gzip_disable "msie6";
  48. gzip_vary on;
  49. gzip_proxied any;
  50. gzip_comp_level 6;
  51. gzip_min_length 1100;
  52. gzip_buffers 16 8k;
  53. gzip_http_version 1.1;
  54. gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+RSS text/javascript;
  55. ##
  56. # Virtual Host Configs
  57. ##
  58. include /etc/Nginx/conf.d/*.conf;
  59. include /etc/Nginx/sites-enabled/*;
  60. }
  61. # configuration file /etc/Nginx/modules-enabled/50-mod-http-geoip.conf:
  62. load_module modules/ngx_http_geoip_module.so;
  63. # configuration file /etc/Nginx/modules-enabled/50-mod-http-image-filter.conf:
  64. load_module modules/ngx_http_image_filter_module.so;
  65. # configuration file /etc/Nginx/modules-enabled/50-mod-http-xslt-filter.conf:
  66. load_module modules/ngx_http_xslt_filter_module.so;
  67. # configuration file /etc/Nginx/modules-enabled/50-mod-mail.conf:
  68. load_module modules/ngx_mail_module.so;
  69. # configuration file /etc/Nginx/modules-enabled/50-mod-stream.conf:
  70. load_module modules/ngx_stream_module.so;
  71. # configuration file /etc/Nginx/mime.types:
  72. types {
  73. text/html html htm shtml;
  74. text/css css;
  75. text/xml xml;
  76. image/gif gif;
  77. image/jpeg jpeg jpg;
  78. application/javascript js;
  79. application/atom+xml atom;
  80. application/RSS+xml RSS;
  81. text/mathml mml;
  82. text/plain txt;
  83. text/vnd.sun.j2me.app-descriptor jad;
  84. text/vnd.wap.wml wml;
  85. text/x-component htc;
  86. image/png png;
  87. image/tiff tif tiff;
  88. image/vnd.wap.wbmp wbmp;
  89. image/x-icon ico;
  90. image/x-jng jng;
  91. image/x-ms-bmp bmp;
  92. image/svg+xml svg svgz;
  93. image/webp webp;
  94. application/font-woff woff;
  95. application/java-archive jar war ear;
  96. application/json json;
  97. application/mac-binhex40 hqx;
  98. application/msword doc;
  99. application/pdf pdf;
  100. application/postscript ps eps ai;
  101. application/rtf rtf;
  102. application/vnd.apple.mpegurl m3u8;
  103. application/vnd.ms-excel xls;
  104. application/vnd.ms-fontobject eot;
  105. application/vnd.ms-powerpoint ppt;
  106. application/vnd.wap.wmlc wmlc;
  107. application/vnd.google-earth.kml+xml kml;
  108. application/vnd.google-earth.kmz kmz;
  109. application/x-7z-compressed 7z;
  110. application/x-cocoa cco;
  111. application/x-java-archive-diff jardiff;
  112. application/x-java-jnlp-file jnlp;
  113. application/x-makeself run;
  114. application/x-perl pl pm;
  115. application/x-pilot prc pdb;
  116. application/x-rar-compressed rar;
  117. application/x-redhat-package-manager rpm;
  118. application/x-sea sea;
  119. application/x-shockwave-flash swf;
  120. application/x-stuffit sit;
  121. application/x-tcl tcl tk;
  122. application/x-x509-ca-cert der pem crt;
  123. application/x-xpinstall xpi;
  124. application/xhtml+xml xhtml;
  125. application/xspf+xml xspf;
  126. application/zip zip;
  127. application/octet-stream bin exe dll;
  128. application/octet-stream deb;
  129. application/octet-stream dmg;
  130. application/octet-stream iso img;
  131. application/octet-stream msi msp msm;
  132. application/vnd.openxmlformats-officedocument.wordprocessingml.document docx;
  133. application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx;
  134. application/vnd.openxmlformats-officedocument.presentationml.presentation pptx;
  135. audio/midi mid midi kar;
  136. audio/mpeg mp3;
  137. audio/ogg ogg;
  138. audio/x-m4a m4a;
  139. audio/x-realaudio ra;
  140. video/3gpp 3gpp 3gp;
  141. video/mp2t ts;
  142. video/mp4 mp4;
  143. video/mpeg mpeg mpg;
  144. video/quicktime mov;
  145. video/webm webm;
  146. video/x-flv flv;
  147. video/x-m4v m4v;
  148. video/x-mng mng;
  149. video/x-ms-asf asx asf;
  150. video/x-ms-wmv wmv;
  151. video/x-msvideo avi;
  152. }
  153. # CACHE (Images rendering)
  154. fastcgi_cache_path /var/www/cache/l7 levels=1:2 keys_zone=L7:1m max_size=100m inactive=20d;
  155. fastcgi_cache_path /var/www/cache/prod levels=1:2 keys_zone=PROD:100m max_size=10000m inactive=30d; # For PROD
  156. add_header X-Cache $upstream_cache_status; #To check what is that for
  157. # SSL
  158. ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
  159. ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
  160. ssl_session_cache shared:SSL:20m;
  161. ssl_session_timeout 180m;
  162. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  163. ssl_prefer_server_ciphers on;
  164. ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DHE+AES128:!ADH:!AECDH:!MD5;
  165. ssl_dhparam /etc/Nginx/cert/dhparam.pem;
  166. add_header Strict-Transport-Security "max-age=31536000" always;
  167. server {
  168. listen [::]:443 ssl ipv6only=on; # managed by Certbot
  169. listen 443 ssl; # managed by Certbot
  170. server_name mywebsite.com www.mywebsite.com l7.mywebsite.com cdn.mywebsite.com dev.mywebsite.com;
  171. set $rootfolder "prod";
  172. set $ask_auth "Restricted Area";
  173. if ($host ~ "l7.mywebsite.com") {
  174. set $rootfolder "l7";
  175. set $ask_auth off;
  176. }
  177. root /var/www/mywebsite.com/$rootfolder;
  178. access_log /var/log/Nginx/mywebsite.com-access.log compression buffer=32k;
  179. error_log /var/log/Nginx/mywebsite.com-error.log;
  180. # Add index.PHP to the list if you are using PHP
  181. index index.PHP index.html index.htm index.Nginx-debian.html;
  182. error_page 404 = /url_rewriting.PHP;
  183. set $no_cache 1;
  184. if ($request_uri ~* "render_img.PHP") {
  185. set $no_cache 0;
  186. }
  187. location / {
  188. # First attempt to serve request as file,then
  189. # as directory,then fall back to displaying a 404.
  190. try_files $uri $uri/ =404;
  191. #auth_basic $ask_auth;
  192. #auth_basic_user_file .htpasswd;
  193. }
  194. set $no_cache 0;
  195. #location ~ render_img.PHP {
  196. location ~ /render\/framed/ {
  197. include snippets/fastcgi-PHP.conf;
  198. #fastcgi_pass unix:/run/PHP/PHP7.0-fpm.sock;
  199. fastcgi_pass 127.0.0.1:9000;
  200. fastcgi_buffers 8 16k; # increase the buffer size for PHP-FTP
  201. fastcgi_buffer_size 32k; # increase the buffer size for PHP-FTP
  202. fastcgi_cache_key $scheme$host$request_uri$request_method;
  203. fastcgi_cache PROD;
  204. fastcgi_cache_valid any 20d;
  205. fastcgi_cache_valid 404 1d;
  206. #fastcgi_cache_valid any 20d;
  207. #fastcgi_cache_use_stale updating error timeout invalid_header http_500 http_503 http_404;
  208. fastcgi_cache_use_stale updating error timeout invalid_header http_500 http_503;
  209. fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
  210. fastcgi_hide_header "Set-Cookie";
  211. fastcgi_cache_bypass $no_cache;
  212. fastcgi_no_cache $no_cache;
  213. expires 10M;
  214. access_log off;
  215. add_header Cache-Control "public";
  216. add_header X-Cache-Status $upstream_cache_status;
  217. }
  218. location ~ \.PHP${
  219. include snippets/fastcgi-PHP.conf;
  220. #fastcgi_pass unix:/run/PHP/PHP7.0-fpm.sock;
  221. fastcgi_pass 127.0.0.1:9000;
  222. fastcgi_buffers 8 16k; # increase the buffer size for PHP-FTP
  223. fastcgi_buffer_size 32k; # increase the buffer size for PHP-FTP
  224. fastcgi_param GEOIP_ADDR $remote_addr;
  225. fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
  226. fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
  227. fastcgi_param GEOIP_REGION $geoip_region;
  228. fastcgi_param GEOIP_REGION_NAME $geoip_region_name;
  229. fastcgi_param GEOIP_CITY $geoip_city;
  230. fastcgi_param GEOIP_AREA_CODE $geoip_area_code;
  231. fastcgi_param GEOIP_LATITUDE $geoip_latitude;
  232. fastcgi_param GEOIP_LONGITUDE $geoip_longitude;
  233. fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
  234. if ($rootfolder = "l7") {
  235. access_log /var/log/Nginx/mywebsite_l7.com-access.log;
  236. }
  237. }
  238. location ~ /\.ht {
  239. deny all;
  240. }
  241. }
  242. server {
  243. if ($host = dev.mywebsite.com) {
  244. return 301 https://$host$request_uri;
  245. } # managed by Certbot
  246. if ($host = cdn.mywebsite.com) {
  247. return 301 https://$host$request_uri;
  248. } # managed by Certbot
  249. if ($host = l7.mywebsite.com) {
  250. return 301 https://$host$request_uri;
  251. } # managed by Certbot
  252. if ($host = www.mywebsite.com) {
  253. return 301 https://$host$request_uri;
  254. } # managed by Certbot
  255. if ($host = mywebsite.com) {
  256. return 301 https://$host$request_uri;
  257. } # managed by Certbot
  258. listen 80 default_server;
  259. listen [::]:80 default_server;
  260. server_name mywebsite.com www.mywebsite.com l7.mywebsite.com cdn.mywebsite.com dev.mywebsite.com;
  261. return 404; # managed by Certbot
  262. }
  263. # configuration file /etc/Nginx/snippets/fastcgi-PHP.conf:
  264. # regex to split $uri to $fastcgi_script_name and $fastcgi_path
  265. fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
  266. # Check that the PHP script exists before passing it
  267. try_files $fastcgi_script_name =404;
  268. # Bypass the fact that try_files resets $fastcgi_path_info
  269. # see: http://trac.Nginx.org/Nginx/ticket/321
  270. set $path_info $fastcgi_path_info;
  271. fastcgi_param PATH_INFO $path_info;
  272. fastcgi_index index.PHP;
  273. include fastcgi.conf;
  274. # configuration file /etc/Nginx/fastcgi.conf:
  275. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  276. fastcgi_param QUERY_STRING $query_string;
  277. fastcgi_param REQUEST_METHOD $request_method;
  278. fastcgi_param CONTENT_TYPE $content_type;
  279. fastcgi_param CONTENT_LENGTH $content_length;
  280. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  281. fastcgi_param REQUEST_URI $request_uri;
  282. fastcgi_param DOCUMENT_URI $document_uri;
  283. fastcgi_param DOCUMENT_ROOT $document_root;
  284. fastcgi_param SERVER_PROTOCOL $server_protocol;
  285. fastcgi_param REQUEST_SCHEME $scheme;
  286. fastcgi_param HTTPS $https if_not_empty;
  287. fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  288. fastcgi_param SERVER_SOFTWARE Nginx/$Nginx_version;
  289. fastcgi_param REMOTE_ADDR $remote_addr;
  290. fastcgi_param REMOTE_PORT $remote_port;
  291. fastcgi_param SERVER_ADDR $server_addr;
  292. fastcgi_param SERVER_PORT $server_port;
  293. fastcgi_param SERVER_NAME $server_name;
  294. # PHP only,required if PHP was built with --enable-force-cgi-redirect
  295. fastcgi_param REDIRECT_STATUS 200;

正如Tarun Lalwani所说,块优先级和顺序很重要.但是,当你想缓存404响应时,你必须在error_page的文件名上检查它:

  1. location ~ (render_img.PHP|^/url_rewriting.PHP$) {
  2. fastcgi_cache...stuffs to cache...
  3. }

我们无法检查位置^〜/ render / framed / {因为它是对应于/url_rewriting.PHP404响应.

最佳答案
您的问题是,在该位置调用.PHP时,您需要优先考虑.PHP块而不是/ render / framed /.它仍然被.PHP块捕获

因此,您需要在下面使用您的位置块.你需要使用

  1. location ^~ /render/framed/ {
  2. ...
  3. }

有关详细信息,请参阅

Nginx location priority

猜你在找的Nginx相关文章