近日凌彬,由于項(xiàng)目需求铲敛,需要使用nginx實(shí)現(xiàn)mail郵件端口轉(zhuǎn)發(fā)会钝,并且能獲取到通過(guò)代理轉(zhuǎn)發(fā)后拿到發(fā)件的真實(shí)ip地址迁酸。由于這方面資料太少,只能自己摸索型型。
經(jīng)過(guò)學(xué)習(xí)與研究nginx中文文檔http://www.nginx.cn/doc/全蝶,發(fā)現(xiàn)使用nginx中mail轉(zhuǎn)發(fā)的功能,在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;
}
}
開(kāi)始實(shí)現(xiàn)時(shí)發(fā)現(xiàn)绷落,使用本域郵件發(fā)件就會(huì)走這個(gè)auth http地址認(rèn)證始苇,外域發(fā)件過(guò)來(lái) 怎么也不走這個(gè)認(rèn)證地址,誤以為是需要外域能請(qǐng)求到這個(gè)地址函喉,所以把該地址改成了外域可以請(qǐng)求到的網(wǎng)絡(luò)地址荣月,最終發(fā)現(xiàn)完全就不是哺窄,也一直感覺(jué)這樣跟nginx有所違背萌业,其實(shí)這個(gè)auth http就是監(jiān)聽(tīng)到訪問(wèn)本機(jī)的25端口時(shí),請(qǐng)求該地址將正確的郵件服務(wù)及端口返回:
C#實(shí)現(xiàn)該認(rèn)證地址:
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=真實(shí)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();
}
郵件服務(wù)中的XCLient代碼:
開(kāi)啟XClient 為on 后懂从,可以調(diào)用郵件服務(wù)的XCLient()方法蹲蒲,可以將真實(shí)ip更改獲取届搁。XCLient方法中XCLIENT ADDR=真實(shí)ip LOGIN=就是這里的Auth-User NAME=[UNAVAILABLE] 將整個(gè)連接的session中用戶(hù)ip更改為正式ip窍育。
官方文檔的XCLient所有參數(shù)為: