
我使用 Docker 和 NGINX 來提供使用 React 構建的 SPA。一切都很好,直到我添加了一些受此要點啟發的快取。目前,NGINX 為靜態檔案夾中的所有檔案回傳 404。
這就是 NGINX 配置的樣子:
server {
listen 80;
location / {
root /usr/share/nginx/html/;
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.html;
}
# These two location directives cause the issue, if remove them it will be running fine
location ~* \.(?:jpg|jpeg|gif|png|ico|svg)$ {
expires 7d;
add_header Cache-Control "public";
}
location ~* \.(?:css|js)$ {
add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
}
}
這是錯誤訊息的樣子,路徑前附加了etc檔案夾,這是無效的:
2022/09/28 09:58:46 [error] 11#11: *4 open() "/etc/nginx/html/static/css/main.45f86988.css" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /static/css/main.45f86988.css HTTP/1.1", host: "localhost:3244", referrer: "http://localhost:3244/"
檔案肯定存盤在容器中,但同樣沒有那個etc檔案夾:
/usr/share/nginx/html/static # find
.
./css
./css/main.45f86988.css
./css/main.45f86988.css.map
./js
./js/496.a53b338e.chunk.js
./js/NotFoundComponent.af4d0ff3.chunk.js
./js/main.adbec619.js
./js/main.adbec619.js.map
./js/285.f49f8f0e.chunk.js
./js/MapComponent.3d89394e.chunk.js
./js/MapComponent.3d89394e.chunk.js.map
./js/NotFoundComponent.af4d0ff3.chunk.js.map
./js/DemoContainer.7bc357cb.chunk.js.map
./js/496.a53b338e.chunk.js.map
./js/main.adbec619.js.LICENSE.txt
./js/285.f49f8f0e.chunk.js.map
./js/DemoContainer.7bc357cb.chunk.js
/usr/share/nginx/html/static #
PS我以前沒有使用過NGINX,如果答案很明顯,我很抱歉
uj5u.com熱心網友回復:
看起來您的檔案在/usr/share/nginx/html/static檔案夾中,但 nginx 正在嘗試從/etc/nginx/html/. 我建議將root /usr/share/nginx/html/線路移動到服務器塊。
像這樣的東西:
server {
listen 80;
root /usr/share/nginx/html/;
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/510492.html
標籤:反应码头工人nginx
