Nginx image anti hotlinking configuration instance

  • 2020-05-07 20:55:03
  • OfStack

Do operation and maintenance of friends will often have 1 some trouble, such as the site is always copied, such as the site is always cited above the picture, although a picture of two pictures, does not consume a lot of traffic, but 1 dener quoted more people, this is still relatively waste traffic.

Hotlinking prevention is barred other web reference their web resources and do 1 set series, advantage is no need to say, majority is to prevent waste of resources, especially with broadband, traffic limit space if you don't do 1 bit may refer to their website photo, video, and so on light resource consumption may be great 1 part flow. This article briefly describes how to do hotlinking protection on nginx servers.

nginx anti-hotlinking code

step 1: download the nginx website profile.

Some users may go directly to the server to use vi or ee editor, and some site configuration may be in the nginx.conf file or in the vhost directory, depending on how the nginx.conf file is configured.

Step 2: add the following code and save it.


location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires 30d;
            access_log off;
            valid_referers none blocked *.web100.cc web100.cc *.youdao.com *.google.cn *.google.com *.google.com.tw *.google.com.sg *.google.com.hk image.soso.com bing.com cn.bing.com image.baidu.com;
            if ($invalid_referer){
                rewrite ^/ http://ofstack.com/images/return.gif;
            }
        }

step 3: upload and overwrite the saved website profile to the terminal to perform

/ etc/init d/nginx reload restart nginx can take effect.

Since the nginx installation directory may not be the same for different systems, simply restart the nginx service.

nginx anti-hotlinking code description:


location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
    expires 30d;# Cache in client browser 30 day
    access_log off; # The above extension file (gif|jpg|jpeg|png|bmp|swf) Do not write to log files.
    valid_referers none blocked *.web100.cc web100.cc ofstack.com *.youdao.com *.google.cn *.google.com *.google.com.tw *.google.com.sg *.google.com.hk image.soso.com bing.com cn.bing.com image.baidu.com;
    #valid_referers none blocked Allow access to the domain site, the background is followed by multiple domain name space.
    #none Match without Refer The access, simple to say is directly used URL Resources that access these extensions.
    #blocked Support for domain prefix.
    # Other than those that allow direct access to domain names, direct access to other domain names directly displays the following image.
    if ($invalid_referer){
        rewrite ^/ http://ofstack.com/images/return.gif;
    }

After this setting, when others want to quote the pictures in your website, they will find that the quoted pictures are not what they want.


Related articles: