首页 > 网站 > Nginx > 正文

Nginx+Tomcat多站点部署的方法

2024-08-30 12:25:20
字体:
来源:转载
供稿:网友

本文介绍了Nginx+Tomcat多站点部署的方法,分享给大家,具体如下:

Tomcat配置:

添加多域名:

Engine节点下:

添加域名1

<Host name="test1.java.com" appBase="webapps1"    unpackWARs="true" autoDeploy="true">  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"      prefix="localhost_access_log" suffix=".txt"      pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/var/lib/tomcat8/webapps/test1" debug="0" reloadable="true" crossContext="true" /> </Host>

添加域名2

<Host name="test1.java.com" appBase="webapps2"    unpackWARs="true" autoDeploy="true">  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"      prefix="localhost_access_log" suffix=".txt"      pattern="%h %l %u %t "%r" %s %b" />  <Context path="" docBase="/var/lib/tomcat8/webapps/manage" debug="0" reloadable="true" crossContext="true" /> </Host>

注意,还需要一个默认语句,即name="localhost"的域名,否则tomcat偶尔会报空指针错误,导致无法使用

 <Host name="localhost" appBase="webapps0"    unpackWARs="true" autoDeploy="true">  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"      prefix="localhost_access_log" suffix=".txt"      pattern="%h %l %u %t "%r" %s %b" />  <Context path="" docBase="/var/lib/tomcat8/webapps/manage" debug="0" reloadable="true" crossContext="true" /> </Host>

至此,配置完成,重启tomcat:

sudo /etc/init.d/tomcat8 stopsudo /etc/init.d/tomcat8 start


sudo /etc/init.d/tomcat8 restart

若域名解析完成,则可在浏览器中进行测试

http://test1.java.com:8080
http://test2.java.com:8080

若正确,则可正常访问

配置Nginx

进入Nginx根目录(以ubuntu 16.04为准)

cd /etc/nginx

进入sites-available目录

cd sites-available

创建配置文件(已test1.java.com为例)

vi test1.java.com

输入配置内容

server{    listen 80;    server_name test1.java.com;    server_name_in_redirect off;    proxy_set_header Host $host:$server_port;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header REMOTE-HOST $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    location / {        proxy_pass http://test1.java.com:8080;    }}

esc后:wq进行保存

创建软连接已进行使用

ln -s /etc/nginx/sites-available/test1.java.com /etc/nginx/sites-enabled/test1.java.com

重启Nginx

/etc/init.d/nginx stop/etc/init.d/nginx start或/etc/init.d/nginx restart            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表