User5518
User5518

Reputation: 43

Nginx in docker-container does not serve static content from Symfony3-app

after wasting nearly the complete sunday on a "little" problem, I want to ask for some help. Maybe I have overseen a little mistake.

I want to develop an application which is based on Symfony 3. In order to have a slim development-environment, I'm using docker with these three containers:

This is the docker/nginx/mysql-configuration:

My 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:

My 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;
}

My Dockerfile:

FROM php:7.1-fpm
MAINTAINER [email protected]

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" of the document-root:

app.php         
apple-touch-icon.png    
config.php      
image.png       
robots.txt
app_dev.php     
bundles         
favicon.ico     
navigation.css

This configuration has the following problem:

When I open "http://localhost:8080/" in my Browser: (nearly) all is fine: PHP is running and the page is displayed (no errors are visible). But what is not working: serving of static files like "navigation.css" through nginx. The the webdeveloper-toolbar from Chrome, in the Network-Tab, I always get the a "404 Not Found"-Message for the "navigation.css".

When I call http://localhost:8080/navigation.css directly in my browser, I get an error from Symfony: "No route found for "GET /navigation.css"".

When I interpret this correctly, this means, that nginx is not able to find "navigation.css" and gives this parameter to Symfony (to the app_dev.php) and the framework is looking for a route named "navigation.css", which is not present.

So my main-question is: Why is nginx not able to see the static file? I mean, the static file and the php-file (which is served) are in the same directory. I do not really get this... Hopefully somebody is able to see my mistake.

Upvotes: 4

Views: 3698

Answers (1)

Sergiu
Sergiu

Reputation: 3185

That's the correct behaviour.

Your Nginx container wouldn't be able to find the files locally. You could either mount just the static files into your Nginx container or you could mount the shared directory between your Symfony and Nginx containers.

To test this, try adding a file /var/www/html/web and see what happens.

Upvotes: 9

Related Questions