ติดตั้ง Nginx ทำหน้าที่เป็น mail proxy (IMAP, POP3, SMTP) บน CentOS7 เขียนเมื่อ 2014.08.21 โดย

หลังจากลองหา solution เพื่อสร้าง proxy สำหรับ mail services ลองมาหลายตัว dovecot ก็พอทำได้ แต่หาข้อมูลยากจึงทำสำเร็จแค่ IMAP 

หาไปหามาก็กลับมาที่ Nginx mail module ซึ่งง่ายกว่ามาก

โดยเงื่อนไขคือทำหน้าที่เป็น mail proxy โดยต้องส่ง authentication ไปที่เครื่องปลายทาง แต่ละเครื่อง

ติดตั้ง Nginx 1.6.1

เริ่มจากติดตั้งโปรแกรมที่สำคัญสำหรับการ compile Nginx

yum install -y nano wget pcre-devel zlib-devel patch gcc gcc-c++ openssl-devel

ทำการดาวน์โหลด Nginx แตกไฟล์ให้เรียบร้อย

wget http://nginx.org/download/nginx-1.6.1.tar.gz
tar xf nginx-1.6.1.tar.gz
cd nginx-1.6.1

เริ่มทำการ compile และติดตั้ง

./configure --with-http_ssl_module --with-mail --with-mail_ssl_module
make && make install

สร้าง init script

nano /etc/init.d/nginx

ใส่เนื้อหาตามนี้ แล้วทำการบันทึก

#!/bin/sh

# nginx - this script starts and stops the nginx daemin
#
#
#
# chkconfig:   - 85 15
#
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#
#               proxy and IMAP/POP3 proxy server
#
# processname: nginx
#
# config:      /usr/local/nginx/conf/nginx.conf
#
# pidfile:     /usr/local/nginx/logs/nginx.pid
#
 
#
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
lockfile=/var/lock/subsys/nginx

# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
 
start() {  
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
เปลี่ยน mode ให้ทำการได้
chmod +x /etc/init.d/nginx
เปิด port ที่ firewalld
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload
หรือจะทำการปิดการทำงานของ firewalld ก็ได้
systemctl stop firewalld
systemctl disable firewalld

มาเริ่มตั้งค่าให้กับ mail proxy โดยเพิ่ม config ของ mail ลงไป นอก http{...} นะครับ
mail {
  proxy  on;

  imap_capabilities  "IMAP4rev1"  "UIDPLUS"; ## default
  server {
    auth_http http://localhost/auth-imap;
    listen     143;
    protocol   imap;
  }

  pop3_capabilities  "TOP"  "USER";
  server {
    auth_http http://localhost/auth-pop3;
    listen     110;
    protocol   pop3;
  }

  smtp_capabilities "SIZE 10485760" ENHANCEDSTATUSCODES 8BITMIME DSN;
  xclient           off;
  server {
    auth_http http://localhost/auth-smtp;
    listen   25;
    protocol smtp;
  }
}
จากการตั้งค่าข้างบน เราทำการแยกแต่ละ mail service ออกจากกัน โดยเป็นให้ทำงานทั้ง IMAP, POP3 และ SMTP แต่ละบริการก็ส่งไปยืนยันตัวที่ URI ที่ต่างกัน โดยใช้ module auth_http ในการทำงาน

มาตั้งค่าในส่วนของ http เพื่อรองรับการ authentication ในที่นี้จะทำการส่งไป authenticate ที่เครื่องปลายทาง (192.168.1.10) โดยเขียนใน block ของ http {...} ตามนี้
server {
        listen       80;
        server_name  localhost;

        location = /auth-imap {
            add_header Auth-Status OK;
            add_header Auth-Server 192.168.1.10;  # backend ip
            add_header Auth-Port   143;        # backend port
            return 204;
        }

        location = /auth-pop3 {
            add_header Auth-Status OK;
            add_header Auth-Server 192.168.1.10;  # backend ip
            add_header Auth-Port   110;        # backend port
            return 204;
        }

        location = /auth-smtp {
            add_header Auth-Status OK;
            add_header Auth-Server 192.168.1.10;  # backend ip
            add_header Auth-Port   25;        # backend port
            return 204;
        }
จากนั้นทำการบันทึกแล้ว restart nginx ก็เรียบร้อยครับ

อย่าลืมปิดบริการอื่นๆ ที่ทำงานอยู่บน port เหล่านั้นด้วยนะครับ เดี๋ยว Nginx จะทำงานไม่ได้





คำเตือนคำเตือน เนื้อหาต่างๆ ในบทความ รวมถึงรูปภาพทั้งหมดในบทความนี้ เป็นความเห็นส่วนตัวของผู้เขียนแต่ละคน ซึ่งแต่ละคนได้ทำการลงทะเบียน และเขียนบทความลงใน Modoeye Articles นี้โดยไม่มีค่าธรรมเนียมใดๆ บทความเหล่านี้เป้าหมายเพื่อการศึกษา และความบันเทิงเท่านั้น การนำส่วนหนึ่งส่วนใดของบทความไปใช้งาน ควรทำการอ้างอิงถึงผู้เขียนและแหล่งที่มาด้วย