Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving remote static files with symfony3

I have a problem with my Nginx configuration. I have 2 servers, one with nginx and one with my webApp in symfony3. Here is my configuration :

location /portal/mysite/ {

    set $frontRoot /srv/data/apps/mysite-portal-stag/current/web;
    set $sfApp app.php; # Change to app.php for prod or app_dev.php for dev

    root  /srv/data/apps/mysite-portal-stag/current/web;

    rewrite ^/portal/mysite/(.*)$ /$1 break; 
    try_files $uri @sfFront;

}
location @sfFront {

    root  /srv/data/apps/mysite-portal-stag/current/web;

    fastcgi_pass myserver:myport;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $frontRoot/$sfApp;

    fastcgi_param SCRIPT_NAME /portal/mysite/$sfApp;

}

The webSite work for all the php scripts but all the assets (static files) are broken files. I don't understand enough how Nginx works to indicate what are the static files and "tell" my proxy that they aren't script.

like image 418
Daniel E. Avatar asked Jan 18 '17 13:01

Daniel E.


2 Answers

The try_files directive automatically tries to find static files, and serve them as static, prior to giving up, and letting the request be served as a script.

  • http://nginx.org/r/try_files

    Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made.

Note that although you're already using try_files, it appears that perhaps your path handling isn't up to spec.


As for your own answer with a temporary solution, there's nothing wrong with using a rewrite or two, but that said, it looks like you'd benefit from the alias directive.

  • http://nginx.org/r/alias

    Defines a replacement for the specified location.

However, you've never explained why you're serving stuff out of /tmp. Note that /tmp is often automatically cleared by some cron scripts, e.g., on OpenBSD, the /etc/daily script would automatically find and remove files older than about 7 days (on a daily basis, as the name suggests).


In summary, you should first figure out what is the appropriate mapping between the web view of the filesystem and your filesystem.

Subsequently, if a prefix is found, just use a separate location for the assets, together with alias.

Else, figure out the paths for try_files to work as intended.

like image 194
cnst Avatar answered Oct 26 '22 18:10

cnst


I have find a very ugly solution until anyone find a better solution, here is what I have done :

  • I have copied all the assets repository and copied it to my proxy server where nginx is.

Here is my new config :

location /portal/mysite/ {

    set $frontRoot /srv/data/apps/mysite-portal-stag/current/web;
    set $sfApp app.php; 

    root  /srv/data/apps/mysite-portal-stag/current/web;

    rewrite ^/portal/mysite/(.*)$ /$1 break;
    try_files $uri @sfFront;

}
location /portal/mysite/asset {
    root  /tmp/mysite/asset;
    rewrite ^/portal/mysite/asset/(.*)$ /$1 break;
}
location @sfFront {

    set $frontRootWeb /srv/data/apps/mysite-portal-stag/current/web;
    root  /srv/data/apps/mysite-portal-stag/current/web;

    fastcgi_pass myAdressWeb:myPort;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $frontRoot/$sfApp;

    fastcgi_param SCRIPT_NAME /portal/mysite/$sfApp;

} 

And now it's working, all the js/css and pictures are found.

If anyone think about a "cleaner" answer, he is more than welcome to answer.

like image 35
Daniel E. Avatar answered Oct 26 '22 20:10

Daniel E.