27

I have a non-Apache server listening to port 8001 and Apache listening port 80. I want a certain virtual domain to actually be served by the non-Apache server over port 80.

Example:

<VirtualHost *:80>
  Servername example.com

  # Forward this on to the server on port 8001
</VirtualHost>

I thought I could do this with mod_proxy and ProxyPass with something like this.

ProxyPass * http://www.example.com:8001/

But that doesn't work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dave
  • 1,658
  • 3
  • 17
  • 19

3 Answers3

39

ProxyPass * http://www.example.com:8001/

star is only valid in a block. Forward slash is what you want.

ProxyPass / http://www.example.com:8001/
ProxyPassReverse / http://www.example.com:8001/

The reverse proxy ensures that redirects sent by your port 8001 server are adjusted to the canonical name name of your proxy.

The apache manual has some examples. http://httpd.apache.org/docs/2.0/mod/mod_proxy.html

Cœur
  • 37,241
  • 25
  • 195
  • 267
txyoji
  • 6,680
  • 1
  • 44
  • 46
  • 3
    If you get an internal server error: `sudo a2enmod proxy` `sudo a2enmod proxy_http` `sudo service apache2 restart` – jaggedsoft Sep 10 '16 at 22:11
  • we shouldn't use mysite.com on stackoverflow, see http://meta.stackexchange.com/questions/208963/why-are-certain-example-urls-like-http-site-com-and-http-mysite-com-blocke – Cœur Dec 31 '16 at 07:51
8

I have a site hosted by apache on port 80. I also have a python web server listening on port 8880, which needed accessed via http://[mydomainname]/something. Using txyoji's answer, I got it working by simply adding a proxy pass to my virtual host definition like so:

ProxyPass /something http://mydomainname:8880/something
ProxyPassReverse /something http://mydomainname:8880/something

UPDATE

Depending your setup, an even better way to do this is to setup a proxy pass for a port on "localhost". I think it's a bit more clear what you're doing, plus more portable. Along with this, you don't have to even open up the firewall to that port! You can proxy pass locally to any port, so there is no reason to expose that to the outside world if you don't have to. Funnel everything through port 80 and have Apache always "run out in front". Then, you can just worry about the security of that.

ProxyPass /something http://localhost:8880/something
ProxyPassReverse /something http://localhost:8880/something
BuvinJ
  • 10,221
  • 5
  • 83
  • 96
1

and full code would be like below.

<IfModule mod_ssl.c>
<VirtualHost *:443>
ProxyPreserveHost On
ServerAdmin webmaster@localhost
ServerName test.example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPass / http://localhost:9301/
ProxyPassReverse / http://localhost:9301/

SSLCertificateFile /etc/letsencrypt/live/test.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/test.example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

this link also helpful.

ashen madusanka
  • 647
  • 1
  • 9
  • 15
  • Information links are great, but providing the details from the site in your answer is more accessible. – haydnD Mar 12 '21 at 17:06