<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Hello,</p>
    <p>I would like to cache (PHP fast_cgi) 404 response.</p>
    <pre class="lang-php prettyprint prettyprinted" style=""><code><span class="pln">error_page </span><span class="lit">404</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">/</span><span class="pln">url_rewriting</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span></code></pre>
    <p>In my <code>url_rewriting.php</code> I generate image by
      requiring the<code><span class="pln"> render_img.php</span></code>
      file:</p>
    <pre class="lang-php prettyprint prettyprinted" style=""><code><span class="kwd">if</span><span class="pun">(</span><span class="pln">strpos</span><span class="pun">(</span><span class="pln">$_SERVER</span><span class="pun">[</span><span class="str">'REQUEST_URI'</span><span class="pun">],</span><span class="pln"> </span><span class="str">'render/framed/file'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">!==</span><span class="pln"> FALSE</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    $urlBlocks </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">'VR'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'sizePixels'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'image'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'ver'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'frame'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'borderSize'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'mat'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'matSize'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'maxSize'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'frameGlass'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'minSize'</span><span class="pun">];</span><span class="pln">
    </span><span class="kwd">foreach</span><span class="pun">(</span><span class="pln">$urlBlocks </span><span class="kwd">as</span><span class="pln"> $oneBlock</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">$pos </span><span class="pun">=</span><span class="pln"> array_search</span><span class="pun">(</span><span class="pln">$oneBlock</span><span class="pun">,</span><span class="pln"> $urlParts</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">isset</span><span class="pun">(</span><span class="pln">$urlParts</span><span class="pun">[(</span><span class="pln">$pos</span><span class="pun">+</span><span class="lit">1</span><span class="pun">)])</span><span class="pln"> </span><span class="pun">&&</span><span class="pln"> $urlParts</span><span class="pun">[(</span><span class="pln">$pos</span><span class="pun">+</span><span class="lit">1</span><span class="pun">)]</span><span class="pln"> </span><span class="pun">!=</span><span class="pln"> </span><span class="str">''</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
                $_GET</span><span class="pun">[</span><span class="pln">$oneBlock</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> urldecode</span><span class="pun">(</span><span class="pln">$urlParts</span><span class="pun">[(</span><span class="pln">$pos</span><span class="pun">+</span><span class="lit">1</span><span class="pun">)]);</span><span class="pln">
            </span><span class="pun">}</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">    
    </span><span class="pun">}</span><span class="pln">

    chdir</span><span class="pun">(</span><span class="str">'include/php/render/framed'</span><span class="pun">);</span><span class="pln">
    header</span><span class="pun">(</span><span class="str">'Status: 200 OK'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">,</span><span class="pln"> </span><span class="lit">200</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">require</span><span class="pln"> </span><span class="pun">(</span><span class="str">'include/php/render/framed/render_img.php'</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}

</span></code></pre>
    <p>By this way, I can have image src URL in the HTML like this : <code><a class="moz-txt-link-freetext" href="https://mywebsite.com/include/php/render/framed/file/VR/1/size/300/image/U3dpwK/the-cat.jpg">https://mywebsite.com/include/php/render/framed/file/VR/1/size/300/image/U3dpwK/the-cat.jpg</a></code>
      which does not exist but is generated by PHP.</p>
    <p>But I didn't find any way with Nginx to cache theses PHP
      generated images.</p>
    <p>I tried this :</p>
    <pre class="lang-php prettyprint prettyprinted" style=""><code><span class="kwd">set</span><span class="pln"> $no_cache </span><span class="lit">0</span><span class="pun">;</span><span class="pln">

location </span><span class="pun">~</span><span class="pln"> </span><span class="str">/render/</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        include snippets</span><span class="pun">/</span><span class="pln">fastcgi</span><span class="pun">-</span><span class="pln">php</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">;</span><span class="pln">
        </span><span class="com">#fastcgi_pass unix:/run/php/php7.0-fpm.sock;</span><span class="pln">
        fastcgi_pass </span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pun">:</span><span class="lit">9000</span><span class="pun">;</span><span class="pln">

        fastcgi_buffers </span><span class="lit">8</span><span class="pln"> </span><span class="lit">16k</span><span class="pun">;</span><span class="pln"> </span><span class="com"># increase the buffer size for PHP-FTP</span><span class="pln">
        fastcgi_buffer_size </span><span class="lit">32k</span><span class="pun">;</span><span class="pln"> </span><span class="com"># increase the buffer size for PHP-FTP</span><span class="pln">
        fastcgi_cache_key $scheme$host$request_uri$request_method</span><span class="pun">;</span><span class="pln">
        fastcgi_cache PROD</span><span class="pun">;</span><span class="pln">
        fastcgi_cache_valid any </span><span class="lit">20d</span><span class="pun">;</span><span class="pln">
        fastcgi_cache_valid </span><span class="lit">404</span><span class="pln"> </span><span class="lit">20d</span><span class="pun">;</span><span class="pln">
        fastcgi_cache_use_stale updating error timeout invalid_header http_500 http_503</span><span class="pun">;</span><span class="pln">
        fastcgi_ignore_headers </span><span class="typ">Cache</span><span class="pun">-</span><span class="typ">Control</span><span class="pln"> </span><span class="typ">Expires</span><span class="pln"> </span><span class="typ">Set</span><span class="pun">-</span><span class="typ">Cookie</span><span class="pun">;</span><span class="pln">
        fastcgi_hide_header </span><span class="str">"Set-Cookie"</span><span class="pun">;</span><span class="pln">
        fastcgi_cache_bypass $no_cache</span><span class="pun">;</span><span class="pln">
        fastcgi_no_cache $no_cache</span><span class="pun">;</span><span class="pln">
        expires </span><span class="lit">10M</span><span class="pun">;</span><span class="pln">
        access_log off</span><span class="pun">;</span><span class="pln">
        add_header </span><span class="typ">Cache</span><span class="pun">-</span><span class="typ">Control</span><span class="pln"> </span><span class="str">"public"</span><span class="pun">;</span><span class="pln">
        add_header X</span><span class="pun">-</span><span class="typ">Cache</span><span class="pun">-</span><span class="typ">Status</span><span class="pln"> $upstream_cache_status</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}

</span></code></pre>
    <p>It's working for image URL <code><a class="moz-txt-link-freetext" href="https://mywebsite.com/include/php/render/framed/img.php?VR=1&size=300&image=U3pmwKi">https://mywebsite.com/include/php/render/framed/img.php?VR=1&size=300&image=U3pmwKi</a></code>
      but not for image URL <code><a class="moz-txt-link-freetext" href="https://mywebsite.com/include/php/render/framed/file/VR/1/size/300/image/U3dpwK/the-cat.jpg">https://mywebsite.com/include/php/render/framed/file/VR/1/size/300/image/U3dpwK/the-cat.jpg</a></code></p>
    <p>Yet I have put <code>fastcgi_cache_valid 404 20d;</code></p>
    <p>So, how to cache scripts going to <code>error_page 404 =
        /url_rewriting.php;</code>?<br>
      <br>
      Thank you for your help,<br>
      Vincent.<br>
    </p>
    <pre class="lang-php prettyprint prettyprinted" style="">
<code><span class="pun"></span></code></pre>
    <p><br>
    </p>
    <pre class="lang-php prettyprint prettyprinted" style="">
<code><span class="pun"></span></code></pre>
  </body>
</html>