Docker 中使用Nginx 一个端口启动多个前端项目

1.pull image

bash 复制代码
docker pull nginx

2.创建docker-compose

bash 复制代码
services:
  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "1234:80"
      - "1235:443"
    volumes:
      - ./conf:/etc/nginx/conf.d
      - ./html:/usr/share/nginx/html
      - ./logs:/var/log/nginx
    restart: unless-stopped

3.目录挂载准备

目录结构如下:

bash 复制代码
conf/
├── default.conf    #nginx配置文件
html/
├── app1/           # 应用1
│   ├── index.html
│   └── ...
├── app2/           # 应用2
│   ├── index.html
│   └── ...
├── app3/           # 应用3
│   ├── index.html
│   └── ...
└── index.html      # 默认首页
log/                # 日志目录
docker-compose.yml  # docker-compose 配置信息

4.创建 default.conf 监听80端口同时启动多个前端

bash 复制代码
server {
    listen       80;
    server_name  localhost;
    client_max_body_size 1000M;
    add_header X-Frame-Options SAMEORIGIN;
    
    # 全局 CORS 配置
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

    # 处理 OPTIONS 预检请求
    if ($request_method = 'OPTIONS') {
        add_header Access-Control-Max-Age 1728000;
        add_header Content-Type 'text/plain; charset=utf-8';
        add_header Content-Length 0;
        return 204;
    }

    #-------------------------------------------------------------------------🚀

    # app1前端配置
    location /app1/ {
        alias /usr/share/nginx/html/app1/;
        index index.html index.htm;
        try_files $uri $uri/ /app1/index.html;
    }
        
    # app1 后端服务配置
    location /app1/api/ {
        proxy_pass http://www.sol_china.cn/app1/;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #-------------------------------------------------------------------------🚀

    # app2前端配置
    location /app2/ {
        alias /usr/share/nginx/html/app2/;
        index index.html index.htm;
        try_files $uri $uri/ /app2/index.html;
    }
        
    # app1 后端服务配置
    location /app2/app-api/ {
        proxy_pass http://192.168.0.100:4567/;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #-------------------------------------------------------------------------🚀
    
    # others server config....

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
}

5.构建服务

在对应的目录(见3小节),执行构建命令:

bash 复制代码
docker-compose -p dockernginx up -d

6.Vue项目注意点(如果你徘徊在404的漩涡中请检查以下内容是否和配置路径匹配)

1.路由部分 base 属性

2.vue.config.js publicPath

3.env.production 中 API_BASE_URL

说明:其中1、2对应 对应 location /{appName}/ (前端)** 3 对应服务访问 **location /{API_BASE_URL }/ (后端)

相关推荐
韩家阿杰1 天前
RabbitMQ技术的使用
1024程序员节
CoderYanger2 天前
动态规划算法-简单多状态dp问题:15.买卖股票的最佳时机含冷冻期
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger2 天前
递归、搜索与回溯-FloodFill:33.太平洋大西洋水流问题
java·算法·leetcode·1024程序员节
CoderYanger2 天前
动态规划算法-斐波那契数列模型:2.三步问题
开发语言·算法·leetcode·面试·职场和发展·动态规划·1024程序员节
CoderYanger2 天前
动态规划算法-简单多状态dp问题:16.买卖股票的最佳时机含手续费
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger2 天前
C.滑动窗口-求子数组个数-越短越合法——3258. 统计满足 K 约束的子字符串数量 I
java·开发语言·算法·leetcode·1024程序员节
CoderYanger2 天前
动态规划算法-路径问题:9.最小路径和
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger2 天前
动态规划算法-路径问题:7.礼物的最大价值
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger2 天前
动态规划算法-简单多状态dp问题:12.打家劫舍Ⅱ
开发语言·算法·leetcode·职场和发展·动态规划·1024程序员节
金融小师妹2 天前
机器学习驱动分析:ADP就业数据异常波动,AI模型预测12月降息概率达89%
大数据·人工智能·深度学习·编辑器·1024程序员节