The Facebook Crawler is hitting my servers multiple times every second and it seems to be ignoring both the Expires header and the og:ttl property.
In some cases, it is
Sending blindly 304 Not Modified
header does not have much sense and can confuse Facebook's crawler even more. If you really decide to just block some request you may consider 429 Too Many Requests header - it will at least clearly indicate what the problem is.
As a more gentle solution you may try:
Last-Modified
header with some static value. Facebook's crawler may be clever enough to detect that for constantly changing content it should ignore Expires
header but not clever enough to handle missing header properly.ETag
header with proper 304 Not Modified
support.Cache-Control
header to max-age=315360000, public, immutable
if the image is static.You may also consider saving cached image and serving it via webserver without involving PHP. If you change URLs to something like http://fb.example.com/img/image/123790824792439jikfio09248384790283940829044
You can create fallback for nonexistent files by rewrite rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^img/image/([0-9a-z]+)$ img/image.php?id=$1 [L]
Only first request should be handled by PHP, which will save cache for requested URL (for example in /img/image/123790824792439jikfio09248384790283940829044
). Then for all further requests webserver should take care of serving content from cached file, sending proper headers and handling 304 Not Modified
. You may also configure nginx for rate limiting - it should be more efficient than delegating serving images to PHP.