如何配置nginx以启用某种“文件浏览器”模式?

avwztpqn  于 2022-11-02  发布在  Nginx
关注(0)|答案(7)|浏览(162)

一旦我以前看到过这种情况,当我输入一个URL http://test.com/test/,而不是给予我一个html页面,它给了我一个'文件浏览器'一样的界面来浏览给定位置的所有文件。
我认为它可能是一个nginx模块,可以在位置上下文中启用。
nginx.conf文件:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  122.97.248.252;
                location /test {
                        root /home/yozloy/html/;
                        autoindex on;
                }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

更新error.log
2012年5月19日20:48:33【错误】20357#0:*72打开()“/home/yozloy/html/test”失败(2:无此类文件或目录),客户端:服务125.43.236.33122.97.248.252请求:“GET /测试HTTP/1.1”,主机:“unicom2.markson.hk
我一定是误解了位置/test的意思,我以为它的意思是当我键入http://example.com/test时,它将访问根字典/home/yozloy/html/

fhg3lkii

fhg3lkii1#

您应该试试ngx_http_autoindex_module
autoindex选项设置为on。默认值为off
您的示例配置应该没问题

location /{ 
   root /home/yozloy/html/; 
   index index.html; 
   autoindex on;
}

如果没有autoindex选项,您应该会在没有index.html档案的目录上,收到以/结尾的要求时,收到错误403。如果有这个选项,您应该会收到一个简单的清单:

<html>
<head><title>Index of /</title></head>
<body bgcolor="white">
<h1>Index of /test/</h1><hr><pre><a href="../">../</a>
<a href="test.txt">test.txt</a>                 19-May-2012 10:43            0
</pre><hr></body>
</html>

**编辑:**更新列表以删除任何测试参考

cwdobuhd

cwdobuhd2#

所有的答案都包含了部分答案。让我试着把所有的答案合二为一。

在新安装的nginx服务器上快速设置“文件浏览器”模式:

1.编辑nginx的默认配置:

sudo vim /etc/nginx/sites-available/default

1.将以下内容添加到配置部分:

location /myfolder {  # new url path
   alias /home/username/myfolder/; # directory to list
   autoindex on;
}

1.在其中创建文件夹和示例文件:

mkdir -p /home/username/myfolder/
ls -la >/home/username/myfolder/mytestfile.txt

1.重新启动nginx

sudo systemctl restart nginx

1.检查结果:http://<your-server-ip>/myfolder,例如http://192.168.0.10/myfolder/

n8ghc7c1

n8ghc7c13#

1.列出所有目录的内容

将autoindex选项设置为on。默认情况下,该选项处于关闭状态。
您的配置文件(vi /etc/nginx/sites-available/default)应该如下所示

location /{ 
   ... ( some other lines )
   autoindex on;
   ... ( some other lines )
}

2.仅列出某些特定目录的内容

将autoindex选项设置为on。默认情况下,该选项处于关闭状态。
您的配置文件(vi /etc/nginx/sites-available/default
应该是这样的。
path_of_your_directory更改为您的目录路径

location /path_of_your_directory{ 
   ... ( some other lines )
   autoindex on;
   ... ( some other lines )
}

希望能帮上忙。

vyswwuz2

vyswwuz24#

您需要创建/home/yozloy/html/test文件夹。或者您可以使用alias,如下所示:

location /test {
    alias /home/yozloy/html/;
    autoindex on;
}
pb3skfrl

pb3skfrl5#

我试过很多次了。
最后我把autoindex on;放在http里面,但是在server的外面,这样就可以了。

xu3bshqb

xu3bshqb6#

只需将此部分添加到服务器,就在location / {之前

location /your/folder/to/browse/ {
        autoindex on;
}
kupeojn6

kupeojn67#

这些解决方案都不起作用!
我有nginx 1.18.0和使用config /etc/nginx/sites-available/default.我的操作系统:分销商ID:Ubuntu说明:Ubuntu 22.04.1 LTS版本:22.04代号:果酱的
以下是我的配置:

server {
  # Example PHP Nginx FPM config file
  listen 80 default_server;
  listen [::]:80 default_server;
  root /var/www/html;

  # Add index.php to setup Nginx, PHP & PHP-FPM config
  index index.php index.html index2.html index.htm index.nginx-debian.html;

  server_name _;

  #location / {
  #  try_files $uri $uri/ =404;
  #}

  autoindex on;
  # pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    # Nginx php-fpm sock config:
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    # Nginx php-cgi config :
    # Nginx PHP fastcgi_pass 127.0.0.1:9000;
  }

  # deny access to Apache .htaccess on Nginx with PHP, 
  # if Apache and Nginx document roots concur
  #location ~ /\.ht {
  #  deny all;
  #}
} # End of PHP FPM Nginx config example

我真的很生气,因为我尝试了一切...但它就是不起作用。我想要的是浏览/家庭/用户/内的一切。当我指的是一切时,我指的是一切!在所有子目录中...

相关问题