Nginx – How to setup Apache as reverse proxy to a docker container

Apache2nginx

I struggling to setup my Apache server as reverse proxy for a docker contained Greenlight instance.

In the official docs it is proposed to run Greenlight in a docker container with an Nginx instance as revers proxy (mainly to make it easy to run it alongside a BBB server).
In my setup however I do not want to run my own BBB instance but rather use Greenlight as a frontend for an external BBB server.

But for convenience I set up Greenlight in a docker container nevertheless.

But as my server has many purposes (mainly it's used to deliver several websites and provide e-mail accounts both manged by Froxlor but it works also as an Matrix server) I do not want to setup Nginx as proxy, for that would force me to substantially change my actual running Froxlor manged Apache setup.

So I was trying to configure Apache instead as an revers prox. Unfortunately in the docs is only a Nginx example:

location /b {
  proxy_pass          http://127.0.0.1:5000;
  proxy_set_header    Host              $host;
  proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
  proxy_set_header    X-Forwarded-Proto $scheme;
  proxy_http_version  1.1;
}

location /b/cable {
  proxy_pass          http://127.0.0.1:5000;
  proxy_set_header    Host              $host;
  proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;
  proxy_set_header    X-Forwarded-Proto $scheme;
  proxy_set_header    Upgrade           $http_upgrade;
  proxy_set_header    Connection        "Upgrade";
  proxy_http_version  1.1;
  proxy_read_timeout  6h;
  proxy_send_timeout  6h;
  client_body_timeout 6h;
  send_timeout        6h;
}

I tied to make this work on Apache with the following VirtualHost settings:

<VirtualHost MYIP:443>
  ServerName greenlight.example.com
ProxyPreserveHost On
ProxyRequests Off
ProxyVia On
ProxyPass / http://127.0.0.1:5000
ProxyPassReverse / http://127.0.0.1:5000   
<Location "/cable">
  ProxyPass / http://127.0.0.1:5000 connectiontimeout=6h timeout=6h
  ProxyPassReverse / http://127.0.0.1:5000
</Location>
</VirtualHost>

I divert from the example that I do not want to use a virtual subfolder »b« but rather a subdomain t redirect specific traffic to the local port 5000. Unfortunately this isn't working.
The server returns

502 Prox error

Proxy Error

The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request

Reason: Error reading from remote server

Can somebody please explain to me what could be the reason for this error? How to translate the Nginx Header settings to Apache and the other timeout settings and are they even necessary?

Best Answer

There are a few issues here:

  1. Order
    The first matched path wins. You placed / before /cable, so / will match always and /cable will never be used.
  2. matching trailing slashes
    If you end the first parameter of ProxyPass with a / you need to add one to the second, and vice versa. Otherwise you'll end up with non working URLs sent to the backend
  3. ProxyPass inside a <Location> block
    If you use ProxyPass inside a <Location> it only gets the second parameter. The first is being replaces by the <Location>.

Example:

<VirtualHost MYIP:443>
    ServerName greenlight.example.com
    ProxyPreserveHost On
    ProxyRequests Off
    ProxyVia On
    ProxyPass /cable http://127.0.0.1:5000 connectiontimeout=6h timeout=6h
    ProxyPassReverse /cable http://127.0.0.1:5000
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
</VirtualHost>

Last, but not least, currently you are proxying both locations to the same backend URL. This is usually wrong.

Related Topic