Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

nginx

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 the files in the given location.

I think it maybe a nginx module that could be enable in the location context.

The nginx.conf file:

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

update the error.log

2012/05/19 20:48:33 [error] 20357#0: *72 open() "/home/yozloy/html/test" failed (2: No such file or directory), client: 125.43.236.33, server: 122.97.248.252, request: "GET /test HTTP/1.1", host: "unicom2.markson.hk

I must misunderstand the location /test mean, I thought it meant when I type http://example.com/test, then it would access the root dictionary which is /home/yozloy/html/

like image 936
mko Avatar asked May 19 '12 07:05

mko


People also ask

How enable conf file in nginx?

We can enable a server block's configuration file by creating a symbolic link from the sites-available directory to the sites-enabled directory, which Nginx will read during startup. To do this, enter the following command: sudo ln -s /etc/nginx/sites-available/ example.com /etc/nginx/sites-enabled/

What is Autoindex on nginx?

Nginx autoindex directives autoindex_exact_size; - This directive specifies whether Nginx should display the exact file sizes of the output in the directory index or simply round to the nearest KB, MB, or GB.

How do I use nginx as a file server?

Serving static files using nginx as web server is a good option. For making the static files available you need to copy your testfolder to /usr/share/nginx/html inside the nginx image. After which you will be able to see the files on your browser on port 8080.


2 Answers

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

like image 94
Dmitri Chubarov Avatar answered Sep 20 '22 18:09

Dmitri Chubarov


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

like image 41
0x8BADF00D Avatar answered Sep 19 '22 18:09

0x8BADF00D