我想开发一个基于Symfony 3的应用程序。为了拥有一个纤薄的开发环境,我使用了这三个容器的docker:
这是docker / nginx / mysql-configuration:
我的docker-compose.yml:
version: '3.0'
services:
nginx:
image: nginx:latest
depends_on:
- app
ports:
- '8080:80'
volumes:
- ./app/config/docker/nginx.conf:/etc/nginx/conf.d/default.conf
app:
build: .
depends_on:
- mysql
volumes:
- .:/var/www/html
- vendor:/var/www/html/vendor
mysql:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
- ./app/config/docker/mysql_character_set.cnf:/etc/mysql/conf.d/character_set.cnf
restart: always
ports:
- '6603:3306'
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "test"
MYSQL_USER: "test"
MYSQL_PASSWORD: "test"
volumes:
db_data:
vendor:
我的nginx.conf:
server {
listen 80 default_server;
root /var/www/html/web;
index app_dev.php app.php;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app_dev.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
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_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log error;
}
我的Dockerfile:
FROM php:7.1-fpm
MAINTAINER dev@mydomain.com
ENV DEBIAN_FRONTEND=noninteractive
ENV PHP_DEPS="pdo pdo_mysql"
ADD . /var/www/html
# php extensions
RUN docker-php-ext-install $PHP_DEPS
# composer
RUN curl -sS https://getcomposer.org/installer | \
php -- --install-dir=/usr/local/bin --filename=composer && \
composer --version
# copy php-conf
COPY app/config/docker/fpm.conf /etc/php-fpm.d/autotrader.conf
CMD ["php-fpm"]
" LS"文档根目录:
app.php
apple-touch-icon.png
config.php
image.png
robots.txt
app_dev.php
bundles
favicon.ico
navigation.css
此配置存在以下问题:
当我打开" http://localhost:8080/"在我的浏览器中:(几乎)一切都很好:PHP正在运行并显示页面(没有错误可见)。 但是什么是行不通的:提供静态文件,如" navigation.css"通过nginx。来自Chrome的webdeveloper工具栏,在Network-Tab中,我总是得到" 404 Not Found" - " navigation.css"的消息。
当我直接在浏览器中调用http://localhost:8080/navigation.css时,我收到来自Symfony的错误:"找不到" GET /navigation.css""。
当我正确地解释这个时,这意味着,nginx无法找到" navigation.css"并将此参数提供给Symfony(到app_dev.php),框架正在查找名为" navigation.css"的路由,该路由不存在。
所以我的主要问题是:为什么nginx无法看到静态文件?我的意思是,静态文件和php文件(提供服务)位于同一目录中。我真的没有这个......希望有人能够看到我的错误。
答案 0 :(得分:1)
这是正确的行为。
您的Nginx容器无法在本地找到这些文件。您可以只将静态文件挂载到Nginx容器中,也可以在Symfony和Nginx容器之间挂载共享目录。
要测试这一点,请尝试添加文件/ var / www / html / web,看看会发生什么。