嘿,我现在配置nginx有点愚蠢。
我的目标是获得proxy_pass以下nginx配置为我的2个REST端点:
- http://localhost/app-one/ping→http://app-one: 1111/平
- http://localhost/app-two/ping→http://app-two: 2222/平
意味着如果我从浏览器调用http://localhost/app-one/ping
,它应该被转发到docker环境中的http://app-one:1111/ping
下面是我的docker和docker组合文件的简化表示:
FROM nginx:alpine
COPY ./nginx.conf /etc/nginx/nginx.conf
version: '3.7'
services:
proxy:
image: "proxy"
build: ./nginx-data
ports:
- "80:80"
environment:
- NGINX_PORT=80
app-one:
image: "app-one"
container_name: app-one
app-two:
image: "app-two"
container_name: app-two
,这里是我的nginx配置失败的尝试:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream app-one {
server app-one:1111;
}
upstream app-two {
server app-two:2222;
}
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-Host $server_name;
server {
listen 80;
location /app-one {
proxy_pass http://app-one:1111;
proxy_redirect off;
}
}
server {
listen 80;
location /app-two {
proxy_pass http://app-two:2222;
proxy_redirect off;
}
}
}
有没有人知道如何调整nginx的配置:)无论如何,以上还有什么问题吗?提前感谢您的帮助。
当nginx代理启动时,应用程序容器必须已经在运行。否则nginx配置不工作,所以它没有启动。
为了解决这个问题,我们可以用depends_on来调整docker-compose文件。version: '3.7'
services:
proxy:
image: "proxy"
build: ./nginx-data
ports:
- "80:80"
environment:
- NGINX_PORT=80
depends_on:
app-one:
condition: service_started
app-two:
condition: service_started
app-one:
image: "app-one"
container_name: app-one
app-two:
image: "app-two"
container_name: app-two
执行过程:
docker-compose up
命令将按依赖顺序启动服务。在我们的示例中,app- 1和app- 2在代理服务启动之前启动。
docker-compose stop
命令将按依赖顺序停止服务。在我们的例子中,代理在app- 1和app- 2之前停止。