Nginx 使用 PHP 进行 IMAP 的认证
程序员文章站
2022-04-27 14:35:38
...
首先假设一下条件:
Your Proxy server for pop/imap is running on 192.168.1.1
You have 2 backend pop/imap servers: 192.168.1.22 and 192.168.1.33
You have a webserver that you will use for the authentication and redirection logic 192.168.1.44.
The authentication script is /mail/auth.php
<?php if (!isset($_SERVER["HTTP_AUTH_USER"] ) || !isset($_SERVER["HTTP_AUTH_PASS"] )){ fail(); } $username=$_SERVER["HTTP_AUTH_USER"] ; $userpass=$_SERVER["HTTP_AUTH_PASS"] ; $protocol=$_SERVER["HTTP_AUTH_PROTOCOL"] ; // default backend port $backend_port=110; if ($protocol=="imap") { $backend_port=143; } if ($protocol=="smtp") { $backend_port=25; } // nginx likes ip address so if your // application gives back hostname, convert it to ip address here $backend_ip["mailhost01"] ="192.168.1.22"; $backend_ip["mailhost02"] ="192.168.1.33"; // Authenticate the user or fail if (!authuser($username,$userpass){ fail(); exit; } // Get the server for this user if we have reached so far $userserver=getmailserver($username); // Get the ip address of the server // We are assuming that you backend returns hostname // We try to get the ip else return what we got back $server_ip=(isset($backend_ip[$userserver] )?$backend_ip[$userserver] :$userserver; // Pass! pass($server_ip, $backend_port); //END function authuser($user,$pass){ // put your logic here to authen the user to any backend // you want (datbase, ldap, etc) // for example, we will just return true; return true; } function getmailserver($user){ // put the logic here to get the mailserver // backend for the user. You can get this from // some database or ldap etc // dummy logic, all users that start with a,c,f and g get mailhost01 // the others get mailhost02 if in_array(substr($user,0,1), array("a","c","f","g")){ return"mailhost01"; } else { return"mailhost02"; } } function fail(){ header("Auth-Status: Invalid login or password"); exit; } function pass($server,$port){ header("Auth-Status: OK"); header("Auth-Server: $server"); header("Auth-Port: $port"); exit; }
推荐阅读
-
Nginx中的用户认证配置及阻止用户使用代理访问的方法
-
使用bcompiler对PHP文件进行加密的代码
-
PHP如何使用JWT做Api接口身份认证的实现
-
Nginx使用的php-fpm的两种进程管理方式及优化
-
使用shell脚本对Nginx日志进行切分的示例代码
-
PHP没有数据库连接池怎么破?PHP环境下使用Nginx ngx_http_limit_req_module模块的高负载解决方案
-
PHP使用gearman进行异步的邮件或短信发送操作详解
-
PHP使用 Pear 进行安装和卸载包的方法详解
-
PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
-
php的list()的一步操作给一组变量进行赋值的使用