8

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.

Daniel E.
  • 2,440
  • 1
  • 14
  • 24
  • Try changing `include fastcgi_params` to `include fastcgi.conf` and remove the `fastcgi_param`s you've set. Instead, add `fastcgi_split_path_info ^(.+\.php)(/.*)$;`. In my opinion, the paths you're passing are probably wrong which is why things work partially. I have `php-fpm` and `nginx` with reverse proxy and it works just fine. – tftd Jan 22 '17 at 18:28
  • @JeanDoux, I hope you find my answer useful and keep your part of the bounty bargain; else, feel free to let me know if anything was unclear, and i'd be happy to connect the leftover dots, if any. – cnst Jan 27 '17 at 17:42

2 Answers2

7

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.

cnst
  • 25,870
  • 6
  • 90
  • 122
1

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.

Daniel E.
  • 2,440
  • 1
  • 14
  • 24