如何使用 nginx 反向代理设置 postfix

如何使用 nginx 反向代理设置 postfix

我想创建一个邮件服务器。但我希望我的邮件服务器不直接连接到互联网。因此我创建了另一个可通过互联网访问的服务器,并使用 nginx 反向代理。但由于某种原因,反向代理无法连接到邮件服务器。这是我的 nginx 配置:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

stream {
        log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

        access_log  /var/log/nginx/access.stream.log proxy;

        server {
                listen 25;

                #protocol smtp;
                proxy_pass 10.0.1.15:25;
        }
        server {
                listen 110;

                #protocol pop3;
                proxy_pass 10.0.1.15:110;
        }
        server {
                listen 143;

                #protocol imap;
                proxy_pass 10.0.1.15:143;
        }
}

这是我的错误日志

2017/09/08 09:02:56 [error] 1444#1444: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 54.240.25.13, server: 0.0.0.0:25, upstream: "10.0.1.15:25", bytes from/to client:0/0, bytes from/to upstream:0/0
2017/09/08 09:06:38 [error] 1444#1444: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 209.85.128.182, server: 0.0.0.0:25, upstream: "10.0.1.15:25", bytes from/to client:0/0, bytes from/to upstream:0/0
2017/09/08 09:07:57 [error] 1444#1444: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 54.240.25.4, server: 0.0.0.0:25, upstream: "10.0.1.15:25", bytes from/to client:0/0, bytes from/to upstream:0/0
2017/09/08 09:16:10 [notice] 1951#1951: signal process started
2017/09/08 09:16:42 [error] 1952#1952: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 74.125.82.46, server: 0.0.0.0:25, upstream: "10.0.1.15:25", bytes from/to client:0/0, bytes from/to upstream:0/0
2017/09/08 09:19:39 [error] 1952#1952: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 54.240.25.8, server: 0.0.0.0:25, upstream: "10.0.1.15:25", bytes from/to client:0/0, bytes from/to upstream:0/0
2017/09/08 09:22:51 [error] 1952#1952: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 74.125.82.50, server: 0.0.0.0:25, upstream: "10.0.1.15:25", bytes from/to client:0/0, bytes from/to upstream:0/0
2017/09/08 09:26:10 [emerg] 9086#9086: unknown log format "main" in /etc/nginx/nginx.conf:19
2017/09/08 09:27:09 [notice] 9090#9090: signal process started
2017/09/08 09:27:53 [error] 9091#9091: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 209.85.128.176, server: 0.0.0.0:25, upstream: "10.0.1.15:25", bytes from/to client:0/0, bytes from/to upstream:0/0
2017/09/08 09:28:37 [error] 9091#9091: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 209.85.128.170, server: 0.0.0.0:25, upstream: "10.0.1.15:25", bytes from/to client:0/0, bytes from/to upstream:0/0

我已经检查了https://www.nginx.com/resources/admin-guide/mail-proxy/. 但我想将已发送的邮件重定向到我的邮件服务器

更新

[ec2-user@ip-10-0-1-15 ~]$ sudo netstat -nlp | grep :25
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1032/master
tcp6       0      0 ::1:25                  :::*                    LISTEN      1032/master

已经注释了 inet_interface 并且仍然相同

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1032/master
tcp6       0      0 ::1:25                  :::*                    LISTEN      1032/master

答案1

您的 postfix 仅监听本地主机接口,它不接受来自其他机器的连接。配置它监听0.0.0.0

通过在前面/etc/postfix/main.cf添加来注释掉该行。#

#inet_interfaces = 127.0.0.1

或者将地址改为0.0.0.0。然后重新启动 postfix。

答案2

除了 Gerald 的回答之外,您还注释掉了部分protocol内的所有指令server {}。它们是确定正确协议所必需的。

我希望你已经关注了官方文章,将 NGINX 配置为邮件代理服务器

至少添加starttls on;所有相关内容是个好主意......

相关内容