问题
I'm building a project with Symfony4 and VueJs, hosted by nginx server and run with Docker. My templates are OK but css and js files are in 404.
Here's my nginx configuration :
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;
}
I configured my webpack file :
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 and JS files are in directory assets\js\*
and assets\css\*
and when i compile with yarn encore dev
my builded files are in public\build\app.js
and public\build\app.css
In template base.html.twig :
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
And
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
Files are compiled as well but I have error 404 for app.js and app.css. I've made like it's explained in https://symfony.com/doc/current/frontend.html So i don't understand what is missing.
Thank you :)
回答1:
Problem solved, it was in docker-compose.yml : In container nginx, i have forgotten to mount in volume the path to my application, it was only for PHP container
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;'"
来源:https://stackoverflow.com/questions/53743889/symfony-4-js-and-css-compiled-with-webpack-return-404