Apache redirect to another port

ApacheRedirect

Apache Problem Overview


I've struggled with this for some time and am definitely doing something wrong.

I have apache server and a JBoss server on the same machine. I'd like to redirect traffic for mydomain.com to JBoss localhost:8080/example. The DNS is currently setup for mydomain.com and it will go straight to port 80 when entered into the browser.

My question is how do I redirect to a different port when a certain domain name comes to apache (in this case, "mydomain.com")?

<VirtualHost ip.addr.is.here> 
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName mydomain.com
  ProxyPass http://mydomain.com http://localhost:8080/example
  ProxyPassReverse http://mydomain.com http://localhost:8080/example
</VirtualHost> 

UPDATED w/ Suggestions

Apache Solutions


Solution 1 - Apache

You should leave out the domain http://example.com in ProxyPass and ProxyPassReverse and leave it as /. Additionally, you need to leave the / at the end of example/ to where it is redirecting. Also, I had some trouble with http://example.com vs. http://www.example.com - only the www worked until I made the ServerName www.example.com, and the ServerAlias example.com. Give the following a go.

<VirtualHost *:80> 
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName www.example.com
  ServerAlias example.com
  ProxyPass / http://localhost:8080/example/
  ProxyPassReverse / http://localhost:8080/example/
</VirtualHost> 

After you make these changes, add the needed modules and restart apache

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart

Solution 2 - Apache

I solved this issue with the following code:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName myhost.com
ServerAlias ww.myhost.com
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>

I also used:

a2enmod proxy_http

Solution 3 - Apache

I wanted to do exactly this so I could access Jenkins from the root domain.

I found I had to disable the default site to get this to work. Here's exactly what I did.

$ sudo vi /etc/apache2/sites-available/jenkins

And insert this into file:

<VirtualHost *:80>
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName mydomain.com
  ServerAlias mydomain
  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
  <Proxy *>
        Order deny,allow
        Allow from all
  </Proxy>
</VirtualHost>

Next you need to enable/disable the appropriate sites:

$ sudo a2ensite jenkins
$ sudo a2dissite default
$ sudo service apache2 reload

Hope it helps someone.

Solution 4 - Apache

Found this out by trial and error. If your configuration specifies a ServerName, then your VirtualHost directive will need to do the same. In the following example, awesome.example.com and amazing.example.com would both be forwarded to some local service running on port 4567.

ServerName example.com:80

<VirtualHost example.com:80>
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName awesome.example.com
  ServerAlias amazing.example.com
  ProxyPass / http://localhost:4567/
  ProxyPassReverse / http://localhost:4567/
</VirtualHost>

I know this doesn't exactly answer the question, but I'm putting it here because this is the top search result for Apache port forwarding. So I figure it'll help somebody someday.

Solution 5 - Apache

You have to make sure that the proxy is enabled on the server. You can do so by using the following commands:

  a2enmod proxy
  a2enmod proxy_http

  service apache2 restart

Solution 6 - Apache

This might be an old question, but here's what I did:

In a .conf file loaded by apache:

<VirtualHost *:80>
  ServerName something.com
  ProxyPass / http://localhost:8080/
</VirtualHost>

Explanation: Listen on all requests to the local machine's port 80. If I requested "http://something.com/somethingorother", forward that request to "http://localhost:8080/somethingorother". This should work for an external visitor because, according to the docs, it maps the remote request to the local server's space.

I'm running Apache 2.4.6-2ubuntu2.2, so I'm not sure how the "-2ubuntu2.2" affects the wider applicability of this answer.

After you make these changes, add the needed modules and restart apache

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart

Solution 7 - Apache

If you don't have to use a proxy to JBoss and mydomain.com:8080 can be "exposed" to the world, then I would do this.

<VirtualHost *:80>
  ServerName mydomain.com
  Redirect 301 / http://mydomain.com:8080/
</VirtualHost>

Solution 8 - Apache

Just use a Reverse Proxy in your apache configuration (directly):

ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar

Look here for apache documentation of how to use the mod

Solution 9 - Apache

My apache listens to 2 different ports,

Listen 8080
Listen 80  

I use the 80 when i want a transparent URL and do not put the port after the URL useful for google services that wont allow local url?

But i use the 8080 for internal developing where i use the port as a reference for a "dev environment"

Solution 10 - Apache

Apache supports name based and IP based virtual hosts. It looks like you are using both, which is probably not what you need.

I think you're actually trying to set up name-based virtual hosting, and for that you don't need to specify the IP address.

Try < VirtualHost *:80> to bind to all IP addresses, unless you really want ip based virtual hosting. This may be the case if the server has several IP addresses, and you want to serve different sites on different addresses. The most common setup is (I would guess) name based virtual hosts.

Solution 11 - Apache

You need 2 things:

  1. Add a ServerAlias www.mydomain.com to your config
  2. change your proxypass to ProxyPassMatch ^(.*)$ http://localhost:8080/example$1, to possibly keep mod_dir and trailing slashes from interferring.

Solution 12 - Apache

All are excellent insights to accessing ports via domain names on virtual servers. Do not forget, however, to enable virtual servers; this may be commented out:

NameVirtualHost *:80
<Directory "/home/dawba/www/">
 allow from all
</Directory>

We run WSGI with an Apache server at the domain sxxxx.com and a golang server running on port 6800. Some firewalls seem to block domain names with ports. This was our solution:

<VirtualHost *:80>
 ProxyPreserveHost On
 ProxyRequests Off
 ServerName wsgi.sxxxx.com
 DocumentRoot "/home/dxxxx/www"
  <Directory "/home/dxxx/www">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
 ScriptAlias /py/ "/home/dxxxx/www/py/"
 WSGIScriptAlias /wsgiprog /home/dxxxx/www/wsgiprog/Form/Start.wsgi
</VirtualHost>

<VirtualHost *:80>
 ProxyPreserveHost On
 ProxyRequests Off
 ServerName sxxxx.com 
 ServerAlias www.sxxxx.com
 ProxyPass / http://localhost:6800/
 ProxyPassReverse / http://localhost:6800/
</VirtualHost>

Solution 13 - Apache

This is working in ISPConfig too. In website list get inside a domain, click to Options tab, add these lines: ;

ProxyPass / http://localhost:8181/
ProxyPassReverse / http://localhost:8181/

Then go to website and wolaa :) This is working HTTPS protocol too.

Solution 14 - Apache

Try this one-

<VirtualHost *:80> 
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName www.adminbackend.example.com
  ServerAlias adminbackend.example.com
  ProxyPass / http://localhost:6000/
  ProxyPassReverse / http://localhost:6000/
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> 

Solution 15 - Apache

This is how I redirected part of the requests to one url and rest to another url:

<VirtualHost *:80>
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName localhost
  ProxyPass /context/static/content http://localhost:80/web/
  ProxyPassReverse /context/static/content http://localhost:80/web/
  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
</VirtualHost>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionagentcurryView Question on Stackoverflow
Solution 1 - Apache2AMTechView Answer on Stackoverflow
Solution 2 - ApacheRenan VizzaView Answer on Stackoverflow
Solution 3 - ApacheLouthView Answer on Stackoverflow
Solution 4 - ApacheEthan B MartinView Answer on Stackoverflow
Solution 5 - ApacheAswin MohananView Answer on Stackoverflow
Solution 6 - ApacheTeeeeeeeeeeeeeeeeeeeeeeeeeeeejView Answer on Stackoverflow
Solution 7 - ApacheMichael RiceView Answer on Stackoverflow
Solution 8 - ApacheJulian DehneView Answer on Stackoverflow
Solution 9 - ApacheMiguelView Answer on Stackoverflow
Solution 10 - ApacheMartin VilcansView Answer on Stackoverflow
Solution 11 - ApacheJon LinView Answer on Stackoverflow
Solution 12 - Apacheuser2099484View Answer on Stackoverflow
Solution 13 - ApachekodmanyaghaView Answer on Stackoverflow
Solution 14 - ApacheAnkit Kumar RajpootView Answer on Stackoverflow
Solution 15 - ApacheRceeView Answer on Stackoverflow