首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >允许索引文件夹,但不提供文件

允许索引文件夹,但不提供文件
EN

Stack Overflow用户
提问于 2016-03-25 10:06:28
回答 1查看 1.9K关注 0票数 2

我希望允许Nginx自动索引文件夹,但不提供文件。在我的应用程序中,这些文件存储在一个受保护的文件文件夹中,用户无法访问这个文件夹。当用户调用一个文件(例如: my-website.com/files/my-great-files.pdf)时,实际上,他被重新定向到一个PHP脚本上,该脚本验证用户是否有权调用该文件。我知道怎么做,没关系。

但是实际上,我想使用一个名为jBrowse的JS插件,这个程序需要访问很多文件,并访问文件夹的索引,以便在其中包含文件列表。

我不知道怎么做..。我们可以在PHP中返回一个文件索引吗?

或者另一个想法,允许Nginx返回包含空文件的文件夹的索引。当用户或插件想要访问文件时,他会重定向到控制权限的PHP脚本上。

我很介意这个:

代码语言:javascript
复制
location /files {
  autoindexon;
}


location ~* \.(doc|pdf)$ {
  deny all;
}

这是好的,用户可以在文件夹中导航,查看文件,但如果他点击,他有一个403错误。但我不能用重定向到php.

这是我的default.conf文件:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-25 11:03:33

可以将deny all替换为rewrite指令,以执行对php处理程序的内部重定向。

代码语言:javascript
复制
location /files {
    autoindex on;
}

location ~* ^/files.*\.(doc|pdf)$ {
    rewrite ^ /app_dev.php last;
}

详情请参见本文件

编辑:您可能希望使regex更加具体,以便只有URI以/files开头,以.doc.pdf匹配结尾。要么更改regex (如上面所示),要么在location /files块中嵌套它,如下所示:

代码语言:javascript
复制
location /files {
    autoindex on;

    location ~* \.(doc|pdf)$ {
        rewrite ^ /app_dev.php last;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36217867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档