NGINX + TOMCAT + Docker 环境构筑

NGINX

Nginx是一款轻量级的Web服务器、反向代理服务器,同时也提供了IMAP/POP3/SMTP服务,在BSD-like 协议下发行。由于它的内存占用少,启动极,高并发能力强,在互联网项目中广泛应用。

相比Apache,克服了C10K问题。

Nginx读作:[‘ɛndʒɪn eks] (engine x)

官网: NGINX

Nginx配置: Nginx配置详解

Tomcat

正式名称:Apache Tomcat
官网: Apache Tomcat®

Docker

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux或Windows操作系统的机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。

官网: Docker

全体像

全体构成如下:

環境

OS: windows10
Docker: 已安装完成

Dockerfile

  • nginx-server/Dockerfile
1
2
3
4
5
FROM nginx:latest

EXPOSE 80

RUN rm -rf /etc/nginx/conf.d
  • tomcat-server/Dockerfile
1
2
3
FROM tomcat:latest

EXPOSE 8080
  • docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
version: "3"
services:
nginx-server:
build:
context: ./nginx-server
volumes:
- ./nginx-server/conf.d:/etc/nginx/conf.d
- ./nginx-server/log:/var/log/nginx
ports:
- 80:80

tomcat-server:
build:
context: ./tomcat-server
volumes:
- ./tomcat-server/webapps:/usr/local/tomcat/webapps
- ./tomcat-server/log:/usr/local/tomcat/logs
ports:
- 8080:8080

文件夹构成如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
│  docker-compose.yml

├─nginx-server
│ │ Dockerfile
│ │
│ ├─conf.d
│ │
│ └─log

└─tomcat-server
│ Dockerfile

├─log

└─webapps

容器做成

% docker-compose build

容器配置

  • nginx-server/conf.d/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name localhost;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

location /test/ {
proxy_pass http://host.docker.internal:8080/test/;
}

#location /test/ {
# rewrite /test/qa1 /test/test.jsp break;
# rewrite /test/qa2 /test/a/a1/a1.html break;
# proxy_pass http://host.docker.internal:8080/;
#}
}
  • tomcat-server/webapps/test/test.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<html>
<head>
<title>index.jsp</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
function submit1(url)
{
document.form1.action=url;
document.form1.submit()
}
// -->
</SCRIPT>
</head>
<body>
<form name="form1" method="POST" action="">
<INPUT type="hidden" name="formPara" value="formPara">
</form>
index.jsp
<br/>
<a href="#" onClick="submit1('requestForForward.jsp?redirectOrForward=requestForForward')">requestForForward.jsp</a>
<br/>
<a href="#" onClick="submit1('requestForRedirect.jsp?redirectOrForward=requestForRedirect')">requestForRedirect.jsp</a>

</body>
</html>
  • tomcat-server/webapps/test/redirectOrForward.jsp
1
2
3
4
5
6
<%out.println("redirectOrForward.jsp<br/>");
String str=request.getParameter("redirectOrForward");
out.println(str+"<br/>");
String str1=request.getParameter("formPara");
out.println(str1+"<br/>");
%>
  • tomcat-server/webapps/test/requestForForward.jsp
1
2
3
4
5
6
7
8
<jsp:forward page="redirectOrForward.jsp"/>

<%out.println("requestForForward.jsp<br/>");
String str=request.getParameter("redirectOrForward");
out.println(str+"<br/>");
String str1=request.getParameter("formPara");
out.println(str1+"<br/>");
%>
  • tomcat-server/webapps/test/requestForRedirect.jsp
1
2
3
4
5
6
7
8
9
10
<%
response.sendRedirect("redirectOrForward.jsp");
%>

<%out.println("requestForRedirect.jsp<br/>");
String str=request.getParameter("redirectOrForward");
out.println(str+"<br/>");
String str1=request.getParameter("formPara");
out.println(str1+"<br/>");
%>

启动

% docker-compose up -d

运行

其他命令

容器停止:

% docker-compose down

已安装镜像确认:

% docker images

运行中容器确认:

% docker ps

进入容器内:

% docker exec -it 容器id /bin/bash

nginx更新配置

nginx -s reload

Author: jimmy367
Link: http://www.ohtudou.com/2022/09/28/make-nginx-tomcat-dev/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
支付宝打赏