0

I need to define rule (using mod_rewrite i think) to map any port to portnumber.domain.com. How to do that in Apache2 ? Now i have one config for each port number like below.

# 9003.domain.com
<VirtualHost *:80>
    ServerName 9003.domain.com
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:9003/
    ProxyPassReverse / http://127.0.0.1:9003/
</VirtualHost>

# 9004.domain.com
<VirtualHost *:80>
    ServerName 9004.domain.com
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:9004/
    ProxyPassReverse / http://127.0.0.1:9004/
</VirtualHost>

# and so on....

3 Answers 3

2

You should be able to do something with ProxyPassMatch, maybe something like

ProxyPassMatch ^([0-9{2,5}]) http://127.0.0.1:$1

8
  • I don't think this will work. ProxyPassMatch operates against the URL, not the HOST name. May 17, 2013 at 17:58
  • He's looking to match the port in the URL.. my example is just theory though :)
    – NickW
    May 20, 2013 at 8:20
  • He's trying to match 9003.domain.com and proxy it to localhost:9003. Since ProxyPassMatch only tries to match against the path part of the URL, not the Hostname part you can't use it here. May 20, 2013 at 8:36
  • I think I see what you mean, but I can't find anything that says that it matches against the path only.. I ask seriously, can you link me to the documentation so I can inform myself?
    – NickW
    May 20, 2013 at 8:46
  • I have the impression that the documentation is not very clear here: httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypassmatch The description of ProxyPass makes it clear that it is path, not url that is matched. The description of ProxaPassMatch seems to imply that it is URLs that are matched, but the examples given tell a different story. May 20, 2013 at 14:01
1

I did some corrections on @Krist answer and make it works now. 403 error was from RewriteEngine On absence.

<VirtualHost *:80>
     RewriteEngine On
     ServerAlias *.domain.com
     RewriteCond %{HTTP_HOST}      (\d+).domain.com
     RewriteRule ^/(.*)$           http://localhost:%1/$1    [P]
</VirtualHost>
0

A RewriteRule would indeed do it.

<VirtualHost *:80>

Servername www.domain.com
ServerAlias *.domain.com

RewriteCond %{HTTP_HOST}      (\d+).domain.com
RewriteRule (.*)              http://localhost:%1/$1    [P]

</VirtualHost>
2
  • Thanks for You response, but i've got 403: Forbidden Error. Do You know what can i do ?
    – marioosh
    Oct 2, 2013 at 12:53
  • Have a look in the error log of your webserver. A 403 means that the server won't give you the content for some reason, and the reason is usually mentioned in the log. Oct 5, 2013 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .