php Nginx容器返回502 Bad Gateway

41zrol4v  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(146)

我有以下问题,希望你能帮助我。
我有三个Docker容器,一个是php-fpm,一个是nginx,一个是mysql。当进入我的网站时,浏览器响应502网关错误代码。我不知道我哪里出错了。顺便说一下,值得澄清的是,这个错误代码之间的错误各不相同,但有时它也会返回403 Forbiden。
docker-compose.yml:

version: '3'

services:
   app:
     build:
       context: .
     ports:
       - "9000:9000"
     volumes:
       - ./:/var/www/html
     depends_on:
       -db
     environment:
       - TZ=America/Argentina/Buenos_Aires
     networks:
       - stephvaez

   nginx:
       image: nginx:latest
       ports:
         - "9001:80"
         - "9002:443"
       volumes:
         - ./:/var/www/html
         - ./nginx-conf:/etc/nginx/conf.d
         - ./certificates:/etc/nginx/ssl
       depends_on:
         - app
       networks:
         - stephvaez

   DB:
     image: mysql:5.7
     environment:
       MYSQL_ROOT_PASSWORD: stephvaez
       MYSQL_DATABASE: stephvaez
     volumes:
       - mysql-data:/var/lib/mysql
     networks:
         - stephvaez

volumes:
   mysql-data: {}

networks:
   stephvaez:

Nginx配置文件:

server {
    listen 80;
    listen [::]:80;
        
    server_name storemanager.local;
        
    return 301 https://$host$request_uri;
        
}
    
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    
    ssl_certificate /etc/nginx/ssl/docker/server.pem;
    ssl_certificate_key /etc/nginx/ssl/docker/server.key;
    
    server_name storemanager.local;
    
    root /var/www/html/public;
    index index.html index.php;
    
    
    location / {
        try_files $uri $uri/ /index.php?$is_args$args;
    }
    
    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    
    location ~ /\.ht {
        deny there;
        return 404;
    }
}

Dockerfile:

# Use a base image with PHP-FPM and Nginx
FROM php:8.1-fpm

# Install extensions necessary for Laravel and other dependencies
RUN docker-php-ext-install pdo pdo_mysql

# Install Nginx
RUN apt-get update && apt-get install -y nginx

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

# Copy your Nginx configuration
COPY ./nginx-conf/nginx.conf /etc/nginx/conf.d

# Copy certificates
COPY certificates/ /etc/nginx/ssl/

# Configure Nginx to work with PHP-FPM
RUN echo "upstream php-upstream { server app:9000; }" > /etc/nginx/conf.d/upstream.conf

# Start both Nginx and PHP-FPM
CMD ["nginx", "-g", "daemon off;"]

太感谢你了!!!

gt0wga4j

gt0wga4j1#

Docker Compose堆栈设置为使用nginx服务作为前端,将PHP请求代理到app服务(php-fpm)。
app服务应该只运行php-fpm,而不是NGINX。通过覆盖图像的CMD,您不再运行php-fpm,这就是为什么NGINX无法代理请求。
Dockerfile中删除所有NGINX特定部分。

FROM php:8.1-fpm

# Install extensions necessary for Laravel and other dependencies
RUN docker-php-ext-install pdo pdo_mysql

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

并重建您的app服务映像

docker compose build app

相关问题