Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Created May 2, 2017 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aya-eiya/2b3163375fccc4f4e6f6203699f65f74 to your computer and use it in GitHub Desktop.
Save aya-eiya/2b3163375fccc4f4e6f6203699f65f74 to your computer and use it in GitHub Desktop.
NginxのTCP Proxy機能とMail Proxy機能を使って常にSMTPをlocalhost:25で受けられるようにする ref: http://qiita.com/aya_eiya/items/dbd2ffff1f07a7bfedd6
$ nginx -V 2>&1 | sed -e 's/--/\n--/g' | grep -e '\(version\|stream\|mail\)'
nginx version: nginx/1.11.9
--with-stream=dynamic
--with-stream_ssl_module
--with-mail=dynamic
--with-mail_ssl_module
$ sudo ln -sf /etc/nginx/sites-available/mail_auth /etc/nginx/sites-enabled/mail_auth
$ sudo ls -l /etc/nginx/sites-enabled/mail_auth
lrwxrwxrwx 1 root root ss MMM d hh:mm /etc/nginx/sites-enabled/mail_auth -> /etc/nginx/sites-available/mail_auth
$ sudo service nginx reload
$ tail -n100 /var/log/nginx/mail-*.log
==> /var/log/nginx/mail-send.log <==
localhost - - [02/May/2017:18:31:31 +0900] 0.000 "app-srv.c.app-grp.internal" "MAIL FROM:<test@mail.com>" "RCPT TO:<someone@foo.org>"
==> /var/log/nginx/mail-smtp-proxy.log <==
2017/05/02 18:31:31 [info] 17222#17222: *3811 client 127.0.0.1:54096 connected to 0.0.0.0:25
2017/05/02 18:31:31 [info] 17222#17222: *3811 client logged in, client: 127.0.0.1, server: 0.0.0.0:25
2017/05/02 18:31:32 [info] 17222#17222: *3811 proxied session done, client: 127.0.0.1, server: 0.0.0.0:25
==> /var/log/nginx/mail-tcp-proxy.log <==
2017/05/02 18:31:31 [info] 17221#17221: *3815 client 127.0.0.1:38740 connected to 0.0.0.0:2525
2017/05/02 18:31:31 [info] 17221#17221: *3815 proxy nnn.nnn.nnn.nnn:33726 connected to xxx.xxx.xxx.xxx:2525
2017/05/02 18:31:32 [info] 17221#17221: *3815 upstream disconnected, bytes from/to client:832/267, bytes from/to upstream:267/832
log_format mail-auth-log '$host - $remote_user [$time_local] '
'$request_time '
'"$http_auth_smtp_helo" '
'"$http_auth_smtp_from" '
'"$http_auth_smtp_to"';
server {
listen 8025;
server_name localhost;
charset utf-8;
access_log /var/log/nginx/mail-send.log mail-auth-log;
error_log /var/log/nginx/mail-auth-error.log;
location ~ /mail_auth/ {
add_header Auth-Status OK;
add_header Auth-Server 127.0.0.1;
add_header Auth-Port 2525;
empty_gif;
break;
}
}
stream {
upstream smtp-server{
server my.mail.server1.com:25 weight=3;
server my.mail.server2.com:25;
server my.mail.server3.com:25;
}
server {
listen 2525;
proxy_pass smtp-server;
error_log /var/log/nginx/mail-tcp-proxy.log info;
}
}
mail {
# See /etc/nginx/sites-available/mail_auth
auth_http localhost:8025/mail_auth/;
proxy on;
proxy_pass_error_message on;
smtp_auth none;
smtp_capabilities "SIZE 10485760" 8BITMIME ENHANCEDSTATUSCODES DSN ;
ssl off;
xclient off;
server {
listen 25;
protocol smtp;
error_log /var/log/nginx/mail-smtp-proxy.log info;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment