docker+nginx部署spa项目404

关于 nginx 配置 spa 项目刷新非主页路由出现 404 的问题

最近在使用 docker 制作 nginx 镜像部署 React 项目,上测试环境后刷新页面出现 404 问题,反复检查 nginx.conf 配置,最终定位到

include /etc/nginx/conf.d/*.conf;

这段代码出现的问题,这将会把 dockernginx 镜像中的 default.conf 包含进来,导致 404 的问题,删除该行代码即可。另外要注意 spa 需要服务器一直渲染 index.html,因为 spa 的路由解析是浏览器接管的,要有下面的配置:

location / {
    root /usr/share/nginx/html;
    try_files $uri $uri/ /index.html;
}