<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.nginx.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.nginx.org/index.php?title=ImapAuthenticateWithEmbeddedPerlScript&amp;feed=atom&amp;action=history</id>
		<title>ImapAuthenticateWithEmbeddedPerlScript - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.nginx.org/index.php?title=ImapAuthenticateWithEmbeddedPerlScript&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://wiki.nginx.org/index.php?title=ImapAuthenticateWithEmbeddedPerlScript&amp;action=history"/>
		<updated>2013-06-20T06:28:25Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.19.0</generator>

	<entry>
		<id>http://wiki.nginx.org/index.php?title=ImapAuthenticateWithEmbeddedPerlScript&amp;diff=179&amp;oldid=prev</id>
		<title>MichaelLustfield: moved NginxImapAuthenticateWithEmbeddedPerlScript to ImapAuthenticateWithEmbeddedPerlScript:&amp;#32;Removing Nginx prefix from page titles</title>
		<link rel="alternate" type="text/html" href="http://wiki.nginx.org/index.php?title=ImapAuthenticateWithEmbeddedPerlScript&amp;diff=179&amp;oldid=prev"/>
				<updated>2010-09-22T19:28:23Z</updated>
		
		<summary type="html">&lt;p&gt;moved &lt;a href=&quot;/NginxImapAuthenticateWithEmbeddedPerlScript&quot; class=&quot;mw-redirect&quot; title=&quot;NginxImapAuthenticateWithEmbeddedPerlScript&quot;&gt;NginxImapAuthenticateWithEmbeddedPerlScript&lt;/a&gt; to &lt;a href=&quot;/ImapAuthenticateWithEmbeddedPerlScript&quot; title=&quot;ImapAuthenticateWithEmbeddedPerlScript&quot;&gt;ImapAuthenticateWithEmbeddedPerlScript&lt;/a&gt;: Removing Nginx prefix from page titles&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Using nginx-embedded-perl module on the same server as the imap/pop proxy as the auth backend ==&lt;br /&gt;
&lt;br /&gt;
Note: this solution will block entire nginx worker when reading user information from DB and therefore not recommended for real use.&lt;br /&gt;
&lt;br /&gt;
* Note2: This solution is being used at at ISP with 35000+ mailboxes for almost 2 years now fine. If you want  shameful plug, the ISP is Worldsoft ([http://worldsoft.info] )&lt;br /&gt;
&lt;br /&gt;
Start with the configuration from [NginxImapProxyExample] . For detail information about different configuration parameters, see the [NginxMailCoreModule]  page.&lt;br /&gt;
&lt;br /&gt;
Configure nginx with embedded perl and mail&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
./configure --with-http_perl_module --with-mail&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
nginx/conf/nginx.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;geshi lang=&amp;quot;nginx&amp;quot;&amp;gt;&lt;br /&gt;
user  nobody;&lt;br /&gt;
worker_processes  1;&lt;br /&gt;
error_log  logs/error.log  info;&lt;br /&gt;
pid        logs/nginx.pid;&lt;br /&gt;
&lt;br /&gt;
events {&lt;br /&gt;
  worker_connections  1024;&lt;br /&gt;
  multi_accept on;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
http {&lt;br /&gt;
  perl_modules  perl/lib;&lt;br /&gt;
  perl_require  mailauth.pm;&lt;br /&gt;
&lt;br /&gt;
  server {&lt;br /&gt;
    location /auth {&lt;br /&gt;
      perl  mailauth::handler;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
mail {&lt;br /&gt;
  auth_http  127.0.0.1:80/auth;&lt;br /&gt;
&lt;br /&gt;
  pop3_capabilities  &amp;quot;TOP&amp;quot;  &amp;quot;USER&amp;quot;;&lt;br /&gt;
  imap_capabilities  &amp;quot;IMAP4rev1&amp;quot;  &amp;quot;UIDPLUS&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
  server {&lt;br /&gt;
    listen     110;&lt;br /&gt;
    protocol   pop3;&lt;br /&gt;
    proxy      on;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  server {&lt;br /&gt;
    listen     143;&lt;br /&gt;
    protocol   imap;&lt;br /&gt;
    proxy      on;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/geshi&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ultrafast nginx based authentifier.&lt;br /&gt;
nginx/perl/lib/mailauth.pm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;geshi lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package mailauth;&lt;br /&gt;
use nginx;&lt;br /&gt;
use DBI;&lt;br /&gt;
my $dsn=&amp;quot;DBI:mysql:database=DBNAME;host=HOSTNAME&amp;quot;;&lt;br /&gt;
our $dbh=DBI-&amp;gt;connect_cached($dsn, 'dbusername', 'dbpass', {AutoCommit =&amp;gt; 1});&lt;br /&gt;
our $sth=$dbh-&amp;gt;prepare(&amp;quot;select password,mail_server from mailaccounts where username=? limit 1&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
our $auth_ok;&lt;br /&gt;
our $mail_server_ip={};&lt;br /&gt;
our $protocol_ports={};&lt;br /&gt;
$mail_server_ip-&amp;gt;{'mailhost01'}=&amp;quot;192.168.1.22&amp;quot;;&lt;br /&gt;
$mail_server_ip-&amp;gt;{'mailhost02'}=&amp;quot;192.168.1.33&amp;quot;;&lt;br /&gt;
$protocol_ports-&amp;gt;{'pop3'}=110;&lt;br /&gt;
$protocol_ports-&amp;gt;{'imap'}=143;&lt;br /&gt;
&lt;br /&gt;
sub handler {&lt;br /&gt;
  my $r = shift;&lt;br /&gt;
  $auth_ok=0;&lt;br /&gt;
&lt;br /&gt;
  $sth-&amp;gt;execute($r-&amp;gt;header_in(&amp;quot;Auth-User&amp;quot;));&lt;br /&gt;
  my $hash=$sth-&amp;gt;fetchrow_hashref();&lt;br /&gt;
  # assuming that the query results password and mail_server&lt;br /&gt;
  # assuming that the password is in crypt format&lt;br /&gt;
&lt;br /&gt;
  if (crypt($r-&amp;gt;header_in(&amp;quot;Auth-Pass&amp;quot;), $hash-&amp;gt;{'password'}) eq $r-&amp;gt;header_in(&amp;quot;Auth-Pass&amp;quot;)){&lt;br /&gt;
    $auth_ok=1;&lt;br /&gt;
  }&lt;br /&gt;
  if ($auth_ok==1){&lt;br /&gt;
    $r-&amp;gt;header_out(&amp;quot;Auth-Status&amp;quot;, &amp;quot;OK&amp;quot;) ;&lt;br /&gt;
    $r-&amp;gt;header_out(&amp;quot;Auth-Server&amp;quot;, $mail_server_ip-&amp;gt;{$hash-&amp;gt;{'mail_server'}});&lt;br /&gt;
    $r-&amp;gt;header_out(&amp;quot;Auth-Port&amp;quot;, $protocol_ports-&amp;gt;{$r-&amp;gt;header_in(&amp;quot;Auth-Protocol&amp;quot;)});&lt;br /&gt;
  } else {&lt;br /&gt;
    $r-&amp;gt;header_out(&amp;quot;Auth-Status&amp;quot;, &amp;quot;Invalid login or password&amp;quot;) ;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  $r-&amp;gt;send_http_header(&amp;quot;text/html&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
  return OK;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
1;&lt;br /&gt;
__END__&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/geshi&amp;gt;&lt;/div&gt;</summary>
		<author><name>MichaelLustfield</name></author>	</entry>

	</feed>