Nginx directory protection, anti-theft chain, speed limit and multi-domain name processing

Source: Internet
Author: User

Http://www.opsers.org/server/nginx-directory-protection-anti-hotlinking-processing-speed-and-multi-domain.html

NGiNX is a very good HTTP Server software, can be set up to implement many functions, such as directory protection, IP access restrictions, anti-theft chain, download speed limit and set multi-domain name and so on

One, Nginx directory protection and Access restrictions

The Nginx protected directory is configured as follows, and the directory password-protected file is/USR/LOCAL/NGINX/HTPASSWD

Location-/admin {#admin为要保护的目录名称, location means to protect the admin directory from the root directory of the Web page auth_basic "please LOGIN"; #就是进入资料夹时会显示的信息auth_basic_user_file/usr/local/nginx/htpasswd; #验证用户及密码文件, my side is set on/USR/LOCAL/NGINX/HTPASSWD}
Location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param script_filename/var/www/bbs$ Fastcgi_script_name;include Fastcgi_params;}

Note that when you set up directory password protection, be aware of the location of the configuration block, if it is located after the site ~ \.php$ {} chunks, if you open a static page or picture and other non-PHP files, you will be prompted to enter a password, but if you open a php file, the settings are invalid, The PHP file is executed directly and displayed. Therefore, it is important to note that the block to which you want to set the directory password protection is placed before the location ~ \.php$ {} block.

To generate a password file:

Htpasswd-b-c/usr/locla/nginx/htpasswd username password;
Second, nginx anti-theft chain

In addition NGiNX anti-theft chain, prevent others to steal pictures (video, flash, software ...)  Of course it's OK! Also in the server section Plus

Location ~* \. (txt|ico|gif|png|bmp|jpg|jpeg|zip|rar|gz|7z|exe|mp3|flv|swf) $ {valid_referers None blocked opsers.org Www.opsers.org, if ($invalid _referer) {rewrite ^/http://www.opsers.org/notlink.html;}}

This is where location means protection from the root of the Web page, all the files of the specified type are in accordance with this rule
Valid_referers none blocked means not stopping where you're coming from. To separate allowed domain names or IP locations with spaces
$invalid _referer means no link is allowed.
Rewrite ^/http://www.opsers.org/notlink.html; The meaning is to specify that the disallowed links automatically turn to a page, or already have a set 404 forwarding address can be annotated out of the line and then take out the #return 404 annotations, will automatically run to the 404 page you set.

Feather Fly Special reminder: How to see whether the anti-theft chain is in force, we suggest you look directly at the service log, do not take a website to test. Because some shameless guy (like Baidu), he will use his own image server to provide services. That's why, the last time I was doing this, I wasted a lot of it.

Third, Nginx download speed limit

NGiNX can also limit the speed and then restrict the download thread! First find the Limit_zone in the HTTP section, and then take out the comments ~
# set an area called crawler, size 20MB

Limit_zone crawler $binary _remote_addr 20m;

Then add the section in the server

# Restricted file types can only be downloaded on a single line

Location ~. *\. (zip|rar|gz|tar|exe|mp3|flv|swf|jpg|jpeg) ${limit_conn crawler 1;limit_rate 500k; # Plus speed limit}
# Restrict only single-line download under specific folders # location/download/{# limit_conn crawler 1;# limit_rate 500k; # Plus speed limit #}
Four, Nginx multi-domain settings

Today in the configuration of Php+nginx a serious problem, in the Nginx virtual host bound the pan domain name, in the program need to use a two-level domain name to point to different content, but regardless of access only jump to the main domain! In order to find a problem, one experiment concluded that no matter how many domain names are bound, using $_server["SERVER_NAME" will only return the first domain name bound in the virtual host!

For example, bind domain name as follows:

server_name www.fxzc.com *.fxzc.com fxzc.com

Now I don't care what domain access, $_server["SERVER_NAME" will only return www.fxzc.com!! This is a very serious problem, has a fatal impact on the pan-domain name!

Since there is a problem, there must be a solution. In the wiki master turned n long, finally found the necessary information! Original: The value returned by $_server["SERVER_NAME" is provided by server_name in Nginx Fastcgi_param, and the default configuration is:
Fastcgi_param server_name $server _name;
Nginx $server _name variable is the domain name set above, will only return the first one!
This is a good run, the above configuration to change:

Fastcgi_param server_name $host;

On the line.

In addition, you need to add a line after the server_name configuration:

Server_name_in_redirect off;

This means that the first domain name in the server_name setting is not used by Nginx when handling its own internal redirection!
Source Text Address: http://www.fxzc.net/archives/nginxmulubaohufangdaolianxiansujiduoyumingchuli.html

Nginx directory protection, anti-theft chain, speed limit and multi-domain name processing

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.