1.下载tomcat
docker pull hub.c.163.com/library/tomcat:8.0.46-jre8
2.启动tomcat,在浏览器输入地址访问。
# 启动tomcat1
docker run -d -p 8082:8080 --name tomcat1 tomcat
# 启动tomcat2
docker run -d -p 8083:8080 --name tomcat2 tomcat
3.创建index.jsp
vi index.jsp
#index.jsp content
This is tomcat1.
4.放到tomcat1的root中,然后修改index.jsp的内容(This is tomcat2.)放到tomcat2
docker cp index.jsp tomcat1:/usr/local/tomcat/webapps/ROOT/index.jsp
5.下载nginx
docker pull hub.c.163.com/library/nginx:latest
6.创建一个nginx.conf
vi nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
proxy_pass http://blance;
}
}
upstream blance{
server 192.168.1.1:8082;
server 192.168.1.1:8083;
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
nginx重写url
#比如访问A:http://127.0.0.1:8080/cl/upload/users/2019-06-0715599128106935009229562019845111.png
#实际访问B:http://127.0.0.1:8080/2019-06-0715599128106935009229562019845111.png
#1、^: 匹配字符串的开始位置;
#2、$:匹配字符串的结束位置;
#3、.*: .匹配任意字符,*匹配数量0到正无穷;
#4、\. 斜杠用来转义,\.匹配 .
#5、(值1|值2|值3|值4):或匹配模式,例:(jpg|gif|png|bmp)匹配jpg或gif或png或bmp
#6、i不区分大小写
# $1表示匹配第二个正则表达式的内容
location ~ /upload/users/(.*) {
proxy_pass http://127.0.0.1:8088/$1;
}
7.启动nginx,去浏览器不断的访问8081端口的地址,看是不是会出现tomcat1和tomcat2
# -v表示挂载本地目录 -d表示在后台运行 -p表示映射端口
docker run -p 8081:80 --name nginx1 -v ~/hyp/nginx.conf:/etc/nginx/nginx.conf -d hub.c.163.com/library/nginx
#挂载一个文件访问目录/data/ftp/
docker run -p 8081:80 --name nginx1 -v ~/hyp/nginx.conf:/etc/nginx/nginx.conf -v /data/ftp/:/data/ftp/ -d hub.c.163.com/library/nginx
来源:oschina
链接:https://my.oschina.net/u/2489258/blog/3213200