Nginx

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

  • September 8, 2017

我想做一個郵件伺服器。但我希望我的郵件伺服器不直接連接到網際網路。所以我製作了另一台可以通過網際網路訪問的伺服器,並使用帶有 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

你的 postfix 只監聽 localhost 介面,它不接受來自其他機器的連接。將其配置為監聽0.0.0.0

通過在前面/etc/postfix/main.cf添加一個註釋掉該行。#

#inet_interfaces = 127.0.0.1

或將地址更改為0.0.0.0。之後重新啟動後綴。

除了 Gerald 的回答之外,您還註釋掉了您部分中的所有protocol指令。server {}它們是確定正確協議所必需的。

我希望你已經在關注官方文章,將 NGINX 配置為郵件代理伺服器

至少添加starttls on;和所有相關的東西都是個好主意……

引用自:https://serverfault.com/questions/872659