nginx-幾個關於location pattern的問題

來源:互聯網
上載者:User
查了好多資料都沒有搞懂的幾個問題。

第一個問題,如果我的location配置是這樣的:

location /doc {    alias /home/user/doc;}

那我訪問http://localhost/doc/a.html的時候實際上nginx是讀取了/home/usr/doc/a.html,如果我訪問的是http://localhost/docs/a.html甚至是http://localhost/docsioajsfopajowejfasd那nginx實際上會嘗試讀取哪個檔案?

第二個問題,如果我將doc配置成一個伺服器,再反向 Proxy。

server {    listen 8000;    server_name doc;    root /home/user/doc;        index index.html index.htm index.nginx-debian.html;    location / {        # First attempt to serve request as file, then        # as directory, then fall back to displaying a 404.        try_files $uri $uri/ =404;    }}

在主伺服器這樣配置:

server {    listen 80;    ....    location /doc {        proxy_pass http://localhost:8000/;    }    }

這樣配置時訪問http://localhost/doc/,如果index檔案引用了靜態檔案,靜態檔案會變成404,瀏覽器會嘗試擷取http://localhost/js/xxx.js而不是http://localhost/doc/js/xxx.js,如果在pattern後面加上/變成

location /doc/ {            proxy_pass http://localhost:8000/;        }

就沒有問題,但如果是第一個問題中的location配置,瀏覽器會正確尋找http://localhost/doc/js/xxx.js。這搞得我很困惑,結尾加不加/究竟有什麼影響?為什麼alias和proxy_pass會出現不同的結果?

回複內容:

查了好多資料都沒有搞懂的幾個問題。

第一個問題,如果我的location配置是這樣的:

location /doc {    alias /home/user/doc;}

那我訪問http://localhost/doc/a.html的時候實際上nginx是讀取了/home/usr/doc/a.html,如果我訪問的是http://localhost/docs/a.html甚至是http://localhost/docsioajsfopajowejfasd那nginx實際上會嘗試讀取哪個檔案?

第二個問題,如果我將doc配置成一個伺服器,再反向 Proxy。

server {    listen 8000;    server_name doc;    root /home/user/doc;        index index.html index.htm index.nginx-debian.html;    location / {        # First attempt to serve request as file, then        # as directory, then fall back to displaying a 404.        try_files $uri $uri/ =404;    }}

在主伺服器這樣配置:

server {    listen 80;    ....    location /doc {        proxy_pass http://localhost:8000/;    }    }

這樣配置時訪問http://localhost/doc/,如果index檔案引用了靜態檔案,靜態檔案會變成404,瀏覽器會嘗試擷取http://localhost/js/xxx.js而不是http://localhost/doc/js/xxx.js,如果在pattern後面加上/變成

location /doc/ {            proxy_pass http://localhost:8000/;        }

就沒有問題,但如果是第一個問題中的location配置,瀏覽器會正確尋找http://localhost/doc/js/xxx.js。這搞得我很困惑,結尾加不加/究竟有什麼影響?為什麼alias和proxy_pass會出現不同的結果?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.