在生产环境中,为了网站的安全访问,需要Nginx禁止一些非法访问,如恶意域名解析,直接使用IP访问网站。下面记录一些常用的配置示例:
如果没有匹配上server name就会找default默认,返回501错误。
server { listen 80 default_server; server_name _; return 501; }
server { listen 80 default_server; server_name _; rewrite ^(.*) http://www.jb51.com/$1 permanent; }
if ($host ~ '^www.jb51.com'){ return 301 https://www.yisu.com$request_uri; }
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!