1、在域名管理平台添加上二级域名,此处不具体介绍实现流程
2、tomcat中的配置:
<!-- 原有部分start -->
<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 "%r" %s %b" />
</Host>
<!-- 原有部分end -->
<!-- 第一个域名配置 -->
<Host name="work.testdomain.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/home/testdomain/tomcat-8.5.51/webapps/work" debug="0" reloadable="true"/>
</Host>
<!-- 第二个域名配置 -->
<Host name="better.testdomain.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/home/testdomain/tomcat-8.5.51/webapps/better" debug="0" reloadable="true"/>
</Host>
如果在多个二级域名,复制Host节点,然后把Host节点中的name换成你想配置的二级域名,path值为空,docBase的值为项目在tomcat中绝对路径。
3、nginx中的配置:
server {
listen 80;
server_name work.testdomain.com;
location / {
proxy_pass http://192.168.107.128:8080;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
break;
}
}
server {
listen 80;
server_name better.testdomain.com;
location / {
proxy_pass http://192.168.107.128:8081;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
break;
}
}
注意:proxy_pass的值格式为:http://ip:port,IP请换成自己服务器真实的IP,必须带http://否则会报URL错误,如果有多个二级域名,配置对应server{}即可
修改完成之后,进入nginx的sbin进入,使用命令,检测下配置是否正确,
./nginx -t
如果有误请参照提示修改,直到出现下面的内容方才通过:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
无须重新启动nginx,不过最好重新加载下nginx配置文件
./nginx -s reload
如此便可在浏览器上测试使用二级域名直接访问项目