首页
/ Nacos-Nginx 模板项目教程

Nacos-Nginx 模板项目教程

2024-08-18 00:15:42作者:钟日瑜

项目目录结构及介绍

nacos-nginx-template/
├── README.md
├── conf
│   ├── nginx.conf
│   └── nacos.conf
├── docker-compose.yml
└── scripts
    └── start.sh
  • README.md: 项目说明文档。
  • conf: 包含 Nginx 和 Nacos 的配置文件。
    • nginx.conf: Nginx 的主配置文件。
    • nacos.conf: Nacos 的配置文件。
  • docker-compose.yml: Docker Compose 文件,用于定义和运行多个 Docker 容器。
  • scripts: 包含启动脚本。
    • start.sh: 启动项目的脚本。

项目的启动文件介绍

docker-compose.yml

version: '3'
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - ./conf/nacos.conf:/etc/nginx/conf.d/nacos.conf
  nacos:
    image: nacos/nacos-server:latest
    environment:
      - MODE=standalone
    ports:
      - "8848:8848"
  • nginx: 使用最新版本的 Nginx 镜像,将本地的 nginx.confnacos.conf 文件挂载到容器中。
  • nacos: 使用最新版本的 Nacos 镜像,设置运行模式为 standalone,并映射端口 8848。

start.sh

#!/bin/bash
docker-compose up -d
  • start.sh: 启动 Docker Compose 服务,-d 参数表示在后台运行。

项目的配置文件介绍

nginx.conf

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;
    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
}
  • nginx.conf: Nginx 的主配置文件,定义了基本的 Nginx 配置,包括用户、工作进程、日志格式等。

nacos.conf

server {
    listen       80;
    server_name  localhost;

    location /nacos/ {
        proxy_pass http://nacos:8848/nacos/;
    }
}
  • nacos.conf: Nginx 的虚拟主机配置文件,将 /nacos/ 路径的请求代理到 Nacos 服务。

以上是基于开源项目 nacos-nginx-template 的教程,涵盖了项目的目录结构、启动文件和配置文件的详细介绍。希望这些内容能帮助你更好地理解和使用该项目。

登录后查看全文
热门项目推荐