我一直尝试在一个位置提供多个目录,以便可以在 /sys/assets/--FILE-- 上访问所有文件,无论文件在以下文件夹中的物理位置如何
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/;
}
}
目标:
/sys/assets/javascriptFile.js
/sys/assets/stylesheetFile.css
/sys/assets/image.png
答案1
您的配置中不能有重复的前缀位置。但是,可以使用try_files
指令来实现您的预期:
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;
}
}
如果你确实需要避免不适当的文件系统查找(即stat()
Linux 下的内核调用),你也可以使用匹配合适文件扩展名的多个正则表达式位置更精确地执行此操作:
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
如果您需要从多个不同的根目录提供文件,请尝试使用命名位置。
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;
}
在第一个上设置自动索引,在最后一个上设置 404(如图所示)。应该可以链接任意多个。您也可以对您的.php
位置执行相同的操作。