docker启动nginx和tomcat

2023-11-17 13:42:15 浏览数 (1)

前言

利用docker启动tomcat,并用nginx做反向代理

docker安装

centos8安装docker和docker-compose

docker-compose

代码语言:javascript复制
version: "3"
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./app:/www/web
      - ./services/nginx/conf:/etc/nginx
      - /var/log/lnt/nginx:/www/web_logs
    networks:
        - lnet
    depends_on:
      - tomcat
  tomcat:
    image: tomcat:latest
    ports:
      - "8080:8080"
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./app:/usr/local/tomcat/webapps
      - ./services/tomcat/config:/usr/local/tomcat/conf
      - /var/log/lnt/tomcat:/usr/local/tomcat/logs
    networks:
        - lnet

networks:
  lnet:
    driver: bridge

nginx配置

代码语言:javascript复制
upstream tomcat_server {
    server tomcat:8080;
}

server {
    listen     80  backlog=1024;
    server_name  0.0.0.0;
    root  /www/web/;
    server_tokens off;

    access_log /www/web_logs/www_access.log wwwlogs;
    error_log  /www/web_logs/www_error.log  notice;

    client_max_body_size 50m;

    location / {
        proxy_pass http://tomcat_server;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
    }

    location ~* .(eot|ttf|woff|woff2|svg)$ {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
    }

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
    }
    location ~ .*.(js|css)?$ {
        expires      12h;
    }
}

tomcat

数据目录

验证

app/ROOT目录下,创建index.html文件。内容为:test

启动服务

代码语言:javascript复制
docker-compose up -d

访问

出现index.html中的内容即正常

0 人点赞