關於 nginx 配置 spa 項目刷新非主頁路由出現 404 的問題
最近在使用 docker 制作 nginx 鏡像部署 React 項目,上測試環境后刷新頁面出現 404 問題,反復檢查 nginx.conf 配置,最終定位到
include /etc/nginx/conf.d/*.conf;
這段代碼出現的問題,這將會把 docker 的 nginx 鏡像中的 default.conf 包含進來,導致 404 的問題,刪除該行代碼即可。另外要注意 spa 需要服務器一直渲染 index.html,因為 spa 的路由解析是瀏覽器接管的,要有下面的配置:
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
