Nginx Proxy Manager (NPM) 是一个基于Nginx的图形化管理工具,可以方便地配置反向代理、SSL证书等。虽然它的界面简化了许多配置,但仍可以实现基于用户的访问控制。以下是详细的实现方案:
登录NPM控制台
http://[服务器IP]:81
选择或创建代理主机
配置访问控制
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/htpasswd/npm_proxy;
创建用户密码文件
sudo mkdir -p /etc/nginx/htpasswd
sudo htpasswd -c /etc/nginx/htpasswd/npm_proxy username1
(后续用户添加时去掉-c
参数)
重启Nginx服务
sudo docker exec -it nginx-proxy-manager nginx -s reload
allow 192.168.1.0/24;
allow 10.0.0.1;
deny all;
确保Nginx有GeoIP模块
添加配置:
geo $allowed_country {
default no;
US yes;
CA yes;
GB yes;
}
server {
# ...其他配置...
if ($allowed_country = no) {
return 403;
}
}
对于更复杂的场景,可以集成LDAP、OAuth等认证:
location / {
auth_request /auth;
# ...其他配置...
}
location = /auth {
internal;
proxy_pass http://auth-service/validate;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
set $allow_access 0;
if ($http_cookie ~* "access_token=valid") {
set $allow_access 1;
}
if ($allow_access = 0) {
return 403;
}
对于企业级需求,可以集成Authelia提供: - 多因素认证 - 细粒度访问控制 - 单点登录
配置示例:
location / {
auth_request /authelia-auth;
error_page 401 =302 https://auth.yourdomain.com/?rd=$scheme://$http_host$request_uri;
# ...其他配置...
}
location /authelia-auth {
internal;
proxy_pass http://authelia:9091/api/verify;
# ...其他头信息...
}
通过以上方法,您可以根据实际需求在Nginx Proxy Manager中实现从简单到复杂的各种访问控制策略。