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
3

2 Answers 2

5

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;
    }

}
2

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.

3
  • 4
    Two years later I come across my own answer when looking up this question again. Thank you, StackExchange. Nov 26, 2018 at 14:41
  • This will show only the contents of the first directory in the user-facing directory listing, right? Jan 14, 2019 at 5:43
  • > nginx: [emerg] the "alias" directive cannot be used inside the named location Jun 13, 2019 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.