nginx+tomcat实现单个IP地址,多个二级域名+多个站点访问

来源:互联网 发布:2017爱情电影 知乎 编辑:程序博客网 时间:2024/04/30 09:17

1.部署多台tomcat

  简单的在一台服务器上部署多台tomcat最简单不过了,只需要将tomcat压缩包解压到多个目录,然后更改/conf/server.xml配置文件中的三个端口:

  a.Server port=”8[X]05″ shutdown=”SHUTDOWN”

  b.Connector port=”8[X]80″ maxHttpHeaderSize=”8192″ …

  c.Connector port=”8[X]09″ enableLookups=”false”

  这里用‘X’代替第几台tomcat,默认的分别为8005,8080,8009。每增加一台tomcat只需要将这三个数字递增就可以。这样就可以根据IP+8[X]80来区别不同的程序了。

2.用nginx绑定域名与不同的tomcat端口

  nginx配置文件位于conf/nginx.conf,它最主要的部分是http部分,这里最重要的两个配置项是upstream,server,这两个项都可以有多个配置。

在http{}内插入下面代码。

upstream home.console.xinyi8090.cn {  server 60.205.149.58:8082;          }      upstream home.vendor.xinyi8090.cn {  server 60.205.149.58:8080;    }    server {        listen       80;        server_name  home.console.xinyi8090.cn;        location / {            index  index.html index.jsp;                proxy_pass  http://home.console.xinyi8090.cn;                proxy_set_header    X-Real-IP   $remote_addr;                client_max_body_size    100m;         }    }    server {        listen       80;        server_name home.vendor.xinyi8090.cn;        location / {            index  index.html index.jsp;                proxy_pass  http://home.vendor.xinyi8090.cn;                proxy_set_header    X-Real-IP   $remote_addr;                client_max_body_size    100m;         }    }

大功告成,亲测有效。

3.扩展修改tomcat默认页

  很多时候我们想输入域名之后就可以访问主页,这时候就需要配置tomcat的默认页面。

首先,修改$tomcat/conf/server.xml文件。
在<host></host>标签之间添加上:

<Context path="" docBase="mypr" debug="0" reloadable="true" />


path是说明虚拟目录的名字,如果你要只输入ip地址就显示主页,则该键值留为空;


docBase是虚拟目录的路径,它默认的是$tomcat/webapps/ROOT目录,现在我在webapps目录下建了一个mypro项目,让该项目文件作为我的默认目录。

然后,修改$tomcat/conf/web.xml文件。
在web.xml文件中,有一段如下:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

改成mypro项目中需要直接显示的jsp或者html即可。

2 1
原创粉丝点击