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

当我输入一个 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/1920:48:33[ error ]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/

167779 次浏览

You should try ngx_http_autoindex_module.

Set autoindex option to on. It is off by default.

Your example configuration should be ok

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

Without autoindex option you should be getting Error 403 for requests that end with / on directories that do not have an index.html file. With this option you should be getting a simple listing:

<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>

Edit: Updated the listing to delete any references to test

1. List content of all directories

Set autoindex option to on. It is off by default.

Your configuration file ( vi /etc/nginx/sites-available/default ) should be like this

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

2. List content of only some specific directory

Set autoindex option to on. It is off by default.

Your configuration file ( vi /etc/nginx/sites-available/default )
should be like this.
change path_of_your_directory to your directory path

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

Hope it helps..

Just add this section to server, just before the location / {

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

You need create /home/yozloy/html/test folder. Or you can use alias like below show:

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

I've tried many times.

And at last I just put autoindex on; in http but outside of server, and it's OK.

All answers contain part of the answer. Let me try to combine all in one.

Quick setup "file browser" mode on freshly installed nginx server:

  1. Edit default config for nginx:

    sudo vim /etc/nginx/sites-available/default
    
  2. Add following to config section:

    location /myfolder {  # new url path
    alias /home/username/myfolder/; # directory to list
    autoindex on;
    }
    
  3. Create folder and sample file there:

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

    sudo systemctl restart nginx
    
  5. Check result: http://<your-server-ip>/myfolder for example http://192.168.0.10/myfolder/

enter image description here

Any of these solutions does not work !

I have nginx 1.18.0 and using the config /etc/nginx/sites-available/default. My OS: Distributor ID: Ubuntu Description: Ubuntu 22.04.1 LTS Release: 22.04 Codename: jammy

Here is my config:

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

I am really angry cuz i tried everything ... and it just does not work. What i want is to browse everything inside /home/user/. When i mean everything i mean EVERYTHING ! In all subdirs ...