RT-Extension-Nginx

 view release on metacpan or  search on metacpan

lib/RT/Extension/Nginx.pm  view on Meta::CPAN


=head2 Reverse proxy like setup

Two servers schema with web server in front and FastCGI (FCGI)
server running RT as backend. Nginx buffers replies from FCGI, so
heavy FCGI processes get free and ready to serve next request
before user gets the current request.

=head2 Forking FCGI

FCGI processes are forked so share some memory between processes
lowering memory footprint.

=head2 Serving images without FCGI

Nginx serves /NoAuth/images/ location from files without touching
FCGI and does it properly accounting local directory and plugins'
directories.

=head2 Semi-static serving of css and js

Files served from /NoAuth/css/ and /NoAuth/js/ locations are stored
on first request for re-use.

=head2 Content gziping

Html, css and js gzipped. For example size of the primary css file
drops from 78k down to 19kb.

=head2 TODO

A few things can be improved within RT and this extension, but it's
a good start.

=cut

sub RootPath { return (shift)->CreateVarDir }
sub FcgiTempPath { return (shift)->CreateVarDir('fcgi.temp') }
sub FcgiStoragePath { return (shift)->CreateVarDir('fcgi.storage') }

sub NginxConfigPath {
    return File::Spec->catfile( (shift)->RootPath, 'nginx.conf' );
}

sub CreateVarDir {
    my $self = shift;
    my $path = File::Spec->catdir( $RT::VarPath, 'nginx', @_ );
    make_path( $path );
    return $path;
}

sub SetupRights {
    my $self = shift;

    my ($wuid, $wgid) = ( ($self->GetWebUser)[0], ($self->GetWebGroup)[0] );
    my ($rtuid, $rtgid) = (stat $RT::EtcPath)[4, 5];

    my $root = $self->RootPath;

    chmod 0400, map File::Spec->catfile($root, $_), $self->Templates;
    chown $rtuid, $rtgid, map File::Spec->catfile($root, $_), $self->Templates;

    chmod 0770, $self->FcgiTempPath, $self->FcgiStoragePath;
    chown $wuid, $wgid, $self->FcgiTempPath, $self->FcgiStoragePath;

    chmod 0755, $self->RootPath;
    chown $rtuid, $rtgid, $self->RootPath;
}

sub Templates {
    return qw(nginx.conf rt.server.conf fcgi.include.conf mime.types);
}

sub FindExecutable {
    my $self = shift;
    my $name = shift;

    foreach my $dir ( File::Spec->path ) {
        my $file = File::Spec->catfile( $dir, $name );
        return $file if -e $file && -x _;
    }
    return undef;
}

sub GetWebUser {
    my $self = shift;
    my $id = (stat $RT::MasonDataDir)[4];
    return ($id, getpwuid $id);
}

sub GetWebGroup {
    my $self = shift;
    my $id = (stat $RT::MasonDataDir)[5];
    return ($id, getgrgid $id);
}

sub GetSystemUser {
    my $self = shift;
    return ($>, getpwuid $>);
}

sub GenerateFile {
    my $self = shift;
    my $name = shift;
    my $stash = shift;

    require RT::Plugin;
    my $from = RT::Plugin->new( name => 'RT::Extension::Nginx' )->Path('etc');

    return $self->ParseTemplate(
        From  => [$from, $name],
        To    => [$stash->{'nginx_root'}, $name],
        Stash => $stash,
    );
}

sub ParseTemplate {
    my $self = shift;
    my %args = @_;

    $_ = File::Spec->catfile(@$_) foreach grep ref $_, $args{'From'}, $args{'To'};

    use Text::Template;
    my $template = Text::Template->new(
        TYPE       => 'FILE',
        SOURCE     => $args{'From'},
        DELIMITERS => [qw(<% %>)],



( run in 0.523 second using v1.01-cache-2.11-cpan-71847e10f99 )