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 stop
sudo /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
至此 配置完成
还有一些其他的配置方法,比如tomcat可配置为多端口对应多站点,但我配置后发现速度明显变慢,未找到原因,而此配置已经能满足要求,特此记录。
网友评论