前言
在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/即可訪問文件
注意:
-
12345 是nginx監聽的端口號;
-
配置中的路由 /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配置靜態資源訪問