简体   繁体   中英

nginx / symfony route to public/styles folder not working

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">

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 by proxypass http://other-docker-container-name:XXXX , where XXXX is exposed port on the other container)

UPDATE 2: How I said initially, the question author needed to set another value for root and as I said in comment the right value is /usr/src/app/symfony/public

Found a solution, just added those lines before the first location came

location /styles {
    root /usr/src/app/symfony/public/;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM