我想允许Nginx自动索引文件夹,但不提供文件。 在我的应用程序中,文件存储在protected-files文件夹中,并且用户无法访问此文件夹。当用户调用文件(例如:my-website.com/files/my-great-files.pdf)时,实际上,他被重定向到PHP脚本,该脚本验证用户是否有权调用此文件。我知道怎么做,没关系。
但实际上,我想使用一个名为jBrowse的JS插件,这个程序需要访问很多文件,并且需要访问文件夹的索引才能有文件列表。
我不知道怎么做...我们可以在PHP中返回文件索引吗?
或者其他想法,允许Nginx返回包含空文件的文件夹的索引。当用户或插件想要访问该文件时,他会重定向控制权限的PHP脚本。
我记得这个:
location /files {
autoindexon;
}
location ~* \.(doc|pdf)$ {
deny all;
}
没关系,用户可以在文件夹中导航,查看文件,但如果他点击,则会出现403错误。但我无法通过重定向到php来取代拒绝所有...
这是我的default.conf文件:
server {
server_name project.dev;
root /home/docker/web;
location / {
# try to serve file directly, fallback to app.php
#try_files $uri /app.php$is_args$args;
try_files $uri /app_dev.php$is_args$args;
}
location /protected_files {
internal;
alias /home/docker/protected-files;
}
location /files {
autoindex on;
}
location ~* \.(doc|pdf)$ {
try_files $uri /app_dev.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass engine:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass engine:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
#internal;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
答案 0 :(得分:0)
您可以将deny all
替换为rewrite
指令,以执行内部重定向到您的php处理程序。
location /files {
autoindex on;
}
location ~* ^/files.*\.(doc|pdf)$ {
rewrite ^ /app_dev.php last;
}
有关详细信息,请参阅this document。
编辑:您可能希望更正确地使用正则表达式,以便只有以/files
开头并以.doc
或.pdf
结尾的URI才会匹配。更改正则表达式(如上所述)或将其嵌套在location /files
块中,如下所示:
location /files {
autoindex on;
location ~* \.(doc|pdf)$ {
rewrite ^ /app_dev.php last;
}
}