Nginx代理smtp、pop等邮件服务
转载
http://blog.liuker.cn/index.php/nginx/35.html
环境需要:
1、需要一台nginx做mail的反向代理
2、需要一台做认证的php环境
3、一台测试客户机(测试邮件发送采用sendEmail)
整个流程:
客户机发邮件---->nginx---->认证---->发送邮件
一、部署nginx
这里基本就一笔带过了。最简单的编译参数即可。但需保证有--with-mail
|
1
2
3
4
5
|
wget http://nginx.org/download/nginx-1.0.4.tar.gztar -zxf nginx-1.0.4.tar.gzcd nginx-1.0.4./configure –prefix=/usr/local/nginx –with-mail –without-httpmake && make install |
二、配置nginx
直接贴配置了
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
user root;worker_rlimit_nofile 65535;worker_cpu_affinity 00000001 00000010 00000100 00001000;worker_processes 4;error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;events { use epoll; worker_connections 65535;}mail { server_name proxy.test.com; #本地监听域名 auth_http auth.test.com/auth.php; #这个是需要的一个认证模块,可以在本地,也可以异地(注意配置hosts,或者域名) pop3_capabilities "TOP" "USER"; imap_capabilities "IMAP4rev1" "UIDPLUS"; server { listen 110; protocol pop3; proxy on; } server { listen 143; protocol imap; proxy on; } server { listen 25; protocol smtp; proxy on; smtp_auth login plain; xclient off; }} |
三、配置认证模块
由于认证脚本是php的,所以需要php环境,准备php(略)可以使用nginx结合php,或者用apache结合php都行。
下面是php脚本
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?phpif (!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;}if($username == $username) { //验证条件 $server_ip = "10.10.10.1"; //这个是邮件服务器的ip}else{ exit;}pass($server_ip, $backend_port);//ENDfunction authuser($user,$pass){ return true;}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;}?> |
四、测试发送
记得配置hosts
nginx代理服务器的ip proxy.test.com
测试发送用的是sendEmail,安装略了。很简单
[root@DonTony ~]# /root/sendEmail -f test@test.com -t test1@test.com -u "nginx proxy" -m "nginx proxy" -o message-charset=utf8 -s proxy.test.com -xu test -xp passwod
Jun 22 18:15:35 DonTony sendEmail[1840]: Email was sent successfully!
测试发送成功。

php也有访问成功的记录。
10.10.10.2 - - [22/Jun/2016:18:17:09 +0800] "GET /auth.php HTTP/1.0" 200 0 "-" "-" "-"
如果发现无法发送,查看代理上的error.log

浙公网安备 33010602011771号