使用docker部署nginx+tomcat架构

你的名字 2023-06-01 11:16 130阅读 0赞

架构说明:

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

参考文档:

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

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

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

下载nginx和tomcat的镜像:

  1. docker pull nginx
  2. docker pull tomcat

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

  1. mkdir -p ~/nginx/www ~/nginx/conf/ ~/nginx/logs
  2. mkdir -p ~/tomcat/webapps/ROOT ~/tomcat/conf ~/tomcat/logs

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>docker deployment</title>
  6. </head>
  7. <body>
  8. <h1>hello, world</h1>
  9. <img src="/static/image/lichmama.png">
  10. </body>
  11. </html>

启动tomcat:

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

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

  1. docker inspect tomcat1|grep "IPAddress"
  2. docker inspect tomcat2|grep "IPAddress"

在nginx/conf增加配置文件nginx.conf:

ContractedBlock.gif ExpandedBlockStart.gif

  1. user nginx;
  2. worker_processes 1;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. #tcp_nopush on;
  17. keepalive_timeout 65;
  18. #gzip on;
  19. upstream tomcat {
  20. server 172.17.0.2:8080;
  21. server 172.17.0.4:8080;
  22. }
  23. server {
  24. listen 80;
  25. server_name localhost;
  26. location / {
  27. proxy_pass http://tomcat;
  28. proxy_redirect off;
  29. index index.html index.htm;
  30. proxy_set_header Host $host;
  31. proxy_set_header X-Real-IP $remote_addr;
  32. proxy_set_header X-Real-Port $remote_port;
  33. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  34. }
  35. location /static/ {
  36. alias /usr/share/nginx/html/;
  37. }
  38. }
  39. include /etc/nginx/conf.d/*.conf;
  40. }

启动nginx:

  1. 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进程:

644402-20190816203440321-663999604.png

访问http://server\_ip/index.html:

644402-20190816204151669-313297097.png

OK,部署成功。

转载于:https://www.cnblogs.com/lichmama/p/11366262.html

发表评论

表情:
评论列表 (有 0 条评论,130人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginxTomcat集群

    tomcat与nginx,apache的区别是什么? Tomcat与Nginx、Apache的关系如下图。由配送中心选择Apache、Nginx中的某一辆货车将包裹送给客