2

我正在使用 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.js模板public\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中解释的那样做了 所以我不明白缺少什么。

谢谢 :)

4

1 回答 1

1

问题解决了,它在 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;'"
于 2018-12-17T12:32:13.653 回答