关于nginx使用mail模块进行邮件端口转发

近日,由于项目需求,需要使用nginx实现mail邮件端口转发,并且能获取到通过代理转发后拿到发件的真实ip地址。由于这方面资料太少,只能自己摸索。

经过学习与研究nginx中文文档http://www.nginx.cn/doc/,发现使用nginx中mail转发的功能,在nginx.conf中增加mail模块:

mail {

# See sample authentication script at:

#

http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript

auth_http localhost/auth.php;

pop3_capabilities "TOP" "USER";

imap_capabilities "IMAP4rev1" "UIDPLUS";

server {

listen     localhost:110;

protocol   pop3;

proxy      on;

}

server {

listen     localhost:143;

protocol   imap;

proxy      on;

}

server {

listen    25;

protocol    smtp;

proxy    on;

smtp_auth login plain none;

xclient    off;

}

}

开始实现时发现,使用本域邮件发件就会走这个auth http地址认证,外域发件过来 怎么也不走这个认证地址,误以为是需要外域能请求到这个地址,所以把该地址改成了外域可以请求到的网络地址,最终发现完全就不是,也一直感觉这样跟nginx有所违背,其实这个auth http就是监听到访问本机的25端口时,请求该地址将正确的邮件服务及端口返回:

C#实现该认证地址:

public void ReturnBackEndServer()

{

LogHelper.WriteLog("-------------------------------nginx start.----------------------------------");

try

{

if (HttpContext.Current.Request != null)

{

if (HttpContext.Current.Request.Headers != null)

{

foreach (string key in HttpContext.Current.Request.Headers)

{

LogHelper.WriteLog(string.Format("key:{0}, value:{1}", key, HttpContext.Current.Request.Headers[key]));

}

}

else

{ LogHelper.WriteLog("header is null1"); }

}

else

{ LogHelper.WriteLog("header is null2"); }

}

catch(Exception ex)

{

LogHelper.WriteLog(ex);

}

finally

{

LogHelper.WriteLog("================================nginx end.===================================");

HttpContext.Current.Response.Headers.Add("Auth-User", "可以更改此值,在XCLIENT ADDR=真实ip LOGIN=就是这里的Auth-User NAME=[UNAVAILABLE]");

HttpContext.Current.Response.Headers.Add("Auth-Status", "OK");

HttpContext.Current.Response.Headers.Add("Auth-Server", ConfigurationManager.AppSettings["NginxServer"]);

HttpContext.Current.Response.Headers.Add("Auth-Port", ConfigurationManager.AppSettings["NginxPort"]);

HttpContext.Current.Response.End();

}

邮件服务中的XCLient代码:

开启XClient 为on 后,可以调用邮件服务的XCLient()方法,可以将真实ip更改获取。XCLient方法中XCLIENT ADDR=真实ip LOGIN=就是这里的Auth-User NAME=[UNAVAILABLE] 将整个连接的session中用户ip更改为正式ip。

官方文档的XCLient所有参数为:


2017年11月14日20:24:07
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容