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

<div class="separator" style="clear: both;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsl0MvOOoU_0K4zXZEpqOJFcApeRnuApbrPJQEbktPP6uj7u7H9DYQeVAtXoyj980EJgHOaO1_4KWtDtz0ULpf3pJCfE5GacNU7DKL3I93Y9_a8nTW2NEh-z2tdHta7Mv-9yrEtil8oWKkkeZbcxx-fiozKTUA-QBSjgl_j6QpKyy0mS9Ukgro7Ptw/s1600/listing.PNG" style="display: block; padding: 1em 0; text-align: center; "><img alt="nginx autoindex" border="0" data-original-height="281" data-original-width="625" loading="lazy" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsl0MvOOoU_0K4zXZEpqOJFcApeRnuApbrPJQEbktPP6uj7u7H9DYQeVAtXoyj980EJgHOaO1_4KWtDtz0ULpf3pJCfE5GacNU7DKL3I93Y9_a8nTW2NEh-z2tdHta7Mv-9yrEtil8oWKkkeZbcxx-fiozKTUA-QBSjgl_j6QpKyy0mS9Ukgro7Ptw/s1600-rw/listing.PNG"/></a></div>

---

<!-- 목록을 표시할 HTML 컨테이너 -->
<div>
    <h3>Related Links</h3>
    <ul id="label-post-list">
        <!-- 여기에 게시물 목록이 추가됩니다 -->
    </ul>
</div>

---

<!-- 목록을 표시할 HTML 컨테이너 -->
<div>
    <h3>Recommended Link</h3>
    <ul id="label-post-list-include">
        <!-- 여기에 게시물 목록이 추가됩니다 -->
    </ul>
</div>

---



댓글

이 블로그의 인기 게시물

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

대통령 퇴진운동 관련주: 방송·통신·촛불수혜주 완벽 분석

키움 OPEN API MFC 개발 (1)