php - nginx / symfony route to public/styles folder not working
Get the solution ↓↓↓this my first time I configure nginx for a symfony project(using docker).
Everything was well for the route to my index file. But now I need to use the route to public/styles/*****.css and other like public/js, etc.
I cannot get this running. It always tells me the route is not allowed from symfony or in other tries from nginx not allowed, HTML500.
This is my NGINX configuration:
server {
server_name ~.*;
location / {
root /usr/src/app;
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
client_max_body_size 50m;
fastcgi_pass php:9000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/src/app/symfony/public/index.php;
}
error_log /dev/stderr debug;
access_log /dev/stdout;
}
This is what I try to call:
http://localhost:8000/styles/main.css
http://localhost:8000 -> works proper, loading my index route
This is the part of my base.html.twig
<link href="styles/main.css" rel="stylesheet" type="text/css">
Answer
Solution:
If files is located on /usr/src/app/public/styles/ , then change your root directive value
http://localhost:8000/styles/main.css takes files from "{root}/styles/main.css" according to nginx root directive doc, and your "{root}/styles/main.css" is "/usr/src/app/styles/" now.
UPDATE:
If the backend can translates /style to /public/styles then you need proxy requests to backend (see proxy_pass or search snippets for proxying), else you need move index.php to public and change root, else move /public/styles to /styles in your local file system.
Proxying get more sense since you said that you used separated docker containers for the backend. (It can be done by linking containers and proxying byproxypass http://other-docker-container-name:XXXX
, whereXXXX
is exposed port on the other container)
UPDATE 2:
How I said initially, the question author needed to set another value forroot
and as I said in comment the right value is/usr/src/app/symfony/public
Answer
Solution:
Found a solution, just added those lines before the first location came
location /styles {
root /usr/src/app/symfony/public/;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mark bundle as not supporting multiuse
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.