docker使用nginx注意事项

92 阅读1分钟

docker使用nginx注意事项

一、docker-compose.yml文件修改

version: '3'
services:
  mysql:
    image: "mysql:8.0.27"
    container_name: "hosp-mysql"
    restart: always
    ports:
    - "3308:3306"
    volumes:
    - /etc/localtime:/etc/localtime:ro
    - #BASE_FOLDER/mysql:/var/lib/mysql
    environment:
    - MYSQL_ROOT_PASSWORD=Ro@2021.com

  redis:
    image: "redis:6.2.6"
    container_name: "hosp-redis"
    restart: always
    entrypoint: redis-server --appendonly yes
    volumes:
    - /etc/localtime:/etc/localtime:ro
    - #BASE_FOLDER/redis:/data

  clickhouse:
    image: yandex/clickhouse-server:21.3.20-alpine
    container_name: hosp-clickhouse-server
    restart: always
    privileged: true
    stdin_open: true
    tty: true
    ports:
      - "8123:8123"
    volumes:
      - ./config/docker_related_config.xml:/etc/clickhouse-server/config.d/docker_related_config.xml:rw
      - ./config/config.xml:/etc/clickhouse-server/config.xml:rw
      - ./config/users.xml:/etc/clickhouse-server/users.xml:rw     
      - /etc/localtime:/etc/localtime:ro
      - #BASE_FOLDER/clickhouse/data:/var/lib/clickhouse:rw
      - #BASE_FOLDER/clickhouse/log/clickhouse-server:/var/log/clickhouse-server

  nginx:
    image: nginx:1.21.1
    container_name: "hosp-nginx"
    restart: always
    ports:
      - 9090:9090
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:rw
      - ./nginx/dist:/usr/share/nginx/html
      - ./nginx/dist:/#BASE_FOLDER/nginx/dist


  boot:
    depends_on:
    - mysql
    - redis
    - clickhouse
    image: "openjdk:8"
    container_name: "hosp-boot"
    restart: always
    entrypoint: java -jar -Duser.timezone=GMT+08 /home/bioinformatics/boot/hosp-appd-bt.jar --spring.profiles.active=prod
    ports:
    - "9001:8080"
    user: "1009"
    networks:
    - default
    - apache_default
    volumes:
    - /etc/localtime:/etc/localtime:ro
    - #BASE_FOLDER/app:/home/bioinformatics/boot:ro
    - #BASE_FOLDER/repo:/home/bioinformatics/repo
    - #BASE_FOLDERSX/fastq:#BASE_FOLDERSX/fastq
    - #BASE_FOLDER/file/pdf:#BASE_FOLDER/pdf
    - #BASE_FOLDER/jasper/template/:#BASE_FOLDER/jasper/template/
    - #BASE_FOLDER/jasper/picture/:#BASE_FOLDER/jasper/picture/
    - #BASE_FOLDERSX/results:#BASE_FOLDERSX/results
    - #BASE_FOLDERSX/original_fastq/:#BASE_FOLDERSX/original_fastq
    - #BASE_FOLDERSX/process:#BASE_FOLDERSX/process
    - #BASE_FOLDERSX/bin:#BASE_FOLDERSX/bin
    - #BASE_FOLDERSX/data/:#BASE_FOLDERSX/data/
networks:
  apache_default:
    external: true

二、nginx.conf修改

#user  nobody;
worker_processes 1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;
events {
    worker_connections 1024;
}
​
​
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
​
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
​
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
​
      server {
    listen 9090;
    server_name localhost;
    client_max_body_size 100m;
    client_body_timeout 300s;
    location / {
        root /NGS/nginx/dist/;
        gzip on;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_comp_level 4;
        gzip_types font/x-font-woff text/plain application/javascript text/css application/xml text/javascript
        index index.html index.htm;
    }
    location ^~/api/ {
        proxy_pass http://boot:8080/;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-Port $remote_port;
    }
    location /JNPROJ1/NGS-panels/ {
            alias /JNPROJ1/NGS-panels/;
    }
      location /assets {
            root /NGS/nginx/dist;
        }
}
    
}
​

其中 api动态代理的IP是docker-comopse.yml 配置文件中的 boot (java后端容器名称 /容器IP

端口也使用 boot容器内的端口(8080)

location ^~/api/ { proxy_pass http://boot:8080/; proxy_set_header X-Real-IP remoteaddr;proxysetheaderXRealPortremote_addr; proxy_set_header X-Real-Port remote_port; }

三、移动nginx.conf配置文件

将修改好的nginx.conf文件放到 docker-compose.yml文件所在目录 下的nginx中,前端页面也放在 nginx.conf同级目录下的 dist目录中

  nginx:
    image: nginx:1.21.1
    container_name: "hosp-nginx"
    restart: always
    ports:
      - 9090:9090
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:rw
      - ./nginx/dist:/usr/share/nginx/html
      - ./nginx/dist:/#BASE_FOLDER/nginx/dist

四、重新构建容器

要先切换到docker-compose.yml文件目录下

docker-compose up -d