Java web application hosting - java

I hosted my application into Tomcat server and configured server.xml so that i can access my web application by directly accessing the website name eg:www.example.com but it works only when it is followed by my port number for eg: www.example.com:8080, I am using godaddy for VPS.
This is the configuration i have used
<Host name="example.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Alias>www.example.com</Alias>
<Context path="" docBase="Prototype" debug="0" privileged="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/></host>

but it works only when it is followed by my port number for eg: www.example.com:8080
By 'works' , do you mean Tomcat starts with any port you mention in config but site can be accessed via 8080 only?
if not sure, refer to <tomcat_dir>/logs/catalina.out file to view startup logs,
it so may happen that your VPS has any other application already running on port 80 , httpd typically or NGINX in some cases, you need to stop the app and start tomcat with port 80 in config.
how to check app running on specific port
netstat -apn|grep <port_number>
or
ss -apn|grep <port_number>
in case no app are running on port 80 or any other port you've mentioned in config and tomcat boots successfully, still you are not able to access your app via outside then most likely its network access issue,
for that you need to allow INGRESS access to your desired port in local IP Tables or Godaddy VPS config panel whichever the case may be.
the issue you have mentioned looks more like a linux admin issue.
please share more details i will guide you to exact solution
All the best.

You could provide som more info about your VPS. Here I will assume you have a full VPS with root access and you can control the all network access. Apache Tomcat runs by default on port 8080. Effectively you have two options
Run the Tomcat on the port 80 as root ( I don't recommend this approach)
As #Kayaman mentiomed, install an http proxy (apache httpd, nginx,..) which would listen on port 80 and forward all requests to 8080 (search for reverse proxy)
Have fun

Related

IIS rewrite rule with Tomcat 9.0

I have deployed a Spring Boot application in Tomcat in Windows Server and it's accessible on browser via http://server-name:8080/app. Now, for end users I don't want them to hit the app at port 8080, so I have configured an IIS rewrite rule
<rule name="app" enabled="true" stopProcessing="false">
<match url="^app.*" />
<action type="Rewrite" url="http://server-name:8080/{R:0}" logRewriteUrl="true" />
</rule>
Now, when I hit http://server-name/app, it works, but the subsequent calls are going to http://server-name:8080/app.
I am not sure if there is a problem with the IIS rule since I ran the app.war using same application.properties as
java -jar app.war --server.port=8080 --spring.config.import=application.properties
The only caveat here is I am passing context path in properties as
server.servlet.context-path=/app
and in this scenario all the URL calls go without port 8080. If anyone have any insight or solution, please share.
Please NOTE: I can't host Tomcat on port 80 as there are other apps running on port 80 which uses the API from app deployed in Tomcat.

How to do a redirect behind Nginx

My Tomcat server sits behind an Nginx reverse proxy.
I configured RemoteIpValve in Tomcat to replace hostname, port and scheme. Tomcat works on localhost:8080.
Tomcat valve (server.xml)
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="x-forwarded-for"
protocolHeader="x-forwarded-proto"
proxiesHeader="x-forwarded-by" />
When i redirect from https://example.com/pages/MyForm.jsp to https://example.com/pages/MyForm.jsp?message=Something, it works correctly,
but when i try to redirect from https://example.com/pages/MyForm.jsp to https://example.com/pages/AnotherPage.jsp?message=Something the
site can't be reached.
Unlike the first example request, the second one displays port 8080 in the location of Response Headers.
location: https://example.com:8080/pages/AnotherPage.jsp?message=Something`
Request Method: GET
Is there any configuration that should be done in Nginx or Tomcat? How can i solve this problem?

Assign domain to IP address

How do i go about making my Java app run an HTTP server on some socket (e.g 172.16.1.10:8080) and make it so that when another computer on the network connects to a domain (e.g http://myjavadomain.com) it gets redirected to the socket?
If you are using Apache Tomcat then the below configuration will helpful to you.
Fot Apache Tomcat you have to make on Host entry in the configuration location of the TOMCAT_HOME location.
Follow below steps that will be helpful to you
1) Find the server.xml file in the conf location of TOMCAT_HOME
2) in the server.xml file make the below host entry
<Host name="www.xyz.com" debug="0" appBase="webapps/mynewhost" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="mynewhost_log." suffix=".txt" timestamp="true"/>
</Host>
In appBase property place the location of your web app
3) Now in browser open the above url.
If you want to run a fully fledged HTTP server then you will probably want to use some external library. For instance, Tomcat is written in Java, but there is also SUN's httpserver package. If it's just a simple socket server you're after, you can use the built-in classes from the java.net package:
ServerSocket server = new ServerSocket(8080);
while (running) {
Socket socket = server.accept();
handleConnection(socket);
}
This will listen for incoming socket connections on port 8080 and create a new Socket whenever a client connects. You can then communicate with the client through the Socket's InputStream and OuputStream, which you would probably do in a separate Thread, so that your ServerSocket can continue listening for incoming connections from other clients.
As for the second part of your question: by default, a web browser will connect to port 80, and there are several ways you could do port forwarding. One possible solution using iptables is given on this website:
iptables -t nat -I PREROUTING --src 0/0 --dst 172.16.1.10 -p tcp --dport 80 -j REDIRECT --to-ports 8080
But the easiest solution would be to just specify the port number directly when connecting to your machine, e.g.
http://myjavadomain.com:8080
This is assuming that your DNS is configured so that it resolves myjavadomain.com to 172.16.1.10 already.

Tomcat 5.5 virtual host with appbase

I'm trying to create a virtualhost in Tomcat 5.5. Up till now I've created the Host entry
<Host name="www.nikoslianeris.gr" appBase="test"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="test" prefix="mydoamin_access_log." suffix=".txt"
pattern="combined" resolveHosts="false"/>
</Host>
and everything is ok with that. The problem is that when I put my app in the appbase directory nothing happens. I mean, I can see from the admin console the new virtual host bat when I type the url nothing happens. I don't know what the problem might be! I found many tutorials on the subject and did what they said but nothing happens!
just brainstorming, but have you mapped www.nikoslianeris.gr to your local machine in your hosts file or a local DNS? ie any request to that domain should result in the request being sent to your machine, then the request gets sent to tomcat, and only then does the virtual host settings above kick in.
In theory you can test if this is the case using ping or traceroute on that domain, or by telnetting to your local machine on the Tomcat port and writing a sample HTTP request and also specify the "Host:www.nikoslianeris.gr" in the HTTP header. In theory.

Linking Apache to Tomcat with multiple domains

Okay, so I've been working for a while on this, and have been searching, but so far I have not found any answers that actually answer what I want to know. I'm a little bit at the end of my rope with this one, but I'm hoping I can figure this out sometime soon.
So I have Apache 2 installed and serving up standard webpages, but I also have that linked to a Tomcat instance for one of my domains currently supported. However, I want to add another domain to the server via Apache that points to a separate code base from the one I already have. I have been coming at this from several different angles, and I have determined that I just don't know enough about setting up these servers to really do what I want to do.
Little information on my server:
Currently running a single Tomcat5.5 instance with Apache 2, using mod_jk to connect them together.
I have a worker in workers.properties that points it's "host" field to "localhost" with the correct port my Tomcat instance, so that all works.
In my Tomcat server.xml file, I have a host defined as "localhost" that points at my webapp that I am currently serving up, with that host set as the defaultHost as well.
One thought I had was that I could add a new worker with a different host than "localhost" (i.e. host2) and then define a new host in my server.xml file called "host2" to match it, but after reading around some on the internet, It seems the "host" of the worker must point to a server, and not a hostname in the Tomcat instance, is this correct?
Again, a simple rundown of what I want:
Setup in apache/tomcat combo such that www.domain1.com points at "webapp1" and www.domain2.com points at "webapp2".
First, setup mod_jk workers for both webapps. Below a sample workers.properties:
workers.tomcat_home=/usr/local/tomcat/apache-tomcat-6.0.20
workers.java_home=/usr/lib/jvm/java-6-sun
ps=/
worker.list=worker1,worker2
worker.worker1.type=ajp13
worker.worker1.host=www.domain1.com
worker.worker1.port=8009
worker.worker2.type=ajp13
worker.worker2.host=www.domain2.com
worker.worker2.port=8009
Then, set up virtual hosts on apache:
<VirtualHost *:80>
ServerName www.domain1.com
JkMount /* worker1
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.com
JkMount /* worker2
</VirtualHost>
Make sure the server.xml contains an uncommented AJP Connector for port 8009 (matching the workers port). Like this :
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Finally, configure the tomcat hosts. Something like this:
<Host name="www.domain1.com"
appBase="/path/to/domain1"
unpackWARs="true"
autoDeploy="true"
xmlValidation="false"
xmlNamespaceAware="false">
<Host name="www.domain2.com"
appBase="/path/to/domain2"
unpackWARs="true"
autoDeploy="true"
xmlValidation="false"
xmlNamespaceAware="false">
You might need to make some adaptation but it should be close to the final result.
You could also use much simpler approach with mod_proxy. Have a look at http://squirrel.pl/blog/2010/03/30/mapping-tomcat-apps-to-subdomains-with-apache/

Categories

Resources