本文介绍了Nginx+Tomcat多站点部署的方法,分享给大家,具体如下:
Tomcat配置:
添加多域名:
Engine节点下:
添加域名1
1
2
3
4
5
6
7
|
< 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
1
2
3
4
5
6
7
|
< 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偶尔会报空指针错误,导致无法使用
1
2
3
4
5
6
7
|
< 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:
1
2
|
sudo /etc/init .d /tomcat8 stop sudo /etc/init .d /tomcat8 start |
或
1
|
sudo /etc/init .d /tomcat8 restart |
若域名解析完成,则可在浏览器中进行测试
http://test1.java.com:8080
http://test2.java.com:8080
若正确,则可正常访问
配置Nginx
进入Nginx根目录(以ubuntu 16.04为准)
1
|
cd /etc/nginx |
进入sites-available目录
1
|
cd sites-available |
创建配置文件(已test1.java.com为例)
1
|
vi test1.java.com |
输入配置内容
1
2
3
4
5
6
7
8
9
10
11
12
|
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进行保存
创建软连接已进行使用
1
|
ln -s /etc/nginx/sites-available/test1 .java.com /etc/nginx/sites-enabled/test1 .java.com |
重启Nginx
1
2
3
4
|
/etc/init .d /nginx stop /etc/init .d /nginx start 或 /etc/init .d /nginx restart |
至此 配置完成
还有一些其他的配置方法,比如tomcat可配置为多端口对应多站点,但我配置后发现速度明显变慢,未找到原因,而此配置已经能满足要求,特此记录。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.jianshu.com/p/d4c206779880?utm_source=tuicool&utm_medium=referral