查了好多資料都沒有搞懂的幾個問題。
第一個問題,如果我的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會出現不同的結果?