生产中的Docker + Symfony资产404 + Nginx

时间:2018-07-08 10:19:07

标签: php symfony docker nginx docker-compose

我正在使用 Docker 开发一个Web应用程序,以管理代码的工作流程和 Symfony 框架。 在开发阶段,我在docker-compose.yml中使用卷来挂载源代码文件。
切换到生产阶段时,我使用docker-compose-prod.yml,并且在此文件中,我不使用数据量,因为在DockerFile中,我使用COPY指令将源代码放入构建的映像中

问题在于,在生产环境中,我只能访问 .php 文件,但是nginx配置文件相同,因此我不明白自己做错了什么。

虚拟服务器配置(nginx)

server {
server_name symfony.local;
root /var/www/symfony/web/;

error_log  /var/log/nginx/symfony_error.log debug;
access_log /var/log/nginx/symfony_access.log;

location / {
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    rewrite ^(.*)$ /app.php/$1 last;
}

location ~ ^/(app|app_dev|config)\.php(/|$) {
    fastcgi_pass php-upstream;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
}
}

这是我的docker-compose.yml(开发模式)的摘要:

php:
    image: <myimage>
    build:
        context: .
        dockerfile: php7-fpm/Dockerfile
        args:
            TIMEZONE: ${TIMEZONE}
    volumes:
        - ./symfony:/var/www/symfony
        - ./logs/symfony:/var/www/symfony/app/logs
        - ./symfony/var/sessions/dev:/var/www/symfony/app/sessions
    environment:
        SYMFONY_ENV: ${SYMFONY_ENV}
        SYMFONY_DEBUG: ${SYMFONY_DEBUG}
nginx:
    image: <myimage>
    build: nginx
    ports:
        - 80:80
    volumes_from:
        - php
    volumes:
        - ./logs/nginx/:/var/log/nginx
    environment:
        SYMFONY_ENV: ${SYMFONY_ENV}
        SYMFONY_DEBUG: ${SYMFONY_DEBUG}

这是我的docker-compose.yml(产品模式)的摘要:

php:
    image: <myimage>
    env_file: .env-prod
    environment:
        SYMFONY_ENV: ${SYMFONY_ENV}
        SYMFONY_DEBUG: ${SYMFONY_DEBUG}
nginx:
    image: <myimage>
    env_file: .env-prod
    ports:
        - 80:80
    volumes_from:
        - php
    links:
        - php
    volumes:
        - ./logs/nginx/:/var/log/nginx
    environment:
        SYMFONY_ENV: ${SYMFONY_ENV}
        SYMFONY_DEBUG: ${SYMFONY_DEBUG}

这是我的PHP Dockerfile

FROM php:7.2.6-fpm
ARG TIMEZONE
RUN apt-get update && apt-get install -y \
    openssl \
    git \
    unzip

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version
#RUN echo "Setting permission for folders.."
#RUN chown -R www-data:www-data var/cache var/logs var/sessions

# Set timezone
RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone
RUN printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > /usr/local/etc/php/conf.d/tzone.ini
RUN "date"

# Type docker-php-ext-install to see available extensions
RUN docker-php-ext-install pdo pdo_mysql


RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

RUN echo "xdebug.remote_log=\"/tmp/xdebug.log\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_handler=\"dbgp\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_host=\"host.docker.internal\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN echo 'alias sf="php app/console"' >> ~/.bashrc
RUN echo 'alias sf3="php bin/console"' >> ~/.bashrc

RUN echo "Changing user to uid 1000 and www-data"
RUN usermod -u 1000 www-data

RUN echo "copying source code...."
ADD symfony /var/www/symfony/
RUN echo "Showing situation of symfony folder after COPY command.."
RUN ls -l /var/www/symfony/
RUN echo "Setting permission for folders symfony.."
RUN chown -R www-data:www-data /var/www/symfony
RUN chmod -R 777 /var/www/symfony
RUN ls -ld /var/www/symfony/
RUN ls -l /var/www/symfony/
RUN echo "Source code correctly copied"

WORKDIR /var/www/symfony

这是我的Nginx Dockerfile

FROM debian:jessie

RUN apt-get update && apt-get install -y \
    nginx

RUN usermod -u 1000 www-data
COPY nginx.conf /etc/nginx/
COPY symfony.conf /etc/nginx/sites-available/

RUN echo "Showing situation of /etc/nginx/.."
RUN ls -l /etc/nginx/

RUN ln -s /etc/nginx/sites-available/symfony.conf /etc/nginx/sites-enabled/symfony
RUN rm /etc/nginx/sites-enabled/default

RUN echo "upstream php-upstream { server php:9000; }" > /etc/nginx/conf.d/upstream.conf


WORKDIR /var/www/symfony

CMD ["nginx"]

RUN echo "Showing situation at the end of /etc/nginx/.."
RUN ls -l /etc/nginx/

EXPOSE 80
EXPOSE 443

0 个答案:

没有答案