微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用docker部署nginx+tomcat架构

架构说明:

使用Nginx+tomcat实现动态/静态(资源请求)分离和负载均衡。

 

参考文档:

https://www.runoob.com/docker/docker-tutorial.html

 

配置docker镜像仓库:/etc/docker/daemon.json

{
    "registry-mirrors": ["https://registry.docker-cn.com", "http://hub-mirror.c.163.com", "https://pee6w651.mirror.aliyuncs.com"]
}

 

下载Nginx和tomcat的镜像:

docker pull Nginx
docker pull tomcat

 

创建Nginx和tomcat本地目录,稍后将挂载到docker容器上:

mkdir -p Nginx/www Nginx/conf/ Nginx/logs
mkdir -p tomcat/webapps/ROOT tomcat/conf tomcat/logs

 

在tomcat/webapps/ROOT中创建index.html:

<!DOCTYPE html>
<html>
<head>
    <Meta charset="UTF-8">
    <title>docker deployment</title>
</head>
<body>
    <h1>hello, world</h1>
    <img src="/static/image/lichmama.png">
</body>
</html>

 

启动tomcat:

docker run -d --name tomcat1 -v ~/tomcat/webapps:/usr/local/tomcat/webapps tomcat
docker run -d --name tomcat2 -v ~/tomcat/webapps:/usr/local/tomcat/webapps tomcat

 

获取tomcat容器IP,获取到的IP将配置到Nginx配置文件中:

docker inspect tomcat1|grep "IPAddress"
docker inspect tomcat2|grep "IPAddress"

 

Nginx/conf增加配置文件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 {
    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" "$upstream_addr"';

    access_log  /var/log/Nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    upstream tomcat {
        server 172.17.0.2:8080;
        server 172.17.0.4:8080;
    }

    server {
        listen 80;
        server_name localhost;

        location / {
            proxy_pass http://tomcat;
            proxy_redirect off;
            index index.html index.htm;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-Port $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /static/ {
            alias /usr/share/Nginx/html/;
        }
    }

    include /etc/Nginx/conf.d/*.conf;
}
View Code

 

启动Nginx

docker run -d -p 80:80 --name Nginx -v ~/Nginx/www:/usr/share/Nginx/html -v ~/Nginx/conf/Nginx.conf:/etc/Nginx/Nginx.conf -v ~/Nginx/logs:/var/log/Nginx Nginx

 

使用docker ps查看docker进程:

 

访问http://server_ip/index.html:

 

OK,部署成功。

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐