如何将 nextjs 部署到非根目录的目录中

How to deploy nextjs into a directory which is not a root directory

最近我学习了一些关于 nextjs 的知识,因为我想通过使用 React 来制作一个对 SEO 友好的网站。一切看起来都很好,直到我 运行 遇到问题。

这是关于如何将 nextjs 应用程序部署到非根目录的目录,例如“/next”。我对nextjs使用了一个简单的配置,使用默认的节点服务器监听3000端口,然后使用nginx进行反向。

next.config 看起来像:

nginx 配置文件:

当我访问localhost:8080/next时,得到了nextjs提供的404页面,而css或js也是404。看来nextjs或nginx的配置有误。

您当前的配置告诉 nginx 将 url 一对一映射,这意味着 /next/what-ever 将映射到 http://localhost:3000/next/what-ever

要解决这个问题,您需要添加一个额外的斜杠。

server {
  ...
  location /next/ {
    proxy_pass http://localhost:3000/ // <----  this last slash tells to drop the prefix
  }
}

有关详细信息,请阅读 https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/#passing-a-request-to-a-proxied-server