首页 > 解决方案 > 我正在将 nginx docker 容器设置为 php-fpm 的反向代理。甚至可能吗?

问题描述

我正在研究我的树莓派(arm 架构),我正在使用 docker 在一个非常轻量级的操作系统(HyperiotOS)上运行容器。如果卷在两个容器上都安装了代码,我已经成功设置了 nginx 配置服务 php 文件的 nginx + php-fpm 容器。

现在我的问题是:我试图将 nginx 设置为反向代理,这意味着我不希望 nginx 访问驱动器上的任何代码我想将所有请求转发到 php 容器并显示结果。我尝试了 proxy_pass ,但它没有用。有没有办法做到这一点 ?下面的代码示例。

我的码头工人撰写:

version: '3'
services:
  nginx:
    image: arm32v7/nginx:latest
    container_name: nginx
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./conf.d:/etc/nginx/conf.d
      - ./logs:/var/log/nginx/
      - ./code:/code # this is what I want to get rid off
    networks:
      - webserver
  php:
    image: arm32v7/php:7.3-fpm
    expose:
      - "9000"
    restart: unless-stopped
    container_name: php-fpm
    volumes:
      - ./code:/code
    networks:
      - webserver
networks:
  webserver:
    driver: bridge

我的 nginx 配置:

server {
    listen 80;
    index index.php index.html;
    server_name raspberry.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    location ~ \.php$ {
        try_files $uri /dev/null =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

现在在 /code 文件夹中,我有 index.php 和 php 信息。此配置运行良好,访问 raspberry.local:8080 后,我可以看到 php 信息页面。

但是,为了访问它,我将该 /code 安装在 nginx 容器中。

- ./code:/code # this is what I want to get rid off

现在我想消除它,这样我就可以将 nginx 作为平衡器放在其他地方而无需访问代码。我想将请求直接代理到 php 容器。我尝试了 proxy_pass http://php:9000但这破坏了事情。将 nginx 从代码中“分离”并充当独立代理的最佳方法是什么?如果可能的话,消除这种情况有几个原因,一个是生产类型构建。我无法共享需要烘焙的文件夹。我不希望两个容器携带代码。如果是这种情况,我可以将 apache 放入 php 容器并在其中代理 nginx 并消除 fpm。

标签: phpdockernginxarm

解决方案


推荐阅读