鍍金池/ 問答/Linux/ nginx 指定路徑訪問靜態(tài)頁面?

nginx 指定路徑訪問靜態(tài)頁面?

我想讓我的 http://119.28.133.206/git 打開我對應文件夾里面的index.html

我現(xiàn)在通過這樣

server {
    listen       80;        #端口
    server_name  localhost;   #服務(wù)名
    index  index.html;
    charset utf-8; # 避免中文亂碼
    root /data/learnGitBranching;
}

實現(xiàn)了http://119.28.133.206/ 有我需要的展示的界面。
但是我想通過這樣配置

server {
    listen       80;        #端口
    server_name  localhost;   #服務(wù)名
    charset utf-8; # 避免中文亂碼

    
      location /git {
        index  index.html;
        root /data/learnGitBranching;
        autoindex on;             #開啟索引功能
    }
}

訪問http://119.28.133.206/git 顯示404 然后發(fā)現(xiàn)

2018/08/20 17:02:56 [error] 7026#7026: *1 open() "/data/learnGitBranching/git" failed (2: No such file or directory), client: 122.224.133.218, server: localhost, request: "GET /git HTTP/1.1", host: "119.28.133.206"

所以我就把訪問改成http://119.28.133.206/index.html
這下就顯示了nginx 的歡迎界面,我看了log

#access.log
122.224.133.218 - - [20/Aug/2018:17:03:27 +0800] "GET /index.html HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"

然后我就改成這樣子

    location /git {
      #  index  index.html;
        alias /data/learnGitBranching/index.html;
        autoindex on;             #開啟索引功能
    }

這下好了,訪問url/git 直接把我的html 下載下來了。

我還嘗試把alias 改成path 但是不能啟動nginx 請問下正確的方式應該是怎么樣?

我想請問下,有沒有nginx 的入門資料,可以讓我理解 各種location , path ,root ,alias 等組合完成的配置對應的url

回答
編輯回答
怣人

正確的配置:

    location /git/ {
        index  index.html;
        alias  /data/learnGitBranching/;
        autoindex on;
    }

直接看官方文檔就行:
https://nginx.org/r/location
https://nginx.org/r/root
https://nginx.org/r/alias

2018年1月4日 09:39