How to configure nginx to enable kinda 'file browser' mode?

前端 未结 6 916
眼角桃花
眼角桃花 2020-12-07 14:48

Once I\'ve seen this before when I type a URL http://test.com/test/, instead of give me a html page, it gives me a \'file browser\' like interface to browse all

相关标签:
6条回答
  • 2020-12-07 14:58

    You should try HttpAutoindexModule.

    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

    0 讨论(0)
  • 2020-12-07 14:59

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

    location /your/folder/to/browse/ {
            autoindex on;
    }
    
    0 讨论(0)
  • 2020-12-07 15:02

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

    location /test {
        alias /home/yozloy/html/;
        autoindex on;
    }
    
    0 讨论(0)
  • 2020-12-07 15:04

    I've tried many times.

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

    0 讨论(0)
  • 2020-12-07 15:11

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

    0 讨论(0)
  • 2020-12-07 15:17

    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/

    0 讨论(0)
提交回复
热议问题