I want to convert this to nginx rules
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?domain.com [NC]
RewriteRule .(jpg|jpeg|png|gif)$ - [NC,F,L]
I’ve tried to put this on the web but I can not even open.
if ($http_referer !~ "^$"){
set $rule_0 1$rule_0;
}
if ($http_referer !~* "^http(s)?://(www.)?domain.com"){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
return 403;
break;
}
I would like .htaccess above script can work smoothly in nginx.
if there is something wrong please correct. and show me the correct script …
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi!
I’m guessing these rules are there to stop people from hotlinking images hosted on your site? In some cases, writing the rules with nginx in mind is better than converting already-written htaccess/Apache rules.
Luckily, nginx supports referrer whitelisting by default. Use the following code:
location ~ .(gif|png|jpe?g)$ {
valid_referers none blocked domain.com www.domain.com;
if ($invalid_referer) {
return 403;
}
}
Make sure to restart nginx so that the changes take effect:
sudo service nginx restart
You can read more on nginx’s referer module here.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.