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
如此便可在瀏覽器上測試使用二級域名直接訪問項目