Symfony 4:JS 和 CSS 使用 Webpack return 404 编译

Symfony 4 : JS and CSS compiled with Webpack return 404

我正在使用 Symfony4 和 VueJs 构建一个项目,由 nginx 服务器和 运行 以及 Docker 托管。我的模板没问题,但是 css 和 js 文件在 404.

这是我的 nginx 配置:

server {
    listen 80;
    listen [::]:80;
    server_name symfony.local;
    root /var/www/myproject/public;

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

    location ~ ^/(index)\.php(/|$) {
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;

        internal;
    }

    location ~ \.php$ {
        return 404;
    }

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

我配置了我的 webpack 文件:

var Encore = require('@symfony/webpack-encore');
Encore
  .setOutputPath('public/build/')
  .setPublicPath('/build')
  .cleanupOutputBeforeBuild()
  .enableSourceMaps(!Encore.isProduction())
  .addEntry('app', './assets/js/app.js')
  .enableBuildNotifications()
  .enableSassLoader()
  .enableVueLoader();
;

module.exports = Encore.getWebpackConfig();

Css 和 JS 文件在目录 assets\js\*assets\css\* 中,当我用 yarn encore dev 编译时,我构建的文件在 public\build\app.jspublic\build\app.css 在模板 base.html.twig 中:

{% block stylesheets %}
  {{ encore_entry_link_tags('app') }}
 {% endblock %}

{% block javascripts %}
  {{ encore_entry_script_tags('app') }}
{% endblock %}

文件也被编译,但我有 app.js 和 app.css 的错误 404。 我已经像 https://symfony.com/doc/current/frontend.html 中解释的那样 所以我不明白缺少什么。

谢谢:)

问题已解决,它在 docker-compose.yml 中: 在容器 nginx 中,我忘记将我的应用程序的路径挂载到卷中,它仅适用于 PHP 容器

nginx:
    image: nginx:latest
    container_name: dso_nginx
    hostname: nginx
    ports:
        - 80:80
        - 443:443
    depends_on:
        - php
    volumes:
        - ./docker/nginx/default.template:/etc/nginx/conf.d/default.template
        - ".:/var/www/my-symfony-project:ro"
        - ./logs/nginx/:/var/log/nginx
    env_file:
        - .env
    environment:
        - NGINX_HOST=${NGINX_HOST}
    command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"