gpt4 book ai didi

php - docker + nginx + node.js + php-fpm

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:01 27 4
gpt4 key购买 nike

大家。

我在启动 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

最佳答案

我曾多次使用 apache 和 nginx 设置反向代理,但我总是发现这项事件非常耗时(不容易测试和调试)。

自从我开始使用 docker 和 docker-compose 以来,我找到了一种更简单的方法来设置反向代理服务,现在可以将时间花在应用程序上。这个简单的方法是使用 Traefik docker compose 文件中的服务:

version: "3"
services:

reverseproxy: # see https://docs.traefik.io/#the-traefik-quickstart-using-docker
image: traefik:v2.2
command: --providers.docker
ports:
- "8082:80"
volumes:
- /var/run/docker.sock:/var/run/docker.sock

backend:
image: tutum/hello-world
expose:
- 80
labels:
traefik.http.routers.back.rule: Path(`/api`)
traefik.http.routers.back.middlewares: back-stripprefix
traefik.http.middlewares.back-stripprefix.stripprefix.prefixes: /api

frontend:
image: nginx
volumes:
- ./www:/usr/share/nginx/html/:ro
expose:
- 80
labels:
traefik.http.routers.front.rule: Path(`/`)



如您所见,所有反向代理规则都被指定为目标容器上的标签。 Traefik 很好地完成了反向代理工作,正确处理 HTTP/2、Websockets、转发 header ……这非常节省时间。

关于php - docker + nginx + node.js + php-fpm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54968592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com