Rewriting URL within tomcat html output - java

I am trying to configure an external Apache front-end with a different URL to the backend tomcat app. I take tomcat's manager app as an example.
I would like users to access my page like externally like:
https://myhost.com/tomcat-manager
Internally, it gets redirected to
http://localhost:8080/manager
I tried the following configuration in apache2 with mod_proxy and mod_rewrite:
RewriteEngine on
SSLProxyEngine on
RewriteRule "/manager/(.*)$" https://myhost.com/tomcat-manager/$1 [P]
ProxyPass /tomcat-manager http://localhost:8080/manager
ProxyPassReverse /tomcat-manager http://localhost:8080/manager
It mostly works, but I dislike that the tomcat manager webapp outputs its links as "/manager" instead of "/tomcat-manager", forcing me to add the above mod_rewrite rule. Ideally, I would like the end-user to see only browser URLs of the pattern:
https://myhost.com/tomcat-manager/(whatever)
and never the following:
https://myhost.com/manager/(whatever)
Any suggestion to configure Apache without modifying the underlying webapp? Thanks!

I found the answer to my question. It is Apache's mod_proxy_html. I enabled the corresponding modules and fixed Ubuntu's mod_proxy_html missing configuration as stated here: https://serverfault.com/questions/684905/proxyhtmlurlmap-not-working-in-apache2-4
Then, I added the following configuration code:
ProxyHTMLEnable On
ProxyHTMLURLMap http://localhost:8080/manager/ /tomcat-manager/
ProxyHTMLURLMap /manager/ /tomcat-manager/
SetOutputFilter proxy-html
RequestHeader unset Accept-Encoding
And that's it. Now, I can access http://myhost.com/tomcat-manager and the html code within is also rewritten!

Related

App name changed so redirect to new one in tomcat

We have a java web application running on tomcat with name, for example, abc/index.jsp
It is now changed to xyz/index.jsp
How can I redirect to the new url (xyz/index.jsp) when someone requesting with (abc/index.jsp)?
I want to set something within the tomcat (server.xml, web.xml, etc.) to redirect to the new url.
(I don't want to write code within the JSP to do that)
Could anybody please help
Thanks
If you have Apache in the front, look at mod_proxy. Following should do.
ProxyPass /abc /xyz
Remember to enable the mod_proxy

How to remove Java application name from url on Weblogic

I have a Java application deployed on Oracle Weblogic server which can be accessed via http://www.example.com/myapp
This works fine but now I want to get rid of "myapp" in the URL so the application can be accessed only via http://www.example.com
Is this possible? If yes, how?
I don't know about Weblogic but in JBoss and Tomcat you'd name your application ROOT.war.
IMHO this is not desirable so I'd employ some URL rewriting, e.g. by using an intermediate apache webserver, which then forwards /myapp(/.*) to $1.
Of course you'd have to remove the context root from any internally generated links, but how that is done would depend on your application and environment.
I deploy a Reverse Proxy, you can use Oracle HTTP Server or Apache for this, I use mod_wl_ohs in the OHS and it's pretty simple:
<VirtualHost *:80>
ServerName myexample.com
<IfModule weblogic_module>
DynamicServerList On
WebLogicHost <IP of the weblogic>
WebLogicPort <PORT>
<Location />
RedirectMatch ^/$ /store
SetHandler weblogic-handler
WebLogicHost <IP of the weblogic>
WebLogicPort <PORT>
</Location>
</IfModule>
</VirtualHost>
Plus, it'll give you another security layer if you setup and URL-Firewall using REWRITE mod.
<context-root> in application.xml needs to be set to / like this:
<web>
<web-uri>web.war</web-uri>
<context-root>/</context-root>
</web>
Further reading:
Weblogic Docs on context-root
Similar question on SO

How to enable CORS in apache tomcat [duplicate]

This question already has answers here:
Set CORS header in Tomcat
(4 answers)
Closed 7 years ago.
I am trying to consume some web services which are cross domain. When I disable chrome's web-security it is working fine. I want it to work without this so I have tried adding cross-domain.xml and still it didnt work. When i searched more, came to know about CORS enabling in tomcat.
from http://www.w3.org/wiki/CORS_Enabled
For Apache
Apache can be configured to expose this header using mod_headers. This is enabled by default in Apache, however you may want to ensure it's enabled in your deployment by running the following command:
a2enmod headers
To expose the header, you can add the following line inside , , and sections, or within an .htaccess file.
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
Can anyone please let me know where to add these configurations in TOMCAT and in which files exactly. I am using tomcat from eclipse.
Appreciate any help.
CORS support in Tomcat is provided via a filter. You need to add this filter to your web.xml file and configure it to match your requirements. Full details on the configuration options available can be found in the Tomcat Documentation.
Check this answer: Set CORS header in Tomcat
Note that you need Tomcat 7.0.41 or higher.
To know where the current instance of Tomcat is located try this:
System.out.println(System.getProperty("catalina.base"));
You'll see the path in the console view.
Then look for /conf/web.xml on that folder, open it and add the lines of the above link.
Just to add a bit of extra info over the right solution. Be aware that you'll need this class org.apache.catalina.filters.CorsFilter. So in order to have it, if your tomcat is not 7.0.41 or higher, download 'tomcat-catalina.7.0.41.jar' or higher ( you can do it from http://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina ) and put it in the 'lib' folder inside Tomcat installation folders.
I actually used 7.0.42
Hope it helps!

how to set java web application's context root when working with reverse proxy

My old way using mod_jk in apache and configure virtual host in tomcat
In the JSP file, I refer to CSS as below
/<%=request.getContextPath()%>/css/styles.css
while the home link is set to
/<%=request.getContextPath()%>/
so this worked fine when I use mod_jk in apache to work with tomcat using ajp;
When I try to configure reverse proxy as below
ProxyPass / http://localhost:800/mywebapp
ProxyPassReverse / http://localhost:800/mywebapp
the home page can be retrieved fine but the css request becomes
http://mydomain.com/mywebapp/mywebapp/css/style.css
so the css file can not be retrieved correctly;
I think one possible way is to always use relative path like ./style.css or ../style.css
a. since header/footer are shared, and the home page is in a different level with detail page, it's inconvenient to use relative path because they're at a different level
b. still, I think the home link will have to be /<%=request.getContextPath()%>/
so I wonder what's the way to set contextroot fine in java web and also work fine with reverse proxy?
thx a lot
As I know your application server (Tomcat) isn't able to be aware of a reverse proxy presence. Generally speaking, it can be contacted through any number of reverse proxies or directly by browsers. A network configuration is usually used to limit this, not HTTP or Java.
So, you must accurately rely on relative URLs to make your application work well.
When I have to deal with reverse proxy presence (almost always due to SSO architectures), I embed a "junction" configuration string item (the portion of the URL used in the proxy to map the application) and use it in the only places where I need to build an absolute URL.

Apache vs Tomcat configuration question

I was searching in Google and found that Apache can be configured via mod_access directives in the httpd.conf file to block a web site from a particular IP.
Is there anything equivalent in Tomcat?
I am not sure I understand what are the corresponding configuration files.
Thanks
Try the Remote Address Filter. http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html
See the Request filters section of this doc. Doing this via tomcat configuration is pretty static, you need to restart to edit configuration. If you need something dynamic, it's probably best to implement a custom servlet filter.

Categories