前端发布到docker,容器内nginx做反向代理到其他的docker容器配置

事件过程:前端打包发布到docker容器,会用到nginx做反向代理到其他不同的容器。
说几个注意的点:
1、前端打包之前,一定要配置服务器坐在的地址到baseUrl,不能再使用localhost

'use strict'
module.exports = {
  NODE_ENV: '"production"',
  API: '"http://192.168.0.221"',
}

2、nginx配置文件内容


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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"';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
	server{
		listen 80;
		server_name localhost;
		location /{
			alias /xx/xx/dist/;
			index index.html index.htm;
			try_files $uri $uri/ @router; # 需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404		
			}
    location @router {
        rewrite ^.*$ /index.html last;
    }
	location /aaa/ {
		rewrite ^/b/(.*)$ /$1 break;
		proxy_pass http://xx8084:8084/;
	}
	location /bbb/ {
		rewrite ^/b/(.*)$ /$1 break;
		proxy_pass http://xx8083:8083/;
	}
    }
}

3、前端打包文件的Dockerfile文件

FROM nginx:latest
MAINTAINER snd
COPY dist/  /xx/xx/dist/ 
COPY nginx.conf /etc/nginx/nginx.conf
RUN rm /etc/nginx/conf.d/default.conf
RUN echo 'done'

4、docker-compose.yml文件

version: '3'
services:
  xxservice8083:
    image: xx_service:8083
    container_name: xx8083
    ports:
      - 8083:8083
    networks:
      - xx_net
  xxservice8084:
    image: xx_service:8084
    container_name: xx8084
    ports:
      - 8084:8084
    networks:
      - xx_net
  xxui:
    image: xx_ui:1.0
    container_name: xxui
    ports:
      - 80:80
    networks:
      - xx_net
    depends_on:
      - xxservice8083
      - xxservice8084
networks: 
   xx_net: 

这里需要注意的是network需要在同一个网桥下,上面都是xx_net这个网桥下

你可能感兴趣的:(笔记,nginx,前端,docker)