ubuntu20 配置nginx靜態文件訪問


前言

在ubuntu上配置nginx,通過網頁可以直接訪問ubuntu本地文件,留作參考。

配置

我的nginx配置文件路徑在/etc/nginx/目錄下。

查看/etc/nginx/nginx.conf可以得知,nginx啟動時,會遍歷conf.d目錄下所有的.conf文件,同時也會引入sites-available中的default文件。

可以選擇直接在default中添加server或着在conf.d中新建一個如files.conf的文件再寫入server配置。

假如我的文件路徑為 /home/files/tst/

在files.conf或default中添加:

server{
    listen 12345;
    location /{
        root html;
        index index.html index.htm;
    }
    location /tst/{
        root /home/files/;
        autoindex on; 
    }

}

配置完成后,執行 nginx -s reload 重啟nginx

在瀏覽器輸入 http://xxx.xxx.xxx.xxx:12345/files/即可訪問文件

注意:

  1. 12345 是nginx監聽的端口號;

  2. 配置中的路由 /tst/也會自動拓展到路徑之下。比如服務器ip為123.123.123.123, 當訪問http://123.123.123.123:12345/tst/時,nginx會將/tst/拼接到設置的路徑 /home/files/之下,這樣就會訪問 /home/files/tst/目錄下的文件。如果你的路徑配置成了 /home/files/tst/,那么就會訪問 /home/files/tst/tst/;

參考

[1] nginx配置靜態資源訪問


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM