同一台机器映射端口不一样,部署两个服务

version: '3'

services:
  nginx:
    image: nginx:latest
    container_name: my_nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./conf.d:/etc/nginx/conf.d
      - ./html:/usr/share/nginx/html
    ports:
      - "80:80"
    environment:
      - NGINX_HOST=localhost
      - NGINX_PORT=80

volumes:
  conf.d:
  html:

nginx的docker-compose的配置如下,如果在同一台机器上部署两个nginx应该怎么写呢?

如果你想在同一台机器上再部署一个 Nginx 实例,可以通过创建另一个服务并为其分配不同的端口和容器名称。以下是更新后的 docker-compose.yml 文件:

version: '3'

services:
  nginx1:
    image: nginx:latest
    container_name: my_nginx1
    volumes:
      - ./nginx1/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx1/conf.d:/etc/nginx/conf.d
      - ./nginx1/html:/usr/share/nginx/html
    ports:
      - "80:80"
    environment:
      - NGINX_HOST=localhost
      - NGINX_PORT=80

  nginx2:
    image: nginx:latest
    container_name: my_nginx2
    volumes:
      - ./nginx2/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx2/conf.d:/etc/nginx/conf.d
      - ./nginx2/html:/usr/share/nginx/html
    ports:
      - "81:80"
    environment:
      - NGINX_HOST=localhost
      - NGINX_PORT=81

volumes:
  nginx1_conf.d:
  nginx1_html:
  nginx2_conf.d:
  nginx2_html:

相关文件和目录结构

你需要为每个 Nginx 服务创建单独的配置文件和内容目录,以避免冲突。假设你的项目目录结构如下:

project/
├── docker-compose.yml
├── nginx1/
│   ├── nginx.conf
│   ├── conf.d/
│   │   └── default.conf
│   └── html/
│       └── index.html
└── nginx2/
    ├── nginx.conf
    ├── conf.d/
    │   └── default.conf
    └── html/
        └── index.html

nginx1/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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_names_hash_bucket_size 128;
}

nginx1/conf.d/default.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;
    root /usr/share/nginx/html;

    location / {
        try_files $uri $uri/ =404;
    }
}

nginx1/html/index.html

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to nginx1!</title>
</head>
<body>
    <h1>Success! The nginx1 server is working!</h1>
</body>
</html>

nginx2/nginx.conf

内容与 nginx1/nginx.conf 相同:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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_names_hash_bucket_size 128;
}

nginx2/conf.d/default.conf

内容与 nginx1/conf.d/default.conf 相同:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;
    root /usr/share/nginx/html;

    location / {
        try_files $uri $uri/ =404;
    }
}

nginx2/html/index.html

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to nginx2!</title>
</head>
<body>
    <h1>Success! The nginx2 server is working!</h1>
</body>
</html>

启动服务

在项目根目录下运行以下命令启动两个 Nginx 服务:

docker-compose up -d

这将启动两个 Nginx 容器,一个监听主机的 80 端口,另一个监听 81 端口。你可以在浏览器中访问 http://localhosthttp://localhost:81,应该分别看到两个不同的 Nginx 欢迎页面。如果遇到任何问题,请检查日志并确保配置文件内容正确。

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值