轉載
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
.gz
tar
-zxf nginx-1.0.4.
tar
.gz
cd
nginx-1.0.4
.
/configure
–prefix=
/usr/local/nginx
–with-mail –without-http
make
&&
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
|
<?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;
}
if
(
$username
==
$username
) {
//驗證條件
$server_ip
=
"10.10.10.1"
;
//這個是郵件服務器的ip
}
else
{
exit
;
}
pass(
$server_ip
,
$backend_port
);
//END
function
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