最近有一個 Tomcat 應用需要部署到現有 VPS 上,該 VPS 已經安裝了 Nginx 和 HHVM,綜合考慮多種因素,決定不安裝 Apache,通過 Nginx 反代 Tomcat。

安裝 Nginx

1
2
$ sudo apt update
$ sudo apt install nginx

安裝 Tomcat 7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ sudo apt install tomcat7
$ sudo vim /etc/tomcat7/server.xml
...
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="8443" />
...
$ sudo apt-get install tomcat7-docs tomcat7-admin tomcat7-examples
$ sudo vim /etc/tomcat7/tomcat-users.xml
<tomcat-users>
<user username="user" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>
$ sudo service tomcat7 start

設定 Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ sudo vim /etc/nginx/sites-enabled/default
...
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
#include /etc/nginx/naxsi.rules;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
...

然後可以使用 http://127.0.0.1/manager/htmlhttp://127.0.0.1/host-manager/html管理 tomcat 。