【发布时间】:2020-09-12 06:43:30
【问题描述】:
每个人。
我在启动 Docker 容器时遇到了一些问题。 最后的任务是:
- 带有 80 和 443 端口的 nginx 作为 localhost:3000 的反向代理(没关系);
- 3000 端口的node.js 是前端(没关系);
- 带有 9000 端口和 domain.con/api url 的 php-fpm(它不起作用);
所以,主要问题在于 nginx.conf:
upstream app {
server app:3000;
}
server {
listen 80;
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;
location / {
try_files $uri @app;
}
location /api {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location @app {
proxy_pass http://app;
}
}
当我尝试访问 localhost 时——没关系,node.js 工作正常; 但是当我尝试访问 localhost/api 时,它给了我一个找不到文件的错误。
docker-compose 看起来像:
version: '3'
services:
api:
container_name: api
build:
context: ./api
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- ./api:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- '9000:9000'
networks:
- app-network
app:
container_name: app
build:
context: ./app
dockerfile: Dockerfile
restart: always
ports:
- '3000'
volumes:
- ./app:/app
networks:
- app-network
nginx:
container_name: nginx
image: nginx:alpine
restart: unless-stopped
volumes:
- ./api:/var/www
- ./nginx/:/etc/nginx/conf.d
ports:
- '80:80'
depends_on:
- app
- api
networks:
- app-network
networks:
app-network:
driver: bridge
【问题讨论】:
-
为什么
nginx和api服务都有- ./api:/var/www卷声明?这是一个错误,或者这背后的意图是什么? -
设置有四个不同的
localhosts;当你说“去 localhost/api”时,你到底在打什么电话,从哪里来的?