Home  >  Article  >  Operation and Maintenance  >  How to ban ip access or illegal domain name access in Nginx

How to ban ip access or illegal domain name access in Nginx

王林
王林forward
2023-05-21 15:55:06982browse

在生产环境中,为了网站的安全访问,需要Nginx禁止一些非法访问,如恶意域名解析,直接使用IP访问网站。下面记录一些常用的配置示例:

1)禁止IP访问

如果没有匹配上server name就会找default默认,返回501错误。

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

2)通过301跳转到主页

server {
  listen 80 default_server;
  server_name _;
  rewrite ^(.*) http://www.jb51.com/$1 permanent;
} 

3)凡是请求www.jb51.com都跳转到后面域名www.yisu.com上。(需要放到server配置里)

if ($host ~ '^www.jb51.com'){
     return 301 https://www.yisu.com$request_uri;
   }

 4)Nginx限制非法域名恶意解析到本地服务器和IP访问网站

server {undefined
    listen 80 default_server;
    server_name _;
    return 501;
}

The above is the detailed content of How to ban ip access or illegal domain name access in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete