[5] Nginx Server Setup - Folder Listing on Web

Before implementing file upload/download functionality, let's first create a local storage directory and make it accessible via the web.


1. Create Storage and Set Permissions

  • Create storage directory
mkdir /root/storage  
cd /root/storage  
vi test.txt  # Add any content, save, and exit  
  • Grant permissions to the directory for the user running NGINX
ps aux | grep nginx  

For example, if the NGINX process runs as "nobody", change the folder's ownership:

[root@vbox build] (master) $ ps aux | grep nginx  
root       52636  0.0  0.0   4544  2412 ?        Ss   01:26   0:00 nginx: master process ./nginx  
root       59772  0.0  0.1  13064  9600 pts/1    T    06:04   0:00 /usr/bin/vim nginx.conf  
nobody     60676  0.0  0.0  13168  5484 ?        S    06:23   0:00 nginx: worker process  
nobody     60677  0.0  0.0  13168  5612 ?        S    06:23   0:00 nginx: worker process  

  • Change permissions and ownership sudo chmod -R 755 /root/storage
    sudo chown -R nobody:nobody /root/storage

  • Modify SELinux security context sudo chcon -R -t httpdsyscontent_t /root/storage

If these steps are not followed, a 403 Forbidden error may occur.


2. Apply NGINX Configuration

  • Using alias, configure the /list endpoint to display the contents of the /root/storage directory.
user nobody;  
worker_processes  10;

events {
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    large_client_header_buffers 4 16k;

    server {
        listen       10099;
        server_name  localhost;

        location /upload.html {
            root /root/nginx/html;
            index upload.html;
        }

        location /list {
            alias /root/storage/;
            autoindex on;                  # Enable directory listing
            autoindex_exact_size off;      # Display file size in a simplified format (optional)
            autoindex_localtime on;        # Display file time in local time (optional)
        }

        location /upload {
            proxy_pass http://localhost:8848;  # Proxy target server
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

After making changes, apply them with "nginx -s reload".


Verifying this setup should display the directory contents correctly.

nginx autoindex



댓글

이 블로그의 인기 게시물

윤석열 계엄령 선포! 방산주 대폭발? 관련주 투자 전략 완벽 분석

한국 핵무장 논의와 방위산업 관련주: 핵무기 개발 과정과 유망 종목 분석

[로스트아크] 제작 효율 최적화 위한 영지 세팅