Easy configuration for domain:8080 to 80 Tomcat - apache

Is there an easy way to change from domain.com:8080/myapp to domain.com? Any step by step working examples for Tomcat 7?

Yes, you can use Apache Mod_Proxy for this. You use Apache2 as a front-end to your Tomcat instance.
Here is a configuration example:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName site.domain.com
Redirect / /tomcat-context/
ProxyPass /tomcat-context/ http://127.0.0.1:tomcat-port/tomcat-context/
ProxyPassReverse /tomcat-context/ http://site.domain.com/tomcat-context/
ProxyPreserveHost On
</VirtualHost>
You can find more infos here.
It may seem a bit hard to grasp at first if you're not an Apache2 expert (I am not), but once you've configured your first frontend, adding more and more tomcat instances behind it is a breeze.
Or if you're lazy, change the connector port from 8080 to 80 in the conf/server.xml file in your tomcat home directory.

Related

Apache and Tomcat proxying

Recently, I was in need of using both Apache and Tomcat together in which Apache was to be used as the reverse proxy to forward requests to port 80 to localhost:8080 which I did like this:
<VirtualHost *:*>
ProxyPass / http://localhost:8080/app/
</VirtualHost>
And it works perfectly well.
Now, what I need to do is: I have Tomcat listening and serving on another port 8082. I need to be able to access it using www.mydomain.com:8082. I tried:
<VirtualHost *:8082>
ProxyPass / http://localhost:8082/app/
</VirtualHost>
But no luck. And I can't listen on 8082 because Tomcat is doing that.
What you have above is a (failed) attempt to map the / URL space into two different places. That's never going to work.
When proxying to Tomcat, it's never a good idea to rewrite URL paths (e.g. / -> /app/ because Tomcat is going to get all kinds of confused. It's much better to map individual applications:
<VirtualHost *:*>
ProxyPass /app1/ http://localhost:8080/app1/
ProxyPass /app2/ http://localhost:8080/app2/
ProxyPass /app3/ http://localhost:8082/app3/
ProxyPass /app4/ http://localhost:8082/app4/
# If you need a fall-back application for `/`, just map it last.
ProxyPass / http://localhost:8080/
</VirtualHost>
Note that the last line up there is mapping / to Tomcat's ROOT context (mounted on /'). Don't do this any other way, or you'll spend years trying to make everything work when you could have just done it the recommended way.

Tomcat Apache port 80 port 8080

I have a vps that runs tomcat 8.0.23 and apache httpd server.
in tomcat i have 3 projects lets call them by names below:
/firstpro
/secondpro
/thirdpro
and i have a domain name lets call it www.mydomain.com
now what i wanna do if request comes to www.mydomain.com i want to forward it to
my.vps.ip.address : 8080/firstpro
the code below is my virtualhost configuration file:
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAlias mydomain.com
ProxyPreserveHost On
ProxyPass /firstpro http://localhost:8080/firstpro
ProxyPassReverse /firstpro http://localhost:8080/firstpro
</VirtualHost>
so, problem comes when i type in browser:
if i go to this url: www.mydomain.com i dont get any cookies that automatically my server generates for that session
but if i go to url: www/mydomain.com:8080/firstpro everything works fine
thanks for reading all this. and i would be glad if someone could help me to figure it out
You may need a ProxyPassReverseCookieDomain since you're forwarding between your domain and localhost. – Joachim Isaksson 3 hours ago

Best approach to set up PHP and Java application on same host

I have two web applications one in PHP and one in Java (Play framework).
I want to make both these applications available to my clients and I have only one server for test environment.
What would be the best and easy to maintain approach for my problem?
I am already looking at options of virtual hosts on Apache server. But is the best? Are there any third party tools which can help me to divert traffic to PHP and Java apps based on the port in the http request?
Port nos for PHP app is 80 and Java app is 9000.
Regards,
Suraj
assuming both ports are forwarded correctly and Apache is only listening for traffic on port 80 and java is only listening on 9000 then going to YourIp:80 should take you to apache and YourIP:9000 should take you to the java app
For PHP app create common vhost as usally, and for Play app create reverse-proxy vhost (with other domain and/or subdomain for this), take a look at samples in docs
LoadModule proxy_module modules/mod_proxy.so
...
<VirtualHost *:80>
DocumentRoot "/path/to/your/php/app/root/folder/"
ServerName your-php-app.com
ErrorLog "/path/to/apache/logs/folder/your-php-app.com-error_log"
CustomLog "/path/to/apache/logs/folder/your-php-app.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ServerName your-play-app.com
ProxyPass /excluded !
ProxyPass / http://127.0.0.1:9000/
ProxyPassReverse / http://127.0.0.1:9000/
</VirtualHost>
Thanks for your comment.
Yeah, I did the same thing. I am simply pointing my domain name to Apache Http server. So its using default port 80 and does not show up in urls.
Also, I am using Apache as front server for all my requests to port 9000. So, I am rerouting my requests for Play application at port 9000 through Apache port 80. It needed a change in url patterns so that play and apache specific urls can be distinguished.
Changed urls and used proxy_http_module + ProxyPassMatch directive to get the rerouting working.
Suraj

How can I use apache or nginx as frontend for a node app?

I'm trying to develop a Node.js web application, but my production environment-to-be is already hosting Apache/2.2.22. So I can't have Node use port 80, and I don't want my users to have to go to http://myapp.com:4000/.
Is there an apache module that does this, perhaps like mod_jk does this for Tomcat?
The same question goes for nginx.
mod_proxy can do that (for apache)
<VirtualHost nodejs.host.com>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:4000
ProxyPassReverse / http://localhost:4000
</VirtualHost>
will forward everything on that virtualhost to Node.js

Apache - Tomcat ProxyPass VirtualHost - Context Path

I have a problem configuring apache tomcat ProxyPass directive for two applications that have two different Context Paths in tomcat. The tomcat is running behind an apache and I use the apache to proxy path the requests to tomcat. In apache I want to access both application via a hostname instead of a context path.
Scenario:
tomcat
https://domain:8443/app1
https://domain:8443/app2
in tomcat the applications have the context path app1 and app2
in apache I want to enable both application as follow:
https://app1.host/
https://app2.host/
In apache I have created a configuration for each domain:
ProxyPass / https://localhost:8443/app1
ProxyPassReverse / https://localhost:/8443/app1
The strange thing is app1 is only available through apache using the context path:
https://app1.host/app1
Is it possible to realize such a setup with apache ProxyPass module?
Thx for your help.
You should be able to achieve the result you want by using virtual hosting. Also it's a good idea to pass the requests to tomcat via the AJP protocol instead of HTTPS. Try adding this to the Apache configuration
NameVirtualHost *:443
<VirtualHost *:443>
ServerName app1.host
ProxyPass / ajp://localhost:8009/app1/
</VirtualHost>
<VirtualHost *:443>
ServerName app2.host
ProxyPass / ajp://localhost:8009/app2/
</VirtualHost>
If you haven't changed the default server settings for Tomcat this should work just as it is. Otherwise make sure to specify the AJP port that is configured in Tomcat's conf/server.xml file. There should be a line similar to this:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Make sure that you have the mod_proxy and mod_proxy_ajp modules loaded in Apache configuration, this may vary depending on your Apache installation. Also remove any previously configured 'ProxyPass / ...' lines as they will interfere with the new configuration. Hope this works for you.
you can try
ProxyPass / https://localhost:8443/app1/
ProxyPassReverse / https://localhost:8443/app1/
with the final /