Nginx禁止IP,只允许域名访问

Nginx禁止IP,只允许域名访问

一、前言

为何禁止IP访问呢?这样是为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网。怎么做呢?

二、修改Nginx配置文件

1、打开配置文件

一般Ngnix的配置文件放在/usr/local/nginx/conf/目录下,使用如下指令打开配置文件:

vim /usr/local/nginx/conf/nginx.conf

2、修改配置文件

在server中添加如下代码:

if ($host ~ "\d+\.\d+\.\d+\.\d") {
    return 404;
}

插入位置如下图所示:

Nginx禁止IP,只允许域名访问

~后面跟着的是正则表达式,匹配的是IP,如果匹配到了IP,则返回404,禁止其访问。

3、重启Nginx服务

使用如下指令重启Nginx服务:

service nginx reload

三、效果

配置好后,使用IP访问自己的网站,会看到如下图所示的效果:

Nginx禁止IP,只允许域名访问

来源:

https://cuijiahua.com/blog/2017/10/website_4.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_3216.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?