2

There are similar questions in SO but not for this specific problem.

Tools: docker-compose 1.21.2; symfony 3.4; nginx 1.10.3

I have several decoupled containers ("Each container should have only one concern"). Everything works except for getting the assets of the symfony (3.4) application in the browser. The defined routes work, templates are rendered correctly, only assets are the problem. Besides using the same setup but serving in a dev machine locally (without containers) there are no problems.

With volumes it worked but I want to have them at production so as the docs indicate https://docs.docker.com/compose/production/#modify-your-compose-file-for-production a change you may need:

  • Removing any volume bindings for application code, so that code stays inside the container and can’t be changed from outside

Facts:

  • Inside the nginx container logs report 404 accesses but no errors.
  • Inside the php-symfony container the prod logs show lines like request.ERROR: Uncaught .... NotFoundException for "GET /css/main.css" at ...RouterListener.php {"exception": ".../NotFoundHttpException(code: 0) ... ResourceNotFoundException"} []
  • Checking dev in a browser http://<host>:<port>/app_dev.php/css/main.css nginx return 404. (I made dev environment available externally)
  • Checking dev in a browser for the route, example http://<host>:<port>/app_dev.php/css appears the Symfony Exception page No route found for "GET /css" with the same information ResourceNotFoundException > NotFoundHttpException
  • Modifying access permissions to the web folder in the container chmod -R a+rwx .../web does not make a difference.

Relevant section docker-compose.yml

version: "2"
services:
    site:
        ...
        working_dir:
            /var/www/html/site
    site_webserver:
        ...
        ports:
           - <port>:80
        depends_on:
           - site
        links:
           - site

Relevant section nginx.conf

server {
    listen 80;
    server_name "";
    root /var/www/html/site/web;

    location / {
        try_files $uri /app.php$is_args$args;
    }  

    location ~ ^.*/(app_dev|config)\.php(/|$) {
        fastcgi_pass lockate_site:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $document_root;
    }
}
Calamar
  • 1,547
  • 1
  • 13
  • 25

3 Answers3

0

you should have existing file /var/www/html/site/web/css/main.css! Do you have it?

Nginx config should not pass this request /css/main.css to PHP code. It should simply send existing file to browser. I think you have this file, but on another path.

You shold correct nginx configuration for it, or put assets-files in /var/www/html/site/web/ directory

Maxim Antonov
  • 295
  • 3
  • 9
  • Ough! Maybe you forgot to mount assets dir into nginx container ? – Maxim Antonov Jun 08 '18 at 09:51
  • 1
    Yes, the file exists "serving in a dev machine locally (without containers) there are no problems."; Why should asset dir be mounted? (Removing any volume bindings for application code, so that code stays inside the container and can’t be changed from outside https://docs.docker.com/compose/production/#modify-your-compose-file-for-production) – Calamar Jun 08 '18 at 09:55
0

Much more could be said but: the assets were blocked at the the php-fpm (symfony's) container.

In the container's file www.conf (in my case /usr/local/etc/php-fpm.d/www.conf ) check the directive security.limit_extensions and leave it like this

security.limit_extensions = 

Note: I found it commented like this ;security.limit_extensions = .php .php3 .php4 .php5

Calamar
  • 1,547
  • 1
  • 13
  • 25
0

Your main problem is, nginx is searching the asset files in nginx container. After you install your assets using (app/console assets:install) they will copied into your php (symfony) container. You must link the assets them between containers. There is an answer to do that: https://stackoverflow.com/a/44284993/6279901

Mehmet Gökalp
  • 314
  • 3
  • 6