我有网络应用程序。
- 公共网络应用程序 (app1)
- api 网络应用 (app2)
我为此应用程序进行 docker 配置。每个应用程序都在其容器中。要从 Web 访问此应用程序,请使用 nginx 配置容器,其中 nginx 代理所有请求。
所以我可以运行 - http://app1.dev/和http://app2.dev/
但我需要从 app1 访问http://app2.dev/(从 app1 容器访问主机 app2.dev)。
Ping(来自 app1 容器):
PING app2.dev (127.0.53.53) 56(84) bytes of data.
64 bytes from 127.0.53.53: icmp_seq=1 ttl=64 time=0.027 ms
64 bytes from 127.0.53.53: icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from 127.0.53.53: icmp_seq=3 ttl=64 time=0.038 ms
我应该配置什么,才能从 app1 容器访问http://app2.dev/主机?
Nginx 代理配置
upstream app1_upstream {
server app1;
}
upstream app1_upstream {
server app2;
}
server {
listen 80;
server_name app1.dev
app2.dev;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
if ($host = "app1.dev") {
proxy_pass http://app1;
}
if ($host = "app2.dev") {
proxy_pass http://app2;
}
}
error_log /var/log/nginx/proxy_error.log;
access_log /var/log/nginx/proxy_access.log;
}
码头工人撰写
version: '2'
services:
proxy:
build: ./proxy/
ports:
- "80:80"
- "443:443"
links:
- app1
- app2
- app1:app1
- app2:app2
hostname: proxy
app1:
build: ./app1/
volumes:
- ../app1/:/var/www/app1
hostname: app1
app2:
build: ./app2/
volumes:
- ../app2/:/var/www/app2
hostname: app2
docker-compose ps
app1 /sbin/my_init Up 80/tcp
app2 /sbin/my_init Up 80/tcp
proxy_1 /sbin/my_init Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp