Use the Referer directive to configure the Nginx server to prevent picture hotlinking _nginx

Source: Internet
Author: User
Tags nginx server

Because Nginx does not support. htaccess, it is not feasible to prevent it directly from this aspect, we have to fix it by modifying the configuration file.
First, we find the need for anti-theft chain of the domain name conf file, path:/usr/local/nginx/conf/vhost/, such as guance.com.conf. First back up the original file, and then find the following section:

Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $
{
expires 30d;
}

Modify it to:

Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $
{
valid_referers none blocked Www.jb51.net jb51.net;
if ($invalid _referer) {
rewrite ^/http://www.jb51.net/404.jpg;
#return 404;
}
Expires 30d;
}

The above content, everybody please according to the individual circumstance discretion revise, I here do the basic explanation:
The first line gif|jpg|jpeg|png ... These are the types of files you need to prevent hotlinking, and you can add some suffix types;
The third line is your site's domain name, that is, the release of the domain name, if there are multiple, please add, pay attention to space;
Line five is to give hotlinking see the picture, return a 404.jpg, this picture source address is to be able to chain of Oh, otherwise, others see also is a XX.
After the completion of the save, upload to the original location coverage, and then restart the LNMP to make it effective.

/ROOT/LNMP restart

Introduction to Referer directives
Nginx module Ngx_http_referer_module is typically used to block domain name requests that are illegal from the source. We should keep in mind that camouflage referer the head is a very simple thing, So this module can only be used to block most illegal requests. We should keep in mind that some legitimate requests do not bring referer source headers, so sometimes do not reject requests from the source header (Referer) as empty.
Syntax: Referer_hash_bucket_size size;
Default value: Referer_hash_bucket_size 64;
Configuration segment: Server, location
This instruction begins to appear in the Nginx 1.0.5.
Sets the bucket size for the valid referers hash tables. The details of setting up hash tables are provided in a separate document.
Syntax: Referer_hash_max_size size;
Default value: Referer_hash_max_size 2048;
Configuration segment: Server, location
This instruction begins to appear in the Nginx 1.0.5.
Sets the maximum size of the valid referers hash tables. The details of setting up hash tables are provided in a separate document.
Syntax: Valid_referers None | Blocked | Server_names | String ...;
Default value:-
Configuration segment: Server, location
To specify the legal source ' referer ', he decides the value of the built-in variable $invalid_referer, if the Referer header is contained within the legitimate URL, the variable is set to 0, otherwise set to 1. Remember, case-insensitive.
Parameter description
None
"Referer" Source with empty head
Blocked
The "Referer" source header is not empty, but the value inside is removed by the agent or the firewall, and these values do not begin with http://or https://.
Server_names
The "Referer" source header contains the current server_names (current domain name)
Arbitrary string
Any string that defines the server name or optional URI prefix. The host name can be used with the * start or end, and the host port in the source domain name will be ignored in the process of detecting the source header.
Regular expression
Regular expression, ~ indicates a string that excludes the beginning of https://or http://.
Last
The use of the source image of the head to do anti-theft chain is the most reasonable. Simple and practical. But there is no way to prevent collection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.