我是靠谱客的博主 无心衬衫,这篇文章主要介绍Nginx【有与无】【NP-AG8-1】邮件代理服务器1.将 NGINX 配置为邮件代理服务器2.介绍3.先决条件4.配置 SMTP/IMAP/POP3 邮件代理服务器5.为邮件代理设置身份验证6.为邮件代理设置 SSL/TLS7.为邮件代理优化 SSL/TLS8.完整示例,现在分享给大家,希望可以做个参考。

说明

  • NP: NGINX Plus
  • AG: Admin Guide
  • 会话: session
  • 上游:  upstream
  • 流量:traffic
  • 后端:backend
  • 区域:zone
  • 切片:slices
  • 位置:location
  • 根:root
  • 终端:termination
  • 端点:endpoint

目录

1.将 NGINX 配置为邮件代理服务器

2.介绍

3.先决条件

4.配置 SMTP/IMAP/POP3 邮件代理服务器

5.为邮件代理设置身份验证

6.为邮件代理设置 SSL/TLS

7.为邮件代理优化 SSL/TLS

8.完整示例


1.将 NGINX 配置为邮件代理服务器

使用 NGINX 或 NGINX Plus 作为 IMAP、POP3 和 SMTP 协议的代理,简化电子邮件服务并提高其性能。

本文将解释如何将 NGINX Plus 或 NGINX Open Source 配置为邮件服务器或外部邮件服务的代理。

2.介绍

NGINX 可以将 IMAP、POP3 和 SMTP 协议代理到托管邮件帐户的上游邮件服务器之一,因此可以用作电子邮件客户端的单个端点。 这可能会带来许多好处,例如:

  • 轻松扩展邮件服务器的数量
  • 根据不同的规则选择邮件服务器,例如根据客户端的IP地址选择最近的服务器
  • 在邮件服务器之间分配负载

3.先决条件

  • NGINX Plus(已经包括代理电子邮件流量所需的邮件模块)或 NGINX Open Source 使用用于电子邮件代理功能的 --with-mail 参数和用于 SSL/TLS 支持的 --with-mail_ssl_module 参数编译邮件模块:
复制代码
1
$ ./configure --with-mail --with-mail_ssl_module --with-openssl=[DIR]/openssl-1.1.1
  • IMAP、POP3 和/或 SMTP 邮件服务器或外部邮件服务

4.配置 SMTP/IMAP/POP3 邮件代理服务器

在 NGINX 配置文件中:

1.创建顶级 <code>mail</code> 上下文(定义在与 <code>http</code> 上下文相同的级别):

复制代码
1
2
3
mail { #... }

2.使用 <code>server_name</code> 指令指定邮件服务器的名称:

复制代码
1
2
3
4
mail { server_name mail.example.com; #... }

3.使用 <code>auth_http</code> 指令指定 HTTP 身份验证服务器。 身份验证服务器将对电子邮件客户端进行身份验证,选择上游服务器进行电子邮件处理,并报告错误。

复制代码
1
2
3
4
5
mail { server_name mail.example.com; auth_http localhost:9000/cgi-bin/nginxauth.cgi; #... }

4.通过指定 <code>proxy_pass_error_message</code> 指令来指定是否将来自身份验证服务器的错误通知给用户。 当邮箱内存不足时,这可能很方便:

复制代码
1
2
3
4
5
6
7
mail { server_name mail.example.com; auth_http localhost:9000/cgi-bin/nginxauth.cgi; proxy_pass_error_message on; #... }

5.使用<code>server</code> 块配置每个SMTP、IMAP 或POP3 服务器。

  • 与带有 <code>listen</code> 指令的指定协议对应的端口号
  • 带有 <code>protocol</code> 指令的协议(如果未指定,将从 <code>listen</code> 指令中指定的端口自动检测)
  • 允许使用 <code>imap_auth</code> , <code>pop3_auth</code>  和 <code>smtp_auth</code> 指令的身份验证方法:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server { listen 25; protocol smtp; smtp_auth login plain cram-md5; } server { listen 110; protocol pop3; pop3_auth plain apop cram-md5; } server { listen 143; protocol imap; }

5.为邮件代理设置身份验证

来自客户端的每个 POP3/IMAP/SMTP 请求将首先在外部 HTTP 身份验证服务器上或通过身份验证脚本进行身份验证。 NGINX 邮件服务器代理必须拥有身份验证服务器。 服务器可以按照基于HTTP协议的NGINX认证协议自行创建。

如果认证成功,认证服务器将选择一个上游服务器并重定向请求。 在这种情况下,来自服务器的响应将包含以下几行:

复制代码
1
2
3
4
HTTP/1.0 200 OK Auth-Status: OK Auth-Server: <host> # the server name or IP address of the upstream server that will used for mail processing Auth-Port: <port> # the port of the upstream server

如果认证失败,认证服务器将返回错误信息。 在这种情况下,来自服务器的响应将包含以下几行:

复制代码
1
2
3
HTTP/1.0 200 OK Auth-Status: <message> # an error message to be returned to the client, for example “Invalid login or password” Auth-Wait: <number> # the number of remaining authentication attempts until the connection is closed

请注意,在这两种情况下,响应都将包含 HTTP/1.0 200 OK,这可能会令人困惑。

有关身份验证服务器的请求和响应的更多示例,请参阅 NGINX 参考文档中的 ngx_mail_auth_http_module。

6.为邮件代理设置 SSL/TLS

通过 SSL/TLS 使用 POP3/SMTP/IMAP,可以确保在客户端和邮件服务器之间传递的数据是安全的。

为邮件代理启用 SSL/TLS:

1.通过在命令行中输入 nginx -V 命令,然后在输出中查找 with --mail_ssl_module 行,确保 NGINX 配置了 SSL/TLS 支持:

复制代码
1
2
$ nginx -V configure arguments: ... with--mail_ssl_module

2.确保已获得服务器证书和私钥并将它们放在服务器上。 证书可以从受信任的证书颁发机构 (CA) 获取或使用 SSL 库(例如 OpenSSL)生成。

3.使用 <code>ssl</code> 指令为邮件代理启用 SSL/TLS。 如果在 <code>mail</code> 上下文中指定了指令,则将为所有邮件代理服务器启用 SSL/TLS。 还可以使用 <code>starttls</code> 指令启用 STLS 和 STARTTLS:

复制代码
1
ssl on;

或者

复制代码
1
starttls on;

4.添加 SSL 证书:使用 <code>ssl_certificate</code> 指令指定证书的路径(必须是 PEM 格式),并在 <code>ssl_certificate_key</code> 指令中指定私钥的路径 :

复制代码
1
2
3
4
5
mail { #... ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.key; }

5.只能使用带有 <code>ssl_protocols</code> 和 <code>ssl_ciphers</code> 指令的 SSL/TLS 的强版本和密码,或者可以设置自己的首选协议和密码:

复制代码
1
2
3
4
5
mail { #... ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; }

7.为邮件代理优化 SSL/TLS

这些提示将帮助 NGINX 邮件代理更快更安全:

1.使用 <code>worker_processes</code> 指令在与 <code>mail</code> 上下文相同的级别上设置工作进程的数量等于处理器的数量:

复制代码
1
2
3
4
worker_processes auto; mail { #... }

2.使用 <code>ssl_session_cache</code> 指令启用共享会话缓存并禁用内置会话缓存:

复制代码
1
2
3
4
5
6
7
worker_processes auto; mail { #... ssl_session_cache shared:SSL:10m; #... }

3.可以使用 <code>ssl_session_timeout</code> 指令增加会话生存期,默认情况下为 5 分钟:

复制代码
1
2
3
4
5
6
7
8
worker_processes auto; mail { #... ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; #... }

8.完整示例

复制代码
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
worker_processes auto; mail { server_name mail.example.com; auth_http localhost:9000/cgi-bin/nginxauth.cgi; proxy_pass_error_message on; ssl on; ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; server { listen 25; protocol smtp; smtp_auth login plain cram-md5; } server { listen 110; protocol pop3; pop3_auth plain apop cram-md5; } server { listen 143; protocol imap; } }

在此示例中,有三个电子邮件代理服务器:SMTP、POP3 和 IMAP。 每个服务器都配置了 SSL 和 STARTTLS 支持。 SSL 会话参数将被缓存。

代理服务器使用 HTTP 认证服务器——它的配置超出了本文的范围。 来自服务器的所有错误消息都将返回给客户端。
 

最后

以上就是无心衬衫最近收集整理的关于Nginx【有与无】【NP-AG8-1】邮件代理服务器1.将 NGINX 配置为邮件代理服务器2.介绍3.先决条件4.配置 SMTP/IMAP/POP3 邮件代理服务器5.为邮件代理设置身份验证6.为邮件代理设置 SSL/TLS7.为邮件代理优化 SSL/TLS8.完整示例的全部内容,更多相关Nginx【有与无】【NP-AG8-1】邮件代理服务器1.将内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(81)

评论列表共有 0 条评论

立即
投稿
返回
顶部