Fayland Lam wrote:
let me explain in Chinese, :)

in the org.conf
--------------------------------
    <Perl>
    use lib qw(/var/org);
    </Perl>

    PerlModule Zorpia::RewriteOrg
    PerlTransHandler Zorpia::RewriteOrg
--------------------------------

The PerlTransHandler 能把 url 转变为另一个 url, 比如我们就是将类如
http://org.zorpia.com/0/1584/10141597.0f8688.jpg
这样的 url 转变为
http://org.zorpia.com/org62/0/1584/10141597.0f8688.jpg

这里因为要访问数据库,所以用 mod_rewrite 是不行的。只能依靠 modperl
而 modperl 只是跟 Apache 打交道,不能将这个地址访问到另外服务器上的文 件。(也就是访问不到 org62 这个服务器)。当然它可以发送一个 header: Location 转到 org62, 但是这样子地址栏是会变到 org62 。

怎么样才能在这个 apache 里访问 org62 服务器上的东西。那只能依靠 mod_proxy
--------------------------------
    RewriteEngine On
    RewriteLog /var/log/httpd/rewrite_log
    RewriteLogLevel 3
    RewriteRule ^/org(\d+)/(.*) http://org$1.zorpia.com/$2 [proxy,last]
--------------------------------
这样我们就可以将 http://org.zorpia.com/org62/0/1584/10141597.0f8688.jpg 用 proxy 访问 http://org62.zorpia.com/0/1584/10141597.0f8688.jpg
对于用户来说这是不可见的。

That's ALL.

而 PerlTransHandler Zorpia::RewriteOrg 也很简单。
--------------------------------
if ($r->uri =~ m|/(\d+)/(\d+)/([\w\.]+)\.jpg$|) {
        my $org = &get_org_from_photo_id($1, $2);
        $r->uri("/org$org/$1/$2/$3\.jpg");
    }
--------------------------------
将访问的 url 转一下。老板你看看就明白了。

另外我想说的是,目前我用的是 Apache2 和 modperl2, 因为 alpha.zorpia 这台 机子用的是这两个版本的。如果要转到 apache 1.3 和 modperl1 也是很容易的。 还有就是 log 问题。如果到最后发布的时候不需要 log(这样能稍微节省一点点 资源),就把 conf 里的 RewriteLog 和 RewriteLogLevel 注释掉。还有 RewriteOrg.pm 与 Apache2::Log 相关的内容。 另外还有个是 RewriteOrg.pm 目前是直接用 DBI 连接的。如果有可能我们可以迁 移到 Apache::DBI.

Thanks. 如果有问题就问我。:) 我拿着工资的。呵呵。


------------------------------------------------------------------------

<VirtualHost *:80>
#       DocumentRoot /var/www/Zorpia

#    SetEnv PERL5LIB "/var/www/fayland/Zorpia/lib"
#    PerlSwitches -I/var/www/Zorpia/lib
        <Perl>
        use lib qw(/var/org);
        </Perl>

    PerlModule Zorpia::RewriteOrg
    PerlTransHandler Zorpia::RewriteOrg

    RewriteEngine On
    RewriteLog /var/log/httpd/rewrite_log
    RewriteLogLevel 3
    RewriteRule ^/org(\d+)/(.*) http://org$1.zorpia.com/$2 [proxy,last]

    ErrorLog /var/log/httpd/org.log
    ServerName org.zorpia.com
</VirtualHost>


------------------------------------------------------------------------

package Zorpia::RewriteOrg;
#use Apache::Constants qw(DECLINED);
use Apache2::RequestRec ();
use Apache2::Const -compile => qw(DECLINED);
use Apache2::Log;

use strict;
use Zorpia::DB();

sub handler {
    my $r = shift;

    # convert something like http://org.zorpia.com/0/1584/10141597.0f8688.jpg
    # to http://org.zorpia.com/org62/0/1584/10141597.0f8688.jpg
    # and we'll use mod_rewrite/mod_proxy to 
http://org62.zorpia.com/0/1584/10141597.0f8688.jpg
$r->log_error($r->uri); if ($r->uri =~ m|/(\d+)/(\d+)/([\w\.]+)\.jpg$|) {
        my $org = &get_org_from_photo_id($1, $2);
        $r->uri("/org$org/$1/$2/$3\.jpg");
    }

    #return DECLINED;
    return Apache2::Const::DECLINED;
}

# borrow from photoaction.cgi sub original
sub get_org_from_photo_id {
    my ($directory_1, $directory_2) = @_;
my $dbh = DBI->connect("DBI:mysql:zorpia:z3-1.zorpia.com", 'zorpia', 'fok8fok2', { PrintError => 1,
                             RaiseError => 1,
                             AutoCommit => 1, }) or die $DBI::errstr;
my $sql_statement = <<SQL_STATEMENT; SELECT server, base_directory FROM directory_location WHERE directory_1 = ? AND directory_2 = ? AND type='org'
SQL_STATEMENT
    my $sth = $dbh->prepare($sql_statement);
    $sth->execute($directory_1, $directory_2);
    my ($server, $base_directory) = $sth->fetchrow_array;
    $sth->finish;

    my $index_1 = $1 if ($server =~ m/(\d+)$/);
        my $index_2 = $1 if ($base_directory =~ m/(\d+)$/);

    return "$index_1$index_2";
}

1;

sorry, wrong post

Reply via email to