Apache 2.2 and tomcat redirection for subdomains - apache

I have two tomcat servers running in my machine and i want to use apache to redirect the traffic to each server depending on the subdomain.
My httpd.conf is:
<VirtualHost *:80>
ServerName dev.bo.example.com
ProxyPass / ajp://localhost:11009/bo/
ProxyPassReverse / ajp://localhost:11009/bo/
ProxyPassReverseCookiePath /bo/ http://localhost:11001/bo/
</VirtualHost>
<VirtualHost *:80>
ServerName dev.com.example.com
ProxyPass / ajp://localhost:10009/com/
ProxyPassReverse / ajp://localhost:10009/com/
ProxyPassReverseCookiePath /com/ http://localhost:10001/com/
</VirtualHost>
The problem is that the traffic is always direct to the first tomcat server despite the subdomain i use.
It seems that only the first virtual host match despite if i use:
http://dev.com.com.example.com/ or
http://dev.bo.example.com/
Does any know what i am missing here?

Finally, i found that i missed configuration i had to add
NameVirtualHost *:80
to the httpd.conf

Related

Apache listen and redirect to a different port

I know this has been asked before, I used the highest rated question as a baseline, but I still can't get it to work.
I'm trying to host a website through an iocage jail in FreeBSD.
In my jail I'm hosting a domain called sub.domain.com. I've managed to get Apache to listen to a different port, 10080. So when I go to sub.domain.com:10080 I can access my website.
I would like to be able to go sub.domain.com and also access my website. As far as I understand, I need to redirect using mod_proxy to allow 80 -> 10080.
In httpd.conf I uncommented mod_proxy and added the following
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.sub.domain.com
ServerAlias sub.domain.com
ProxyPass / http://localhost:10080
ProxyPassReverse / http://localhost:10080
</VirtualHost>
This doesn't work for me, I still have to go to sub.domain.com:10080 to access my website.
Any tips?
EDIT: current config, also doesn't work
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName <jailip>
ServerAlias <jailip>
ProxyPass / http://<jailip>:10080
ProxyPassReverse / http://<jailip>:10080
</VirtualHost>

Apache Reverse proxy using hosts file

I have set up the following config for my apache server:
<VirtualHost *:80>
ServerName www.tld.com
ServerAlias *.tld.com
ProxyRequests On
ProxyPreserveHost On
RewriteEngine on
RewriteRule ^(.+) $1 [P]
ProxyPassReverse / $1
</VirtualHost>
<VirtualHost *:443>
ServerName www.tld.com
ServerAlias *.tld.com
ProxyRequests On
ProxyPreserveHost On
SSLEngine On
SSLCertificateFile /etc/ssl/certs/server.crt
SSLCertificateKeyFile /etc/ssl/private/server.key
ProxyPass / http://localhost/
ProxyPassReverse / http://localhost/
</VirtualHost>
And then I set up my host file to look like this:
127.0.0.1 tld.com
external_ip sub.tld.com
external_ip_2 sub.tld.com
external_ip and external_ip_2 are two servers that run apache and have vhosts as well. For the main domain I have a wildcard CSS and added a wildcard DNS record.
The proxying works perfect with HTTP and HTTPS as well, for all the servers that are in my /etc/hosts file. But it seems that when I try to resolve a subdomain that is not in my hosts file, apache get's stuck and in the logs I can see the following error:
[pid 4690] AH00161: server reached MaxRequestWorkers setting, consider raising the MaxRequestWorkers setting
I think that there is an infinite loop and I don't know why this is caused. I consider that the page from the proxy server should be displayed if the subdomain is not resolved by the hosts file.
Shouldn't the ServerName be like the domain name, and the alias the www ? Like:
ServerName tld.com
ServerAlias www.tld.com
Seems a bit redundant to me to put the ServerName as a subdomain, an then add an alias with a wildcard.

When default virtualhost is not available, no virtual hosts are available

I have an apache2 instance proxying requests to several tomcat instances. Sometimes the default virtualhost is unresponsive (tomcat is running but app not responding). When this happens none of the other virtual hosts are reachable through apache but they work going direct to tomcat. Should the default virtualhost be a static page to avoid this issue? Any other settings to tell apache to skip the default if it doesn't respond?
In the httpd.conf there are proxypass/proxypassreverse statements outside of the virutalhost blocks. I don't know if that could be confusing things.
ProxyPass /test/ ajp://localhost:9009/test/
ProxyPassReverse /test/ ajp://localhost:9009/test/
ProxyPass /test2/ ajp:/localhost/9010/test2/
ProxyPassRevers /test2/ ajp://localhost:9010/test2/
NameVirtualHost 192.168.0.1:80
VirtualHost 192.168.0.1:80>
ServerName test.domain.com
ProxyPreserveHost On
ProxyRequests Off
...
ProxyPass / ajp://localhost:9009/test/
ProxyPassReverse / ajp://localhost:9009/test/
...
/VirtualHost>
VirtualHost 192.168.0.1:80>
ServerName test2.domain.com
ServerAlias test2
PorxyPreseveHost On
ProxyRequests Off
...
ProxyPass / ajp://localhost:9010/test2/
ProxyPassReverse / ajp://localhost:9010/test2/
...
/VirtualHost>
Solaris 10
apache 2.2.6
mod_proxy_ajp
It would be suggested to put ProxyPass parameters in VirtualHost section only.

Redirect http to https by configuring .conf file of apache

I have configure apache to tomcat configuration by code like
<VirtualHost *:80>
ServerName captiveportal
ProxyPass / http://ip:port/path
ProxyPassReverse / http://ip:port/path
</VirtualHost>
Now i want to reirect this request to https
How can i achieve this ?
After looking your answer i have changes my configuration like
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile "/etc/httpd/conf/crt1.crt"
SSLCertificateKeyFile "/etc/httpd/conf/key1.key"
ProxyPass / http://ip:port/path
</VirtualHost>
<VirtualHost *:80>
ServerName captiveportal
Redirect / https://ip:port/path
</VirtualHost>
but when i type captiveportal on my browser it redirects me on url https://ip:port/path and it displays problem loading page
One more thing i don't want to display https://ip:port/path on browser.
Note :- https://ip:port/path where port is my tomcat port and ip is machine ip where tomcat run.
You could do something like this:
<VirtualHost *:80>
ServerName captiveportal
Redirect / https://my.host.name/
</VirtualHost>
...and then put your ProxyPass directives in side your SSL VirtualHost block instead.

Apache redirect to another port

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.example to JBoss localhost:8080/example. The DNS is currently setup for mydomain.example 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.example)?
<VirtualHost ip.addr.is.here>
ProxyPreserveHost On
ProxyRequests Off
ServerName mydomain.example
ProxyPass http://mydomain.example http://localhost:8080/example
ProxyPassReverse http://mydomain.example http://localhost:8080/example
</VirtualHost>
After implementing some suggestions:
Still not forwarding to port 8080
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName mydomain.example
ServerAlias www.mydomain.example
ProxyPass http://mydomain.example http://localhost:8080/example
ProxyPassReverse http://mydomain.example http://localhost:8080/example
</VirtualHost>
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
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.example
ServerAlias www.myhost.example
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
I also used:
a2enmod proxy_http
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.example
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
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.
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.
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
If you don't have to use a proxy to JBoss and mydomain.example:8080 can be "exposed" to the world, then I would do this.
<VirtualHost *:80>
ServerName mydomain.example
Redirect 301 / http://mydomain.example:8080/
</VirtualHost>
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
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"
You need 2 things:
Add a ServerAlias www.mydomain.example to your config
change your proxypass to ProxyPassMatch ^(.*)$ http://localhost:8080/example$1, to possibly keep mod_dir and trailing slashes from interfering.
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.
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.
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>
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>
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.example
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>