代码之家  ›  专栏  ›  技术社区  ›  Tom aguilera

Nginx容器返回502坏网关

  •  0
  • Tom aguilera  · 技术社区  · 2 年前

    我有以下问题,希望你能帮助我。

    我有3个docker容器,其中一个包含php-fpm、nginx和mysql。 当进入我的网站时,浏览器会返回502 Gateaway错误代码。 我不明白哪里出了错。顺便说一句,值得澄清的是,这个错误代码之间的错误各不相同,但有时它也会返回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;"]
    

    非常感谢!!!

    1 回复  |  直到 2 年前
        1
  •  1
  •   Phil    2 年前

    Docker Compose堆栈设置为使用 nginx 服务作为前端,将PHP请求代理到 app 服务(php-fpm)。

    这个 应用程序 服务应该 只有 正在运行php-fpm,而不是NGINX。通过覆盖图像的 CMD ,你不再跑步 php-fpm 这就是NGINX无法代理请求的原因。

    从您的 Dockerfile 。。。

    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
    

    重建您的 应用程序 服务形象

    docker compose build app
    
    推荐文章