Nginx prohibit IP access only allow domain name access _nginx

Source: Internet
Author: User
Tags net return

We will encounter a lot of malicious IP attacks when using, this time will use Nginx prohibit IP access. Let's take a look at Nginx's default virtual host when users access through IP, or through an unnamed domain name (such as when someone points his own domain name to your IP), the key point is to add this line to the server's settings:
listen default;
The following default parameter indicates that this is the default virtual host.
Nginx prohibit IP access to this setting is useful.

For example, when other people visit your website via IP or unknown domain name, you want to prohibit the display of any valid content, you can return 500 to him. At present, many computer rooms are required to shut down the main web site host head, to prevent the record of the domain name pointed over to cause trouble. You can set it this way:

Simple and straightforward way to modify:

modifying nginx.conf files
Modified into

server {
listen;
server_name www.jb51.net;  #这里是你自己指定的域名
...
}

Continue adding a section after the last server:

is to access the default Web site directly to return 403

server {
listen default_server;
server_name _;
return 403;
}

This is where you return 403 errors when you receive IP access or if you do not specify a domain name access

Some of the following are described in more detail:

server { 
  listen default; 
  return 500; 
 

You can also collect these traffic, import to their own website, as long as the following jump settings can be:

server { 
  listen default; 
  Rewrite ^ (. *) http://www.jb51.net permanent; 
 

After setting up, it is not possible to access the server through IP, but in the should be used when server_name followed by multiple domain names, one of the domain name can not access, set as follows:

server { 
  listen; 
  server_name www.jb51.net jb51.net 
 }

Before the change, through the server_name www.jb51.net jb51.net can access the server, add Nginx prohibit IP access settings, through the jb51.net can not access the server, www.jb51.net access, with Nginx- The T detection profile prompts warning:
[Warn]: Conflicting server name "Jb51.net" on 0.0.0.0:80,
Ignored
The Configuration file/usr/local/nginx/conf/
nginx.conf syntax is OK
Configuration File/usr/local/nginx/conf/nginx.
Conf Test is successful
Finally passed in listen default; then add server_name _; Resolve, form as follows:

#禁止IP访问 
server 
{ 
listen default; 
server_name _; 
return 500; 

Or

server { 
listen dufault; 
server_name _; 
Rewrite ^ (. *) http://www.jb51.net permanent; 
#禁止IP访问 
 Server { 
  listen default; 
  server_name _; 
  server_name www.jb51.net jb51.net return 
  500; 
 

In this way, the server can be accessed through jb51.net.

The first case: access to a station to B station

server { 
server_name www.jb51.net; 
Rewrite ^ (. *) http://www.jbzj.com$1 permanent; 

In the second case: not all redirects to the a station to the specified page

server { 
server_name www.jb51.net; 
if ($host!= ' jb51.net ') { 
rewrite ^/(. *) $ http://www.jbzj.com/$1 permanent; 
} 
}

If you write in the first server segment
will also be redirected when using IP access

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.