Apache-to-PSGI proxy over a Unix domain socket

泄露秘密 提交于 2019-12-12 01:57:32

问题


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?


回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/14648515/apache-to-psgi-proxy-over-a-unix-domain-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!