Nginx+Tomcat multi site deployment approach
- 2020-05-13 04:41:25
- OfStack
This paper introduces the method of Nginx+Tomcat multi-site deployment and shares it with you as follows:
Tomcat configuration:
Add multiple domain names:
Under the Engine node:
Add domain name 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>
Add domain name 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>
Note that you also need one default statement, the domain name name="localhost", otherwise tomcat will occasionally report a null pointer error and become unusable
<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>
At this point, the configuration is complete and tomcat is restarted:
sudo /etc/init.d/tomcat8 stop
sudo /etc/init.d/tomcat8 start
or
sudo /etc/init.d/tomcat8 restart
Once the domain name resolution is complete, you can test it in your browser
http://test1.java.com:8080
http://test2.java.com:8080
If correct, it can be accessed normally
Configuration Nginx
Enter the Nginx root directory (ubuntu 16.04 shall prevail)
cd /etc/nginx
Enter the sites-available directory
cd sites-available
Create a configuration file (test1.java.com for example)
vi test1.java.com
Enter configuration content
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;
}
}
After esc :wq is saved
Creating a soft connection has been used
ln -s /etc/nginx/sites-available/test1.java.com /etc/nginx/sites-enabled/test1.java.com
Restart Nginx
<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>
0
Configuration is now complete
There are also some other configuration methods, such as tomcat, which can be configured for multiple ports and multiple sites. However, I found that the speed was obviously slowed down after configuration, and I did not find the reason. However, this configuration has already met the requirements.