Nginx配置只能通过域名访问,禁止通过IP访问


为了避免客户直接使用IP地址访问我们的网站,导致运维需要变更域名对应IP时引发客户报障,我们可以通过Nginx进行域名访问的限制性配置,具体配置如下:

修改nginx.conf 文件
修改成

server {
    listen 80;
    server_name www.example.com; #这里是你自己指定的域名
    ...
}

再在上个server后继续添加一段:
如果没有找到域名配置的server,会继续向后查找server,走到默认server直接返回403

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

通过以上配置,当使用IP访问时就会返回403错误

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.