如何使用 Docker NGINX 映像为带有域名的一对容器提供服务
How to serve couple containers with domain names with Docker NGINX image
我以后需要用我的 docker 机器托管多个站点,但我知道我正在尝试在本地主机上启动它。
主要目的是获取某个站点,当然我需要从 OS X 上的 /etc/hosts
文件开始,下面是清单:
127.0.0.1 localhost
255.255.255.255 broadcasthost
127.0.0.1 dev.proling.ru
127.0.0.1 dev.vshvetsov.ru
此配置使我可以独立于 DNS 设置访问具有选定域的站点。现在我将展示我的 docker-compose.yml
:
version: '2'
services:
mysql:
build: mysql/
restart: always
volumes:
- mysql_data:/var/lib/mysql
container_name: mysql
vshvetsov:
depends_on:
- mysql
build: vshvetsov/
ports:
- 8000:80
restart: always
container_name: vshvetsov
proling:
depends_on:
- mysql
build: proling/
ports:
- 8003:80
restart: always
container_name: proling
nginx:
depends_on:
- proling
- vshvetsov
image: nginx
restart: always
ports:
- "80:80"
volumes:
- ./nginx/volume:/etc/nginx
container_name: nginx
volumes:
mysql_data:
两个站点容器在 localhost:8000
和 localhost:8003
上都能完美运行,下面是我的 nginx/volume
文件夹树:
volume
├── conf.d
│ ├── default.conf
│ ├── proling.ru
│ └── vshvetsov.ru
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf
所以影响我的 NGINX 配置的唯一方法是将 .conf
个文件放在 conf.d
目录中,下面是它们:
dev.proling.ru
server {
listen 80;
server_name www.proling.ru;
rewrite ^ http://proling.ru$request_uri? permanent; #301 redirect
}
server {
listen 80;
server_name proling.ru *.proling.ru;
location / {
proxy_pass http://proling;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
access_log /var/log/nginx/proling.ru-access.log;
}
}
和dev.vshvetsov.ru
server {
listen 80;
server_name www.vshvetsov.ru vshvetsov.ru dev.vshvetsov.ru;
location / {
proxy_pass http://vshvetsov;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
access_log /var/log/nginx/vshvetsov.ru-access.log;
}
}
通过重命名 conf.d
中的 nginx 配置文件解决了问题。它们必须以 .conf
扩展名结尾,因为 nginx.conf
默认具有此字符串:
include /etc/nginx/conf.d/*.conf;
当然,如果方便的话,我可以挂载或复制我们的配置,但我更喜欢默认路径。
我以后需要用我的 docker 机器托管多个站点,但我知道我正在尝试在本地主机上启动它。
主要目的是获取某个站点,当然我需要从 OS X 上的 /etc/hosts
文件开始,下面是清单:
127.0.0.1 localhost
255.255.255.255 broadcasthost
127.0.0.1 dev.proling.ru
127.0.0.1 dev.vshvetsov.ru
此配置使我可以独立于 DNS 设置访问具有选定域的站点。现在我将展示我的 docker-compose.yml
:
version: '2'
services:
mysql:
build: mysql/
restart: always
volumes:
- mysql_data:/var/lib/mysql
container_name: mysql
vshvetsov:
depends_on:
- mysql
build: vshvetsov/
ports:
- 8000:80
restart: always
container_name: vshvetsov
proling:
depends_on:
- mysql
build: proling/
ports:
- 8003:80
restart: always
container_name: proling
nginx:
depends_on:
- proling
- vshvetsov
image: nginx
restart: always
ports:
- "80:80"
volumes:
- ./nginx/volume:/etc/nginx
container_name: nginx
volumes:
mysql_data:
两个站点容器在 localhost:8000
和 localhost:8003
上都能完美运行,下面是我的 nginx/volume
文件夹树:
volume
├── conf.d
│ ├── default.conf
│ ├── proling.ru
│ └── vshvetsov.ru
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules -> /usr/lib/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf
所以影响我的 NGINX 配置的唯一方法是将 .conf
个文件放在 conf.d
目录中,下面是它们:
dev.proling.ru
server {
listen 80;
server_name www.proling.ru;
rewrite ^ http://proling.ru$request_uri? permanent; #301 redirect
}
server {
listen 80;
server_name proling.ru *.proling.ru;
location / {
proxy_pass http://proling;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
access_log /var/log/nginx/proling.ru-access.log;
}
}
和dev.vshvetsov.ru
server {
listen 80;
server_name www.vshvetsov.ru vshvetsov.ru dev.vshvetsov.ru;
location / {
proxy_pass http://vshvetsov;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
access_log /var/log/nginx/vshvetsov.ru-access.log;
}
}
通过重命名 conf.d
中的 nginx 配置文件解决了问题。它们必须以 .conf
扩展名结尾,因为 nginx.conf
默认具有此字符串:
include /etc/nginx/conf.d/*.conf;
当然,如果方便的话,我可以挂载或复制我们的配置,但我更喜欢默认路径。