nginx限制指定路径只能被白名单IP访问

- web

本篇内容为了增加网站的安全性,保护后台登录页(/admin/login)只能被白名单IP访问,非白名单IP只能访问前端页面。

1、默认反向代理配置

server {
    listen       80;
    server_name  _;

    location / {
        proxy_pass http://localhost:6412;
    }
}


2、为 /admin 目录添加白名单限制

server {
    listen       80;
    server_name  _;

    location /admin {
        allow 192.168.3.13;
        deny all;
        proxy_pass http://localhost:6412;
    }
    location / {
        proxy_pass http://localhost:6412;
    }
}


3、验证访问

使用 192.168.3.13 主机访问站点能正常显示
52797d4cf6cf6e3a771e5219c2cf3a6.png

使用其他IP主机访问站点提示 403 访问拒绝
0a43e4f489e919a141ff88d9d63b2a0.png