最近研究nginx代码, 以mail模块入手, 但是从配置看, 一直看不出来nginx把消息proxy到哪里去了.
今天研究了半天, 才搞这个事情搞清楚. 之前在网上也没有找到类似的说明, 包括官方的文档也没有说明.
mail模块有一个ngx_mail_auth_http_module.c模块, 其中有一个配置命令auth_http,nginx通过向此配置发送一个http请求,以获取得后端mail server的ip和端口. 比如:
Request:
GET /auth HTTP/1.0
Host: localhost
Auth-Method: plain # plain/apop/cram-md5
Auth-User: user
Auth-Pass: password
Auth-Protocol: imap # imap/pop3/smtp
Auth-Login-Attempt: 1
Client-IP: 192.0.2.42
Client-Host: client.example.org
Good response:
HTTP/1.0 200 OK
Auth-Status: OK
Auth-Server: 198.51.100.1
Auth-Port: 143
以上的Request是nginx发送给auth_http配置的url的请求,Good response是相应响应. nginx从这个响应中的Auth-Server和Auth-Port获取得到后端mail server的ip和端口. nginx会连接到这个ip:port,并与此按相就的邮件协议(如POP3)交互.
同时
If the “Auth-User” header exists in the response, it overrides the username used to authenticate with the backend.