Nginx prevents direct access to the Web server using IP

  • 2020-05-06 12:12:22
  • OfStack

The method provided in the official documentation:

If you do not want to process requests with undefined "Host" header lines, you may define a default server that just drops the requests:

 
server { 
listen 80 default_server; 
server_name _; 
return 444; 
} 

Simply reset the 444 error whenever a visitor accesses it with ip. However, it seems that this is not very friendly, if you can direct to the web server url would be good. The configuration is as follows:
 
server { 
listen 80 default_server; 
server_name _; 
rewrite ^ http://www.domain.com$request_uri?; 
} 

So there is still a problem, some special address, I need to use ip access, the others are forbidden, how to configure? For example, I want the monitor to access my machine's nginx status information directly using ip, and all other requests for access using ip are diverted to the domain name.
 
server { 
listen 80 default_server; 
server_name _; 
location /xxxxx{ 
stub_status on; 
access_log off; 
} 
location /{ 
rewrite ^ http://www.domain.com$request_uri?; 
} 
} 

So we have what we want.


Related articles: