首页 > 解决方案 > NextJS 在生产中重写

问题描述

我在开发中发现了对代理到我的后端服务器的重写:

https://nextjs.org/docs/api-reference/next.config.js/rewrites

rewrites: async () => [
...nextI18NextRewrites(localeSubpaths),
{ source: '/api/:path*', destination: 'http://localhost:8080/:path*' },
],

如果 url 不是本地主机,这在生产中如何工作?

对于目的地,我需要添加完整域还是需要单独的开发/生产重写规则?

标签: javascriptproxyurl-rewritingnext.js

解决方案


只需从目标中删除域部分并使用absolute路径。

rewrites: async () => [
  ...nextI18NextRewrites(localeSubpaths),
  { source: '/api/:path*', destination: '/:path*' },
  // ------------------------------------^
];


推荐阅读