简体   繁体   中英

Apache-to-PSGI proxy over a Unix domain socket

We have many (isolated) installations of a PSGI app that run on the same machine and thus require their PSGI servers to run on unique ports. This is less than ideal from a resource/management perspective, but it also requires the (yet-unmeasured and possibly insignificant) "overhead" of TCP/IP when a Unix domain socket would seem to be more obvious choice when running on the same machine.

Fortunately, the app works under Plack's HTTP interface (proxied from Apache via mod_proxy's "ProxyPass"), but unfortunately, it breaks under the FastCGI interface (see: https://stackoverflow.com/questions/14643165/can-psgi-apps-fork-under-plackhandlerfcgi ).

Other than mod_fastcgi's FastCgiExternalServer (or patching mod_proxy with this untested, user-contributed patch: http://mail-archives.apache.org/mod_mbox/httpd-dev/201207.mbox/%3C20120731200351.GB11038@gmail.com%3E ), is there any way to proxy Apache connections over a Unix domain socket to a PSGI app?

Proxying to a Unix domain socket should work with mod_proxy since Apache 2.4.7 and Starman .

Another approach is to run the different PSGI apps in a single process. I use something similar to the following wrapper app to achieve this:

use strict;
use warnings;

use lib qw(
    /path/to/app1
    /path/to/app2
    /path/to/app3
);

use Plack::Builder;
use Plack::Util;

sub load_psgi_in_dir {
    my ($dir, $psgi) = @_;
    my $app = Plack::Util::load_psgi("$dir/$psgi");
    return sub {
        chdir($dir);
        return $app->(@_);
    };
}

builder {
    mount 'http://app1.com/' => load_psgi_in_dir(
        '/path/to/app1',
        'app1.psgi',
    );
    mount 'http://app2.com/' => load_psgi_in_dir(
        '/path/to/app2',
        'app2.psgi',
    );
    mount 'http://app3.com/' => load_psgi_in_dir(
        '/path/to/app3',
        'app3.psgi',
    );
};

The only problem I had was that some apps used different versions of a local module with the same name. After fixing that everything worked fine.

A considerable benefit of this approach is that you can share workers across all your apps which reduces memory usage (or enables you to add more workers).

There is mod_proxy_fdpass which allows Apache to proxy to domain sockets, although I haven't tried it.

I was personally recommend using the standard each-app-on-a-port arrangement, unless you can measure the overhead to be worth doing something unconventional for.

You also have the option of using one private-to-the-server IP address per app, and having them all run on port 80 on their private IPs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM