How to listen in two port numbers in tomcat?

时间:2016-02-12 22:08:21

标签: tomcat amazon-ec2

I have an EC2 instance with tomcat 8. this tomcat has a rest web service (in port 8080) that I consume in an android application. Now, I want to put a website in that same tomcat. I'm able to acces the website in this way:

www.domain.com:8080

I know that to access the website without the port number I need to change the default tomcat port to 80. But if I do that, my Android application will not be able to access the web service anymore.

So, how can I do this? Access the website without specify the port number and preserve the 8080 port for my android application?

EDIT:

This is my server.xml

update table2 set university_id = '$val' where b.name = '$name';

EDIT 2:

If I run this:

<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
    type="org.apache.catalina.UserDatabase"
    description="User database that can be updated and saved"
    factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />

<Connector port="80" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<Engine name="Catalina" defaultHost="localhost">
    <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
            resourceName="UserDatabase"/>
    </Realm>

    <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
            prefix="localhost_access_log" suffix=".txt"
            pattern="%h %l %u %t &quot;%r&quot; %s %b" />

    </Host>

</Engine>
</Service>
    </Server>

I get this:

sudo netstat -lnp | grep 2563 (tomcat PID)

2 个答案:

答案 0 :(得分:0)

在conf / server.XML中添加/更新新/现有连接器。根据需要更改端口或协议。

答案 1 :(得分:0)

就像@Sheetal Mohan Sharma对他的回答一样:“由于安全问题,80通常会被阻止”。

所以,这给了我正确的方向。 事实上,端口80确实被阻止了,在linux中解决这个问题的解决方法是安装 authbind 。但对我来说,这并不能解决问题,因为 authbind 无法在我的ec2实例中安装(例如 yum install )。

为了最终解决这个问题,我遵循了这个网站的教程:

http://www.excelsior-usa.com/articles/tomcat-amazon-ec2-advanced.html

步骤如下:

1 - 将传入端口80请求转发到端口8080

sudo /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

2 - 测试一下。如果有效,您可以保存此iptables规则

sudo /sbin/service iptables save

3 - 如链接中所述:最后,您需要Web应用程序中的servlet,就好像传入的请求被定向到端口80.这将防止任何非特权端口出现在任何端口中。发送回客户端的URL。在server.xml中的HTTP连接器配置中包含proxyPort属性:

<Connector port="8080" proxyPort="80" .../>