Website Performance
What Is Hotlinking and How to Block It on Your Server
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيHotlinking happens when another website displays your images, videos or downloads by linking directly to the file on your server instead of hosting a copy. Their page looks complete, but every view is served from your machine and counted against your bandwidth. For a small site it may go unnoticed; when a popular forum or a content-scraping site starts embedding your files, it can turn into real traffic, real cost and a slower experience for your own visitors. This article explains how hotlinking works, how to spot it and how to block it safely.
How Hotlinking Works
When a browser loads an image embedded on a page, it sends the request to whatever server hosts the file. Along with the request it usually includes a Referer header (spelled with one "r" in the HTTP standard) that names the page containing the image. If other-site.com embeds:
<img src="https://example.com/uploads/photo.jpg">your server receives a request with Referer: https://other-site.com/.... That header is what most hotlink protection relies on.
Why It Matters
- Bandwidth and transfer. Every embedded view downloads the full file from you. Large images, videos and ZIP files add up fast.
- Server load. Requests compete with your real visitors for connections and disk I/O.
- Content misuse. Your photos or product images appear on pages you do not control, sometimes presented as someone else's work.
How to Detect Hotlinking
Your access log already records the referrer. On Nginx with the default log format, this lists the top external referrers for image requests:
sudo awk '$7 ~ /\.(jpg|jpeg|png|gif|webp|mp4)$/ {print $11}' /var/log/nginx/access.log \
| grep -v 'example.com' | sort | uniq -c | sort -rn | head -20Replace example.com with your domain. Field numbers depend on your log format, so check one log line first. Also review your traffic graphs: a sudden jump in outbound transfer without a matching rise in page views is a typical sign.
Blocking Hotlinking in Nginx
Nginx has a built-in valid_referers directive. Add a location for media files inside your server block:
location ~* \.(jpg|jpeg|png|gif|webp|avif|svg|mp4|webm|zip|pdf)$ {
valid_referers none blocked server_names
*.example.com example.com
~\.google\. ~\.bing\.;
if ($invalid_referer) {
return 403;
}
expires 30d;
}What each value means:
none: noRefererheader at all, such as a direct visit or a privacy-focused browser.blocked: the header exists but was stripped or masked by a proxy or firewall.server_names: the names in yourserver_namedirective.- The regex entries allow search engines, so image search keeps working.
Test and reload:
sudo nginx -t && sudo systemctl reload nginx
curl -I -e "https://other-site.com/" https://example.com/uploads/photo.jpg # expect 403
curl -I -e "https://example.com/" https://example.com/uploads/photo.jpg # expect 200Instead of return 403, you can rewrite to a small placeholder image, but keep that image outside the protected location or you will create a loop.
Blocking Hotlinking in Apache
With mod_rewrite enabled, add this to the virtual host or .htaccess:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteCond %{HTTP_REFERER} !(google|bing)\. [NC]
RewriteRule \.(jpe?g|png|gif|webp|mp4|zip|pdf)$ - [F,NC]The first condition allows requests with an empty referrer; the rule returns 403 Forbidden for everything else not on the list.
Using a CDN
If your site is behind a CDN, enable hotlink protection there. Most CDNs offer a referrer-based toggle or rule, and blocking at the edge means the unwanted requests never reach your server at all. Remember to keep the origin rules consistent, or allow the CDN to fetch files freely.
Stronger Options for Valuable Files
Referrer checks are easy to bypass because a script can send any header it likes. For paid downloads, private videos or documents, use:
- Signed, expiring URLs. The link contains a token and an expiry time; Nginx can verify it with the
secure_linkmodule, and object storage services support pre-signed URLs. - Authenticated downloads. Serve the file through your application after checking the session, with
X-Accel-Redirectin Nginx so the web server still does the heavy lifting.
Mistakes to Avoid
| Mistake | Result |
|---|---|
| Not allowing empty referrers | Direct visits and some privacy tools see broken images |
| Forgetting your own subdomains or CDN host | Your own pages break |
| Blocking search engines | Images disappear from image search |
| Blocking social and messaging previews | Link previews lose their image; allow those you rely on |
| Protecting CSS and JS | Usually unnecessary and risks breaking your site |
Hotlinking is one of several things that drive data transfer; our guide to bandwidth in hosting covers the rest and what to watch.
FAQ
Is hotlinking illegal?
Hotlinking itself is a technical practice, and how the law treats it depends on the country and the content. Displaying someone's copyrighted images without permission can raise legal issues. Blocking it at the server is the practical step regardless of the legal question.
Will hotlink protection hurt my SEO?
Not if you allow search engine referrers and empty referrers. Search engine crawlers fetch images without a referrer, and the none value keeps them working. Test with image search after enabling the rules.
Can someone bypass referrer-based protection?
Yes. A script or a server-side fetch can fake or omit the header. Referrer checks stop casual embedding on other web pages, which is the common case. For content that must stay private, use signed URLs or authenticated downloads.