Hotlink Protection In Nginx


05th February 2021 1 min read
Share On        


What is Hotlink Protection?

A hotlinking is a process of using our website images, static assets (CSS, Javascript, Fonts etc.) to other websites that are not related to us. Because of this, our website performance will reduce drastically. Every time the other user website loads it will pull the resources from our website.

No need to worry as we can prevent it with the following method


Implement in Nginx Web Server

valid_referers none blocked wifidabba.com *.wifidabba.com;
if ($invalid_referer) {
    return   403;
}


Full implementation snippet in Nginx Web Server

location ~* \.(gif|otf|jpg|jpeg|png|css|js)$ {
    valid_referers none blocked wifidabba.com *.wifidabba.com;
    if ($invalid_referer) {
        return   403;
    }
    add_header Cache-Control public;
    add_header Pragma public;
    add_header Vary Accept-Encoding;
    expires max;
}




AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing