【问题标题】:How to configure nginx to enable kinda 'file browser' mode?如何配置 nginx 以启用有点“文件浏览器”模式?
【发布时间】:2012-05-26 15:42:53
【问题描述】:

我之前在输入 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/05/19 20:48:33 [错误] 20357#0: *72 open() "/home/yozloy/html/test" 失败(2:没有这样的文件或目录),客户端:125.43。 236.33,服务器:122.97.248.252,请求:“GET /test HTTP/1.1”,主机:“unicom2.markson.hk

我一定是误会了/test的位置,我以为这意味着当我输入http://example.com/test时,它会访问根字典/home/yozloy/html/

【问题讨论】:

  • 测试目录是否存在?
  • @DmitriChubarov 测试目录不存在,我以为/test只表示在url中,所以我可以直接到根目录,我不想做测试字典,我必须误解了,但我怎样才能使它正确呢?
  • 您是否会创建 /home/yozloy/html/test 目录并以某种方式填充它以查看自动索引是否有效?
  • @DmitriChubarov 感谢它在我创建测试文件夹时有效。

标签: nginx


【解决方案1】:

你应该试试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>

编辑:更新列表以删除任何对测试的引用

【讨论】:

  • 看起来很有希望。但我不能让它工作我把相关指令放在这里location /{ root /home/yozloy/html/; index index.html; autoindex on;}
  • 请检查您是否重启了 nginx 并且 nginx 没有使用 --without-http_autoindex_module 编译
  • 我记得我给的唯一标志是--with-mp4_module,这是否意味着默认安装了http_autoindex_module?或者我是否有一些命令来检查我是否安装了它
  • nginx -V 应该为您提供配置选项的列表。请注意,默认情况下启用自动索引。因此,如果 --without-autoindex 不存在,那没关系。
  • 然后请检查错误日志并更新问题。这个讨论太长了。我会把它移到聊天室。
【解决方案2】:

所有答案都包含部分答案。让我尝试将所有内容合二为一。

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

  1. 编辑 nginx 的默认配置:

    sudo vim /etc/nginx/sites-available/default
    
  2. 将以下内容添加到配置部分:

    location /myfolder {  # new url path
       alias /home/username/myfolder/; # directory to list
       autoindex on;
    }
    
  3. 在那里创建文件夹和示例文件:

    mkdir -p /home/username/myfolder/
    ls -la >/home/username/myfolder/mytestfile.txt
    
  4. 重启nginx

    sudo systemctl restart nginx
    
  5. 检查结果:http://&lt;your-server-ip&gt;/myfolder 例如http://192.168.0.10/myfolder/

【讨论】:

  • 如果有人在寻找windows版本的默认配置文件:你必须搜索\nginx-1.xx.y\conf\nginx.conf。如果要将文件夹 C:\Temp 添加为别名目录,则必须使用带有正斜杠的语法:C:/Temp/ - 否则您会在 \logs\error.log 文件中发现类似 invalid number of arguments in "alias" directive 的错误
【解决方案3】:

1。列出所有目录的内容

将自动索引选项设置为on。默认关闭。

你的配置文件(vi /etc/nginx/sites-available/default)应该是这样的

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

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

将自动索引选项设置为on。默认关闭。

您的配置文件 (vi /etc/nginx/sites-available/default)
应该是这样的。
path_of_your_directory 更改为您的目录路径

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

希望对你有帮助..

【讨论】:

  • location /&lt;something&gt; 是访问您要共享的文件的 URL(例如 /data),而不是您希望通过 nginx 共享的目录(例如 ~/videos/funny-cats/)。跨度>
【解决方案4】:

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

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

【讨论】:

    【解决方案5】:

    我试过很多次了。

    最后我只是把autoindex on;放在http之外,但放在server之外,就可以了。

    【讨论】:

      【解决方案6】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-06-12
        • 1970-01-01
        • 2015-02-15
        • 2020-03-28
        • 2022-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多