docker-compose 的 Nginx 反向代理不转发请求
Nginx reverse proxy with docker-compose doesn't forward requests
我一直在创建一个微前端项目,但胶水 (nginx) 无法正常工作 expected。
我的项目结构如下:
/app1
Dockerfile
/app2
Dockerfile
/nginx
Dockerfile
nginx.conf
/shell
Dockerfile
docker-compose.local.yaml
项目 Dockerfile 如下所示:
# ...
FROM nginx:alpine
# Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*
COPY --from=builder /app/dist/shell /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]
nginx Dockerfile:
FROM nginx:alpine
COPY ./nginx.conf /etc/nginx/conf.d/nginx.conf
nginx.conf
文件:
upstream shell {
server localhost:3001;
}
upstream app1 {
server localhost:3002;
}
upstream app2 {
server localhost:3003;
}
log_format compact '$request $status - $bytes_sent';
access_log off;
server {
listen 80;
listen [::]:80;
access_log /var/log/nginx/access.log compact;
ssi on;
location = / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://shell;
}
location /app1/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://app1/;
}
location /app2/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://app2/;
}
}
最后 docker-compose.local.yaml
文件:
version: '3.8'
services:
nginx:
image: localhost:5000/nginx:alpine
build: ./nginx
ports:
- "3000:80"
depends_on:
- shell
- app1
- app2
shell:
image: localhost:5000/mf-angular-shell
build: ./shell
ports:
- "3001:80"
app1:
image: localhost:5000/mf-angular-app1
build: ./app1
ports:
- "3002:80"
app2:
image: localhost:5000/mf-angular-app2
build: ./app2
ports:
- "3003:80"
然后通过以下方式启动应用程序:
docker-compose -f docker-compose.local.yaml up -d
启动后,我可以毫无问题地导航到各个应用程序:
shell http://localhost:3001/
但是当我导航到根目录时 http://localhost:3000/ 我只看到“欢迎使用 nginx!”消息。
当导航到 http://localhost:3000/app1/ 等单独的路径时,我在代理的终端中看到以下错误:
"/usr/share/nginx/html/app1" failed (2: No such file or directory)
浏览器出现 404 错误。
是不是我覆盖错文件了? /etc/nginx/conf.d/nginx.conf
还是配置本身的问题?完全是别的东西?
主要问题是 localhost:<port>
无法在容器之间访问。容器应引用 docker-compose.
中定义的服务名称
nginx.conf
变为:
upstream shell {
server shell:3001;
}
upstream app1 {
server app1:3002;
}
upstream app2 {
server app2:3003;
}
...
我一直在创建一个微前端项目,但胶水 (nginx) 无法正常工作 expected。
我的项目结构如下:
/app1
Dockerfile
/app2
Dockerfile
/nginx
Dockerfile
nginx.conf
/shell
Dockerfile
docker-compose.local.yaml
项目 Dockerfile 如下所示:
# ...
FROM nginx:alpine
# Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*
COPY --from=builder /app/dist/shell /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]
nginx Dockerfile:
FROM nginx:alpine
COPY ./nginx.conf /etc/nginx/conf.d/nginx.conf
nginx.conf
文件:
upstream shell {
server localhost:3001;
}
upstream app1 {
server localhost:3002;
}
upstream app2 {
server localhost:3003;
}
log_format compact '$request $status - $bytes_sent';
access_log off;
server {
listen 80;
listen [::]:80;
access_log /var/log/nginx/access.log compact;
ssi on;
location = / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://shell;
}
location /app1/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://app1/;
}
location /app2/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://app2/;
}
}
最后 docker-compose.local.yaml
文件:
version: '3.8'
services:
nginx:
image: localhost:5000/nginx:alpine
build: ./nginx
ports:
- "3000:80"
depends_on:
- shell
- app1
- app2
shell:
image: localhost:5000/mf-angular-shell
build: ./shell
ports:
- "3001:80"
app1:
image: localhost:5000/mf-angular-app1
build: ./app1
ports:
- "3002:80"
app2:
image: localhost:5000/mf-angular-app2
build: ./app2
ports:
- "3003:80"
然后通过以下方式启动应用程序:
docker-compose -f docker-compose.local.yaml up -d
启动后,我可以毫无问题地导航到各个应用程序:
shell http://localhost:3001/
但是当我导航到根目录时 http://localhost:3000/ 我只看到“欢迎使用 nginx!”消息。
当导航到 http://localhost:3000/app1/ 等单独的路径时,我在代理的终端中看到以下错误:
"/usr/share/nginx/html/app1" failed (2: No such file or directory)
浏览器出现 404 错误。
是不是我覆盖错文件了? /etc/nginx/conf.d/nginx.conf
还是配置本身的问题?完全是别的东西?
主要问题是 localhost:<port>
无法在容器之间访问。容器应引用 docker-compose.
nginx.conf
变为:
upstream shell {
server shell:3001;
}
upstream app1 {
server app1:3002;
}
upstream app2 {
server app2:3003;
}
...