Docker nginx 502坏网关- laravel项目

nxagd54h  于 2023-01-16  发布在  Nginx
关注(0)|答案(1)|浏览(177)

希望你一切顺利
我一直在努力解决我所面临的问题,一切看起来都很好,但仍然得到错误。我刚刚停靠我的项目和mysql,phpmyadmin,redis ...他们都工作得很好,但我不能在浏览器上打开我的项目,因为它给的错误
docker-comopse.yml

version: '2.4'
services:

    #PHP Service
    app:
        build:
            context: .
            dockerfile: Dockerfile
        image: ${APP_NAME}_app
        container_name: ${APP_NAME}_app
        restart: unless-stopped
        tty: true
        environment:
            SERVICE_NAME: ${APP_NAME}
            SERVICE_TAGS: ${APP_ENV}
            CONTAINER_ROLE: app
        working_dir: /var/www
        volumes:
            - ./:/var/www
            - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
        networks:
            - app-network

    #Nginx Service
    webserver:
        image: nginx:1.22
        #        platform: linux/arm64/v8
        container_name: ${APP_NAME}_webserver
        restart: unless-stopped
        tty: true
        ports:
            - "8080:80"
            - "443:443"
        volumes:
            - ./:/var/www
            - ./nginx/conf.d/:/etc/nginx/conf.d/
        networks:
            - app-network

    #MySQL Service
    db:
        image: mysql:latest
        #        platform: linux/arm64/v8
        container_name: ${APP_NAME}_db
        restart: unless-stopped
        tty: true
        ports:
            - "3307:3306"
        environment:
            MYSQL_DATABASE: ${DB_DATABASE}
            MYSQL_USER: ${DB_USERNAME}
            MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
            SERVICE_TAGS: ${APP_ENV}
            SERVICE_NAME: ${DB_DATABASE}
        volumes:
            - dbdata:/var/lib/mysql/
            - ./mysql/my.cnf:/etc/mysql/my.cnf
        networks:
            - app-network

    #Phpmyadmin Service
    phpmyadmin:
        image: phpmyadmin:latest
        #        platform: linux/arm64/v8
        container_name: ${APP_NAME}_phpmyadmin
        restart: always
        links:
            - db
        depends_on:
            - db
        ports:
            - "8081:80"
        environment:
            - PMA_ARBITRARY=1
            - PMA_HOST=${APP_NAME}_db
            - PMA_PORT=3306
            - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
        networks:
            - app-network

    #Redis Service
    redis:
        image: redis:latest
        #        platform: linux/arm64/v8
        container_name: ${APP_NAME}_redis
        command: redis-server --appendonly yes --requirepass "${REDIS_PASSWORD}"
        ports:
            - "6380:6379"

    #Queue Service
    queue:
        build:
            context: .
            args:
                - APP_ENV=${APP_ENV}
        environment:
            - APP_ENV=${APP_ENV}
            - CONTAINER_ROLE=queue
        container_name: ${APP_NAME}_queue
        working_dir: /var/www
        volumes:
            - ./:/var/www

#Docker Networks
networks:
    app-network:
        driver: bridge
#Volumes
volumes:
    dbdata:
        driver: local

nginx配置文件在项目中

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 app: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;
    }
}

停靠文件

FROM php:8.1-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 \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    libzip-dev \
    libonig-dev

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

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring 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

ENTRYPOINT [ "./entrypoint.sh" ]

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

日志显示了什么

2022/10/20 20:27:46 [error] 22#22: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.112.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://192.168.112.2:9000", host: "localhost:8080"
192.168.112.1 - - [20/Oct/2022:20:27:46 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
192.168.112.1 - - [20/Oct/2022:20:27:46 +0000] "GET /favicon.ico HTTP/1.1" 304 0 "http://localhost:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"

先谢了

lmvvr0a8

lmvvr0a81#

您必须在nginx配置中将网络名称从app更改为app-network,并且在PHP服务中必须以这种方式发布端口9000。

ports:
     - "9000:9000"

希望这能解决你的问题。对我很有效。

相关问题