Nginx map conversion to Apache2

74 views Asked by At

I'm struggling to find an answer, is it possible de convert an Nginx map like below to Apache2 (2.4) equivalent ?

server {
    [...]
    location ~* ^(/_matrix|/_synapse) {
        proxy_pass http://$matrix_worker_upstream$request_uri;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        client_max_body_size 50M;
        proxy_http_version 1.1;
    }
    [...]
}
upstream synapse {
    server 127.0.0.1:8008;
}
upstream generic-worker {
    ip_hash;
    server 127.0.0.1:8008;
    server 127.0.0.1:8081;
}
upstream media_repository {
    server 127.0.0.1:8085;
}
map $uri $matrix_worker_upstream {
    default synapse;
    ~^/_matrix/client/(r0|v3)/sync$ generic-worker;
    ~^/_matrix/client/(api/v1|r0|v3)/events$ generic-worker;
    ~^/_matrix/client/(api/v1|r0|v3)/initialSync$ generic-worker;
    ~^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$ generic-worker;
    [...]
}

If i understand well, upstreams can be converted to proxy balancer like this ?

<Proxy balancer://generic-worker>
    BalancerMember http://127.0.0.1:8080
    BalancerMember http://127.0.0.1:8081
</Proxy>

But i don't see how to convert the map part ? (there are loads of urls inside map)

For information, this is a configuration for Synapse (Matrix) with workers. And why Apache2 ? This is for avoid extra configuration for Shibboleth with Matrix.

1

There are 1 answers

0
Aurélien Grimpard On BEST ANSWER

Here is what i got, seems to work even if other errors heads up ...

RequestHeader set X-Forwarded-For "%{REMOTE_ADDR}e"
RequestHeader set X-Forwarded-Proto expr=%{REQUEST_SCHEME}
RequestHeader set Host expr=%{HTTP_HOST}

<Proxy balancer://generic_worker>
    BalancerMember http://127.0.0.1:8008
    BalancerMember http://127.0.0.1:8081
</Proxy>

<Proxy balancer://media_repository>
    BalancerMember http://127.0.0.1:8085
</Proxy>

ProxyPassMatch /_matrix/client/(r0|v3)/sync(.*) balancer://generic_worker/_matrix/client/$1/sync$2 nocanon
ProxyPassReverse /_matrix/client/(r0|v3)/sync(.*) balancer://generic_worker/_matrix/client/$1/sync$2

ProxyPassMatch /_matrix/client/(api/v1|r0|v3)/events(.*) balancer://generic_worker/_matrix/client/$1/events$2 nocanon
ProxyPassReverse /_matrix/client/(api/v1|r0|v3)/events(.*) balancer://generic_worker/_matrix/client/$1/events$2

ProxyPassMatch /_matrix/client/(api/v1|r0|v3)/initialSync(.*) balancer://generic_worker/_matrix/client/$1/initialSync$2 nocanon
ProxyPassReverse /_matrix/client/(api/v1|r0|v3)/initialSync(.*) balancer://generic_worker/_matrix/client/$1/initialSync$2

ProxyPassMatch /_matrix/client/(api/v1|r0|v3)/rooms/(.*)/initialSync(.*) balancer://generic_worker/_matrix/client/$1/rooms/$2/initialSync$3 nocanon
ProxyPassReverse /_matrix/client/(api/v1|r0|v3)/rooms/(.*)/initialSync(.*) balancer://generic_worker/_matrix/client/$1/rooms/$2/initialSync$3


[...]

# defaut master

ProxyPassMatch /_matrix/(.*) http://127.0.0.1:8008/_matrix/$1 nocanon
ProxyPassReverse /_matrix/(.*) http://127.0.0.1:8008/_matrix/$1

ProxyPass /_synapse/client(.*) http://127.0.0.1:8008/_synapse/client$1 nocanon
ProxyPassReverse /_synapse/client(.*) http://127.0.0.1:8008/_synapse/client$1

Upstreams convert to proxy balancer but each map element are converted into proxypass rules. There should be a way to shorten this though ...

Also, Apache mods need to be enabled :

a2enmod headers
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_balancer
a2enmod lbmethod_byrequests