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://localhost
和 http://localhost:81
,应该分别看到两个不同的 Nginx 欢迎页面。如果遇到任何问题,请检查日志并确保配置文件内容正确。