I'm running Apache HTTPD on port 80, and I have 2 instances of Apache Tomcat on port 8080 and 1010.
I deployed a war file on each tomcat:
project1.war on tomcat1
project2.war on tomcat2
The goal is being able to call project1 and project2 using just the domain, I want to avoid using the port number in the URL.
I figured out that mod_jk is the right tool to use for this purpose but I couldn't configure Apache properly to run it. I have two domains ready to use:
domain1.mysite.com
domain2.mysite.com
Any help is appreciated. Thank you
You'd be needing some apache httpd virtual host configuration like
Listen 80
<VirtualHost *:80>
ServerName domain1.mysite.com
ProxyPass "/project1" "ajp://backend.example.com:8009/project1"
ProxyPassReverse "/project1" "http://www.example.com/project1"
# Other directives here
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.mysite.com
ProxyPass "/project2" "ajp://backend.example.com:8009/project2"
ProxyPassReverse "/project2" "http://www.example.com/project2"
# Other directives here
</VirtualHost>
composed from Httpd VirtualHost Configuration and mod_proxy_ajp
Related
I'm running a apache2 reverse proxy that sends the request to a dedicated tomcat8 on localhost:8080
For proper self-reference within my applications, I need to forward the https headers accordingly. Therefor the proxy is configured as follows:
<VirtualHost *:443>
ServerName www.myapp.org
ProxyPass / http://127.0.0.1:8080/
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443
ProxyPreserveHost On
... (SSL directives omitted for readability)
</VirtualHost>
Now the spring advise is to set the following application.properties in case of running an embedded tomcat behind an proxy:
server.use-forward-headers=true
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-prot
Problem: as I'm running a dedicated tomcat, any server.* properties are ignored.
Question: how can I achieve the same configuration for the dedicated tomcat?
I am running multiple instance of tomcat on my linux machine. so there are more than one connector ports for different instance like 8080,8081,8082. I want to remove port number from URL.
For example :-
Current url : - www.sushant.com:8081/
Needed :-www.sushant.com/
please suggest me how can i do this.
Thanks.
You should consider using a proxy on your server. There is a really good tutorial at apache.org, using an Apache Web Server.
http://tomcat.apache.org/tomcat-7.0-doc/proxy-howto.html
This enables you to connect to your server via port 80, which is not printed in the url bar of your browser.
I saw answer above and struggled a bit, so thought of putting up an example since I was on ubuntu, so I had to change apache2.conf file in /etc/apache2/
You can find your apache2.conf file or httpd.conf as per your OS
I added following rules -
<VirtualHost *:80>
ServerName sushant.com
ServerAlias www.sushant.com
ProxyRequests On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:7777/
ProxyPassReverse http://localhost:7777/
</Location>
</VirtualHost>
<VirtualHost *:8081>
ServerName sushant.com
ServerAlias www.sushant.com
ProxyRequests on
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:8081/
ProxyPassReverse http://localhost:8081/
</Location>
</VirtualHost>
So, now it works both with and without the port.
This question has been asked before, and, in resume I want configure this scenario:
1 - I have one Jetty 7 server with many applications, e.g: app1, app2, app3, etc.
2 - I have one main domain, and, one sub-domain per Jetty application, e.g:
app1.example.com, app2.example.com, app3.example.com, etc..
3 - I'm try using Apache 2.2.22 mod_proxy to mask these Jetty applications across domains managed by Apache. This is my functional configuration for one application:
<VirtualHost *:80>
ServerName example.com
ServerAlias app1.example.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *:80>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /app1 http://localhost:8080/app1
</VirtualHost>
This configuration works, but, it doesn't remove the context name, the URI is:
http://app1.example.com/app1/?args=or/pages/etc...
Have an way to remove this context name, leaving a full transparent URIs ? e.g:
http://app1.example.com/?args=or/pages/etc..
All examples on the web uses this context name on URIS :/
Additional information:
VM with Ubuntu 12.04;
Jetty 7 without modifications;
Apache 2.2.22 with mod_proxy and mod_rewrite enabled (and some basics mods enabled by default);
Correct and valid CNAME and domain name within VM manager at Digital Ocean;
Thanks in advance.
I've found a solution:
1 - Install this specific lib for apache2 mod-proxy-html:
sudo apt-get install libapache2-lib-proxy-html
2 - Enable this new module:
sudo a2enmod proxy_html
3 - Config your application descriptor at:
sudo vim /etc/apache2/sites-enabled/app1
with this content:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName app1.example.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *:80>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/app1
ProxyPassReverse / http://localhost:8080/app1
ProxyHTMLURLMap / /app1/
<Location />
Order allow,deny
Allow from all
</Location>
</VirtualHost>
4 - Restart apache2 service:
sudo service apache2 restart
Be happy, transparent requests across:
http://app1.example.com/some/beautiful/page
I have a web application on tomcat http://localhost:8080/WebApp/
The I have configrued Apache 2 (mod_proy) so that the web application is directly accessible by localhost with out port and name: e.g http://localhost
<VirtualHost localhost:80>
ProxyPreserveHost On
ProxyPass / http://localhost:8080/WebApp/
ProxyPassReverse / http://localhost:8080/WebApp/
</VirtualHost>
The index.html is shown correctly on http://localhost.
But if a servlet redirects:
#WebServlet(description = "...", urlPatterns = { "/login" })
public class LoginServlet extends HttpServlet
{
#Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.sendRedirect("a.html");
}
}
and I use the URL http://localhost/login - I am redirected to http://localhost/WebApp/a.html
How do I get the correct redirect to http://localhost/a.html?
Thanks to Stuart and his link to this blog I found a solution:
Reverse Proxying Tomcat Web Applications Behind Apache
Solution: ProxyPreserveHost must be turned off!
Reason: If it is switched on, the response headers returned by the proxy backend will contain “localhost” or the real domain without the port number (or 80). So the ProxyPassReverse pattern does not match (because of the different port and if another domain name is used, also the domain name will not match).
Config:
<VirtualHost localhost:80>
ProxyPreserveHost Off
ProxyPass / http://localhost:8080/WebApp/
ProxyPassReverse / http://localhost:8080/WebApp/
</VirtualHost>
But this works only via http, not via ajp (I don’t know why).
If you still want to use ajp you could use the following workaround - Let Apache do another redirect after the wrong redirect:
<VirtualHost localhost:80>
ProxyPass /WebApp !
ProxyPass / ajp://localhost:8009/WebApp/
ProxyPassReverse / ajp://localhost:8009/WebApp/
RedirectMatch 301 ^/WebApp/(.*)$ /$1
RedirectMatch 301 ^/WebApp$ /
</VirtualHost>
The ProxyPass /WebApp ! directive is needed to exclude the path from further processing in mod_proxy (because proxy directives are evaluated before redirect directives)
Then the RedirectMatch directives redirect everything stating with /WebApp/... respectively /WebApp to the URL without /WebApp at the beginning.
The only drawback is that you must not have any sub folder named WebApp in your web application
I also had this problem and spent some time on it. I believe that if you change your apache httpd configuration to the following your redirect will work:
<VirtualHost localhost:80>
ProxyPreserveHost On
ProxyPass / http://localhost:8080/WebApp/
ProxyPassReverse / http://localhost/WebApp/
ProxyPassReverseCookiePath /WebApp /
</VirtualHost>
This is because the tomcat response headers will contain the proxy headers (i.e. the Location header is http://localhost/WebApp rather than http://localhost:8080/WebApp) because ProxyPreserveHost is switched On.
As a footnote: This also works with you want to change your webapps context. Say you wanted to change the publicly visible context to context you can use the following:
<VirtualHost localhost:80>
ProxyPreserveHost On
ProxyPass /context/ http://localhost:8080/WebApp/
ProxyPassReverse /context/ http://localhost/WebApp/
ProxyPassReverseCookiePath /WebApp /context
</VirtualHost>
For reference, I found this blog post extremely helpful: Reverse Proxying Tomcat Web Applications Behind Apache
you have use to AJP Connector to connect apache2 & tomcat , it will be the perfect solutions for this.
if you need how to configure this, tell me i will explain this detail
Use forwarding instead of redirection
I think your problem is the use of sendRedirect. Calling sendRedirect is actually suppose to show the browser that the URL has been redirected. If you want to hide that you need to use forwarding.In your servlet try this instead of sendRedirect.
String servletPath = request.getServletPath();
if(servletPath.equals("/app1")){
ServletContext ctx = request.getServletContext().getContext("/app1");
RequestDispatcher dispatcher=ctx.getServletContext().getRequestDispatcher( "/app1/app1.html" ); // or wherever you actually keep app1.html
dispatcher.forward( request, response );
}
Inside your context.xml set crossContext = "true" so you can forward requests to other web applications.
<Context crossContext="true" ....../>
I had the same problem while tried to redirect the apache2(running on port 80) request to tomcat(application server running on port 8080).
This is the configuration which is working perfectly.
Go to /etc/apache2/sites-available/000-default.conf and add the following config:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
# for redirecting the websocket requests
ProxyPass /ws ws://localhost:7681/
#ProxyPass /ws ws://localhost:7681/
ProxyPassReverse /ws ws://localhost:7681/
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
# for redirecting the http request
ProxyPass /applicationContextUrl ' http://localhost:8080/applicationContextUrl
ProxyPassReverse /applicationContextUrl http://localhost:8080/applicationContextUrl
ProxyPassReverseCookiePath /applicationContextUrl /
ProxyPassReverseCookieDomain localhost applicationContextUrl
ProxyRequests off
ProxyTimeout 15
ErrorLog ${APACHE_LOG_DIR}/nirad_error.log
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/nirad_access.log combined
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
#Require all denied
Require all granted
Require local
</Proxy>
</VirtualHost>
Done.
Now goto terminal and hit the following command.
sudo a2enmod proxy_http (for http redirection).
sudo a2enmod proxy_wstunnel (for websocket redirection)
and sudo service apache2 restart
run your application server on port 8080
I have been trying to configure my apache server to support virtual hosts, these hosts, would then redirect any request made at port 80 to different applications hosted in a Jboss AS, so for example my configuration would be like this:
<VirtualHost *:80>
ServerName www.testdomain.com
ProxyPass / http://localhost:8080/contextPath
ProxyPassReverse / http://localhost:8080/contextPath
ProxyPreserveHost On
ProxyPassReverseCookiePath / /
</VirtualHost>
However the problem is that when I'm trying to access http://www.testdomain.com, the url gets redirected effectively to localhost:8080, however, I got a duplicated context path. I.E: http://www.testdomain.com/contextPath/contextPath.
Any ideas why is this happening. Thanks a lot.
I had the same issue and this was solved by adding forwardslashes to the urls.
ProxyPass / http://localhost:8080/contextPath/
ProxyPassReverse / http://localhost:8080/contextPath/
That solved it for me !
Full example for a single virtual host file. I have several, one for each domain and subdomain.
ProxyRequests Off
ProxyPreserveHost On
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName enter.name.here
ProxyPass / http://127.0.0.1:8080/<contextPath>/
ProxyPassReverse / http://127.0.0.1:8080/<contextPath>/
ErrorLog /var/log/apache2/somelog.log
CustomLog /var/log/apache2/somecustom.log common
</VirtualHost>
You need to remove the "ProxyPass" and "ProxyPassReverse" entries, unless you really are trying to proxy something. If the jboss AS is on another server, then you need to retain the proxy entries, but it looks to me like you might be making it too difficult if the content is on one machine and not multiples.
If you want one server to use different base folders as the root for 2 different domains, then you would need to configure the 2 domains by specifying the DocumentRoot parameter.
for example, if I wanted to host google.com and yahoo.com on one computer, my virtualhost entries would contain:
<VirtualHost *:80>
ServerName www.google.com
DocumentRoot /var/www/Google
</VirtualHost>
<VirtualHost *:80>
ServerName www.yahoo.com
DocumentRoot /var/www/Yahoo
</VirtualHost>
Then, your root directories for each server will go in the google folder and the yahoo folder, respectively.
If you are trying to proxy a completely different machine, then the following should work:
<VirtualHost *:80>
ServerName www.google.com
ProxyPass / www.google.com
</VirtualHost>
<VirtualHost *:80>
ServerName www.yahoo.com
ProxyPass / www.yahoo.com
</VirtualHost>
Your entry specifically doesn't make much sense. I think it should look like:
<VirtualHost *:80>
ServerName www.testdomain.com
ProxyPass /contextPath http://localhost:8080
ProxyPassReverse /contextPath http://localhost:8080
</VirtualHost>