【问题标题】:502 Bad Gateway, cURL request to API Platform, Symfony502 Bad Gateway,对 API 平台的 cURL 请求,Symfony
【发布时间】:2021-10-11 13:56:02
【问题描述】:

我正在发出cURL 请求

curl -X 'POST' \
'http://localhost:8013/api/superheros' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
 "name": "Clark Kent",
 "slug": "supermman",
 "featured": true,
 "createdAt": "2021-10-11T11:48:22.366Z"
}'

到我上面本地的 API 端点。我收到 502 bad gateway 错误。

我将 Docker 用于所有意图和目的。

docker-compose.yml

version: '3'
services:

  #PHP Service
  app1:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app1
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app1
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  #Nginx Service
  webserver1:
    image: nginx:alpine
    container_name: webserver1
    restart: unless-stopped
    tty: true
    ports:
      - "8013:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db1:
    image: mysql:5.7.22
    container_name: db1
    restart: unless-stopped
    tty: true
    ports:
      - "3318:3306"
    environment:
      MYSQL_DATABASE: rest_api
      MYSQL_ROOT_PASSWORD: password
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

#Volumes
volumes:
  dbdata:
    driver: local

Dockerfile

FROM php:7.4-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    libzip-dev \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

app.conf

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app1: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 / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

我读到有人说PHP-FPM 没有运行,但我的Dockerfile 显然使用它。你能提供一些线索吗?

【问题讨论】:

    标签: php docker symfony docker-compose


    【解决方案1】:

    您可以尝试将参数传递给 Dockerfile 中的php-fpm,如下所示,并确保在local.ini 中配置了9000 端口

    CMD ["php-fpm", "-F", "-y", "/usr/local/etc/php/conf.d/local.ini"]
    
    • -F force php-fpm 在前台运行
    • -y指定配置路径

    Dockerfile修改后,需要运行docker-compose build重建docker镜像

    【讨论】:

    • 同样的事情,遵循docker-compose down/up - 网关错误
    • 也许你可以在 php-fpm 容器中运行netstat -an 命令来检查 php-fpm 是否在 9000 端口运行,
    • 请看我的编辑。我在您建议的命令中没有看到图形9000
    • 需要在php/conf.d/loca.ini中配置9000端口,修改Dockerfile后务必重建docker镜像
    • 请问有配置9000端口的语法吗?
    猜你喜欢
    • 2014-03-05
    • 2013-08-04
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2021-07-01
    • 2019-08-24
    • 2019-03-11
    • 2019-05-25
    相关资源
    最近更新 更多