3

I have been trying serve multiple directories in one location so that all files can be access on /sys/assets/--FILE-- regardless of where is the file physically locate in the following folder

location /sys {
    alias /var/www/website_api/sys/public;
    try_files $uri $uri/ /sys/index.php$is_args$args;
    location ~ \.php {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    include /etc/nginx/fastcgi_params;
    }
        location /sys/assets {
           alias /var/www/website_api/sys/app/assets/javascripts/;
        }
        location /sys/assets {
           alias /var/www/website_api/sys/app/assets/stylesheets/;
        }
        location /sys/assets {
           alias /var/www/website_api/sys/app/assets/images/;
        }
}

Goal:

/sys/assets/javascriptFile.js
/sys/assets/stylesheetFile.css
/sys/assets/image.png
johndavedecano
  • 133
  • 1
  • 1
  • 3

2 Answers2

6

You can't have duplicate prefixed locations in your configuration. However, that's possible to do what you expect using try_files directive :

location /sys {

    alias /var/www/website_api/sys/public;
    try_files $uri $uri/ /sys/index.php$is_args$args;

    location ~ \.php {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
    }

    location ~ ^/sys/assets/?(.*)$ {
        root /var/www/website_api/sys/app/assets;
        try_files /javascripts/$1 /stylesheets/$1 /assets/images/$1;
    }

}

If you absolutely need to spare inappropriate filesystem lookups (i.e. kernel stat() calls under Linux), you can also do it more precisely using multiple regex locations matching suitable file extensions :

location /sys {

    alias /var/www/website_api/sys/public;
    try_files $uri $uri/ /sys/index.php$is_args$args;

    location ~ \.php {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
    }

    location ~ ^/sys/assets/?.*\.css$ {
        rewrite '^/sys/assets/(.*)$' '/$1' break;
        root /var/www/website_api/sys/app/assets/stylesheets;
    }

    location ~ ^/sys/assets/?.*\.(gif|ico|jpe?g|png|svg)$ {
        rewrite '^/sys/assets/(.*)$' '/$1' break;
        root /var/www/website_api/sys/app/assets/images;
    }

    location ~ ^/sys/assets/?.*\.js$ {
        rewrite '^/sys/assets/(.*)$' '/$1' break;
        root /var/www/website_api/sys/app/assets/javascripts;
    }

}
Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50
3

If you need to serve files from several different roots, try using named locations.

    location / {
        root "E:\Directory1";
        try_files $uri $uri/ @dir2;
        index  index.php;
        autoindex on;
    }
    location @dir2 {
        root "F:\Directory2";
        index  index.php;
        try_files $uri $uri/ =404;
    }

Set autoindex on the first one and 404 on the last one (as shown). Should be able to chain as many as you wish. You can also do the same thing for your .php location.

Arlen Beiler
  • 177
  • 9