Search code examples
nginxsymfonyfastcginginx-location

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.


Solution

  • 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.

    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.