【问题标题】:Symfony 4 : JS and CSS compiled with Webpack return 404Symfony 4:使用 Webpack 编译的 JS 和 CSS 返回 404
【发布时间】:2018-12-12 13:15:44
【问题描述】:

我正在使用 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 中的解释做了 所以我不明白缺少什么。

谢谢你:)

【问题讨论】:

  • 编辑:我尝试添加:位置~* \.(jpg|jpeg|gif|css|png|js|ico|html|eof|woff|ttf)$ { if (-f $request_filename) { 过期 30 天; access_log 关闭; } }

标签: symfony nginx webpack http-status-code-404 symfony4


【解决方案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;'"

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 2013-09-18
    • 1970-01-01
    • 2020-05-24
    • 2019-06-15
    • 2020-11-27
    • 2019-12-03
    • 1970-01-01
    • 2018-10-27
    相关资源
    最近更新 更多