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

王林
Release: 2023-05-21 15:55:06
forward
1023 people have browsed it

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

1)禁止IP访问

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

server {
   listen 80 default_server;
   server_name _;
   return 501;
}
Copy after login

2)通过301跳转到主页

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

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

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

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

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

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template