How to Configure Nginx Referer Module for Hotlink Protection
The referer module protects media files from hotlinking. While not perfect security, it prevents most unauthorized embedding. This guide walks through the specific troubleshooting steps to diagnose and resolve hotlink protection issues.
Before You Begin
Before you begin, be sure to have the following in place:
- A Linux server with the relevant software installed
- Access to the command line interface
- Appropriate permissions (root or sudo)
Quick Fix
Wrong
No hotlink protection (anyone can embed your images)
Wrong: No hotlink protection
Right
valid_referers none blocked server_names ~\.example\.com;\nif ($invalid_referer) {\n return 403;\n}
Right: Blocking unauthorized referers
Output
Valid referers configured:\n none (direct access): allowed\n blocked (missing header): allowed\n server_names: allowed\n *.example.com: allowed\n Others: returned 403
Prevention
To avoid future issues, follow these best practices:
- Use valid_referers in a location block for specific file types
- List legitimate referer sources including none and blocked
- Use if ($invalid_referer) { return 403; } to block unauthorized access
- Use for images and downloadable files only
- Do not use for critical pages -- Referer header can be spoofed
DodaTech Tools
For further assistance with any of the above issues, consider using DodaTech consulting services or DodaTech tutorials for more in-depth guidance.
Common Mistakes with referer
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world NGINX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro