【问题标题】:Docker Swarm - PHP-FPM not working with Nginx running on managerDocker Swarm - PHP-FPM 不适用于在管理器上运行的 Nginx
【发布时间】:2020-12-06 10:33:55
【问题描述】:

我已经构建了一个集群(1 名经理,4 名工人)。只有 manager 有公网 IP,worker 和 manager 在私网。

我尝试使用 Docker Swarm 构建一个网络服务器(Nginx + PHP-FPM)。所以我在manager上设置了Nginx容器,这样就可以从私网外部请求了。如果我这样做,容器在请求 PHP 文件时会收到 upstream timed out (110: Connection timed out) while connecting to upstream 错误。如果我在工作节点上运行它,一切正常,但无法从专用网络外部访问 Nginx(我只能在管理器或每个工作人员上使用 curl 请求)。

你们有什么想法吗?我真的不明白为什么在管理器上运行 Nginx 会导致 PHP-FPM 超时。谢谢!

这里是docker-compose.yml 文件:

version: '3.8'
services: 
  nginx:
    image: arm32v5/nginx:latest
    container_name: webserver_nginx
    ports: 
      - 80:80
    volumes: 
      - /media/storage/webserver/nginx/nginx.conf:/etc/nginx/nginx.conf
      - /media/storage/webserver/nginx/log:/var/log/nginx
      - /media/storage/www:/var/www
    links: 
      - php
    networks:
      - webserver
    deploy:
      placement:
        constraints:
          - "node.role==manager"

  php:
    image: arm32v5/php:7.4-fpm
    container_name: webserver_php
    volumes:
      - /media/storage/www:/var/www
      - /media/storage/webserver/nginx/www.conf:/usr/local/etc/php-fpm.d/www.conf
    networks:
      - webserver
    links:
      - nginx
    ports:
      - 9000:9000

networks:
  webserver:
    driver: overlay
    attachable: true

Nginx 配置:

user                       nginx;
worker_processes           1;

error_log                  /var/log/nginx/error.log warn;
pid                        /var/run/nginx.pid;

events {
    worker_connections     1024;
}

http {
    include                /etc/nginx/mime.types;
    default_type           application/octet-stream;
    
    log_format             main  '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
    access_log             /var/log/nginx/access.log main;
    
    sendfile               on;
    keepalive_timeout      65;
    server_tokens          off;

    server {
        listen 80;
        
        error_page         500 502 503 504 /50x.html;
        
        location / {
            index          index.php index.html index.htm;
            root           /var/www/;
        }

        location ~ \.php$ {
            root           /var/www/;
            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;
        }
    }
}

【问题讨论】:

    标签: php docker nginx docker-swarm


    【解决方案1】:

    我遇到了类似的问题,解决方案是在上游使用docker service name

    这是我的设置

    • 3 EC2 与 Docker Swarm
    • 1 Swarm Manager(已部署 Nginx 代理)
    • 2 个 Swarm Worker(前端 + 后端部署)

    流程
    浏览器 ->> Nginx-proxy ->> React-frontend (Nginx) ->> Go-backend

    nginx.conf

    http {
        
        upstream allbackend {
            server exam105_frontend:8080; #FrontEnd Service Name
        } 
    
        server {
            listen 80;
    
            location / {
    
                  proxy_pass http://allbackend;
            }
        }
    
    }
    
    

    Docker 群服务

    AWS 注意:

    记得在您的 AWS 设置的安全组中公开打开端口 808080(或您使用的任何端口号)进行内部通信,否则您不会能够访问该服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 2017-12-13
      • 1970-01-01
      相关资源
      最近更新 更多