Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httpd - reverse proxy on multiple ports

What I want to be able to do is take the following:

http://localhost:10000
http://localhost:11000
http://localhost:12000

and route them respectively like follows:

http://my-app (this is port 10000 traffic)
http://my-app/app  (this is port 11000 traffic)
http://my-app/blog (this is port 12000 traffic)

Here is my conf.d file -

<VirtualHost *:80>
  ServerName my-app.domain.com
  ServerAlias my-app

  Redirect / https://my-app.domain.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName my-app.domain.com
  ServerAlias my-app

  Include ssl/default/ssl.cfg

  RewriteEngine On
  ProxyRequests Off
  ProxyPreserveHost On
  RemoteIPHeader X-Forwarded-For
  RequestHeader set X-FORWARDED-SSL on
  RequestHeader set X-FORWARDED_PROTO https


  ProxyTimeout 900
  TimeOut 900

  RewriteRule ^$ / [R]

  ProxyPass / http://localhost:10000/
  ProxyPassReverse / http://localhost:10000/

  RewriteRule ^/app/(.*) http://localhost:11000/$1 [P,L]
  ProxyPassReverse /app/ http://localhost:11000

</VirtualHost>

The re-direct is working for the initial port, but not for traffic going to port 11000. I'm sure I'm doing something stupid but I don't know what.

like image 478
whoisearth Avatar asked Dec 29 '16 18:12

whoisearth


People also ask

Is Apache httpd a reverse proxy?

In addition to being a "basic" web server, and providing static and dynamic content to end-users, Apache httpd (as well as most other web servers) can also act as a reverse proxy server, also-known-as a "gateway" server.

What is ProxyPass and ProxyPassReverse?

ProxyPassReverse will intercept those headers, and rewrite them to match the Apache proxy server. ProxyPass will create a reverse proxy. A reverse proxy (or gateway), appears to the client just like an ordinary web server. The client makes ordinary requests for content in the namespace of the reverse proxy.

What is Apache ProxyPreserveHost?

The ProxyPreserveHost directive is used to instruct Apache mod_proxy, when acting as a reverse proxy, to preserve and retain the original Host: header from the client browser when constructing the proxied request to send to the target server.


1 Answers

You need to specify the most "specific" paths first when using proxypass, specify /blog/ /app/ first and then /. If you don't do it this way ProxyPAss / will override the others.

RewriteRule ^$ / [R] <-- is not happening since in virtualhost only ^/$ will match and ^/$ is already /, so it is not working, if it did it would loop.

Aslo, don't use mod_rewrite since there is no need to proxy with it or at least you are not doing anything that proxypass or proxypassmatch wouldn't do alone, and match slashes when using ProxyPass (if there is a ending slash in source add in target, if there is not, dont, otherwise unexpected behaviour can happen with responses from the backend), and as stated first, specify most specific paths first:

So, Remove mod_rewrite directives entirely and:

ProxyPass /app/ http://localhost:11000/
ProxyPassReverse /app/ http://localhost:11000/

ProxyPass /blog/ http://localhost:12000/
ProxyPassReverse /blog/ http://localhost:12000/

ProxyPass / http://localhost:10000/
ProxyPassReverse / http://localhost:10000/
like image 121
ezra-s Avatar answered Oct 12 '22 18:10

ezra-s