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.
Related
I am trying to access my localhost server from Parallels Desktop and facing some issues.
For example, I have an app on Spring Boot that runs on port 8081, and running sudo lsof -PiTCP -sTCP:LISTEN in Terminal gives me the following output:
java 8168 username 107u IPv6 0x53524f3d71f26ae5 0t0 TCP *:8081 (LISTEN)
And the output for second application (not Spring Boot, but Spring inside Tomcat 7) is:
java 7756 username 504u IPv6 0x53524f3d6cfe6fa5 0t0 TCP localhost:8096 (LISTEN)
I can easily access the first application from Parallels Desktop by address http://10.211.55.2:8081/, but doing the same for http://10.211.55.2:8096/ gives me this site can’t be reached error message.
So, how can I run my second application on *:8096 instead of localhost:8096?
The IP address tomcat listens in is controlled by the address attribute in the connector element in tomcat configuration file server.xml.
To listen on all addresses, set the address to 0.0.0.0:
<Connector port="8096"
address="0.0.0.0"
...other attributes.../>
I have a remote server, and its default port is 8080, but you know the browser default use 80 port to access it, so can not access my tomcat.
How to configure it to access the tomcat's 8080 port?
It is under the windows.
If I use www.example.com:8080 in the browser I can access the server, but if I use www.example.com I can not access it.
EDIT
I use Tomcat + IIS (Server Consolidation), IIS occupy the 80 port.
You cannot access the URL on http://www.example.com because the browser will try to make a TCP connection on port 80 while tomcat is listening to post 8080.
If you want to change the port tomcat is listening to, see this question
Another option is to use a proxy in front of your tomcat server. You can use apache web server or Nginx. They can listen to port 80 and forward your request to the tomcat server on port 8080.
Update
As mentioned in the comments below, as the post80 is already taken, adding reverse proxy cannot help. Because the reverse proxy will not be able to listen to port 80.
It is recommended to see which process is using port 80 and then a proper solution can be implemented.
Update
As IIS is listening on port 80, it is recommended to configure IIS to act as the reverse proxy.
You can follow this link to do so.
You can change tomcat connector port number.
Navigate to /tomcat-root/conf folder. Within you will find the server.xml file.
Open the server.xml and search for connector port and change it.
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
I am using centOs VPS server with root access. I am using Apache Tomcat to run my java application. The problem that i am facing is that the Tomcat runs on port no 8080 and the default port no of Domain is 80.
so whenever i hit my domain name it move to port no 80 where nothing is present.
My application is working perfectly fine on domain.com:8080/.
I want when even i hit my domain domain.com . it automatically open my Java application. It does not show me the empty space.
I don't want to change my default port no of tomcat to 80.
Is there any way to change the port no. of the website. i am having the root access and using WHMCpanel.
HTTP protocol works on port 80 if you don't specify it. So somehow you need to find a way that when you reach the server at port 80, it redirects it to Tomcat.
I don't want to change my default port no of tomcat to 80. Is there any way to change the port no. of the website. i am having the root access and using WHMCpanel.
The answer is "depends". There are couple workarounds and a good solution to go:
You can create a simple webpage or configure the Apache or Nginx to redirect you to http://domain.com:8080 with a 302 redirection. I don't really recommend to go on that direction, but if you don't want to modify the ports.
Also, there's a trick with iptables where you can redirect the port 80 to 8080, but it that case you'd need to switch off the webserver running at port 80 as the port is already bound.
The good solution: Switch off Nginx or Apache and configure on server.xml the port to 80. Refer to this post for more details.
Nothing seems to be present on port 80 so you could switch off what is running on port 80.
Then do not run tomcat on port 80 because tomcat will need root and this is not a good solution.
The good solution is to redirect port 80 on port 8080 using iptable:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
then to make it persistent:
apt-get install iptables-persistent
iptables-save > /etc/iptables/rules.v4
I have freshly installed apache-tomcat-7.0.62 on server. It starts without any error but i am unable to access it using : -
http://IP:8080/
here IP is ip address of server.
I have also run following command : -
here is the screenshot
Thanks
Find the server.xml located under confs folder of the catalina(tomcat).
Find out Connector tag which each one indicates for one connector isntance.
If the connector comes with no address attribute(as default), so tomcat will listen for all addresses. If it has address attribute, so just remove it out, or either have another Connector to listen for the related ipv4 address.
<Connector ... address="ip_v4_addr"/>
Check tomcat spec for Connectors here.
Next make sure the port is open and allowed to be invoked by any firewall, also make sure the ip-address is valid(public and reachable), and ISP doesn't block it.
Default port for tomcat(HTTP) is 8080 and 8443 for HTTPS, maybe your ISP doesn't let you workout with non-std ports, or the firewall issue.
I have tomcat installed on my local machine. I see it in server.xml where I have below entry
<Connector executor="tomcatThreadPool"
port="${http.port}"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="${https.port}"
acceptCount="100"
maxKeepAliveRequests="15"/>
where http.port value in catalina.properties is 8080 .
But every time I try to access my application it url http://localhost/myApp I get error
could not connect to localhost but it works fine http://localhost:8080/myApp. I am not getting why it expects
the port 8080 when it is already a default port? What should I do so that I do not have to mention port?
When you write http://localhost/myApp on the Address Bar of your Browser, the request always goes to Port 80, and not Port 8080. So the default is Port 80 here. For http://localhost/myApp to work you need to install something like Apache HTTP Server.
Then you can configure it with the help of a connector like mod_jk or mod_proxy to use http://localhost/myAppi, instead of http://localhost:8080/myApp. So that what ever request comes on Port 80 can be diverted to Port 8080 automatically.
Once you will download mod_jk, simply extract the file mod_jk.so to the modules folder of your Apache HTTP Server.
Hopefully the steps written here How to Configure Apache HTTP Server with Apache Tomcat, might help you in doing that.
On a Windows platform all you have to do is changing the port number in server.xml from 8080 to 80 and you are done.
The above is not true for Unix/Linux environments. Changing the port number might work on Windows but i think it might be slightly tricky on Unix/Solaris.
Under UNIX all ports <1024 are "privileged" ports. Only root may open a privileged port. It is still possible but keep in mind that it is not as simple as changing the port number when on Unix.
There are a number of workarounds for this.
http://raibledesigns.com/rd/entry/how_to_run_tomcat_on
http://mihail.stoynov.com/2011/04/04/howto-start-tomcat-on-port-80-without-root-privileges/
http://java-notes.com/
http://www.klawitter.de/tomcat80.html