Symfony 4:使用Webpack编译的JS和CSS返回404

时间:2018-12-12 13:15:45

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

我正在用Symfony4和VueJs构建一个项目,该项目由nginx服务器托管并与Docker一起运行。我的模板还可以,但是css和js文件在404中。

这是我的Nginx配置:

Main

我配置了我的webpack文件:

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

Css和JS文件位于目录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(); assets\js\*中,当我使用assets\css\*进行编译时,我构建的文件位于yarn encore devpublic\build\app.js中 在模板base.html.twig中:

public\build\app.css

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

文件也被编译,但是app.js和app.css出现错误404。 我已经像在https://symfony.com/doc/current/frontend.html中进行了解释 所以我不明白缺少了什么。

谢谢:)

1 个答案:

答案 0 :(得分: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;'"