nextJS + ExpressJS 与 Apache 反向代理在 url 中产生 "undefined"

nextJS + ExpressJS with Apache reverse proxy results in "undefined" in url

我正在尝试在 Apache 上设置 nextJS + ExpressJS 站点。我有一个反向代理,它将任何请求转发到 http://1.23.45.67(我在端口 80 的 ip)到 localhost:3000,我的 nodejs 服务器是 运行。当我在浏览器中打开网站时,一切看起来都很好。 Images/css 加载正确,我可以在网站上导航,但每当向后端发出 ajax 请求(例如 api 调用以注册用户)时,它会根据请求 [=31] 执行此操作=] http://1.23.45.67/undefined/api/v1/users/register.

我的虚拟主机 apache 配置看起来像

<VirtualHost *:80>
    ServerName www.myservername.com
    ServerAlias myservername.com
    DocumentRoot /var/www/myapp/html
    ErrorLog /var/www/myapp/log/error.log
    CustomLog /var/www/myapp/log/requests.log combined

    ProxyRequests on
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>

我正在使用 .env 文件中的基础 url 变量生成我的 ajax 请求 url。 我也试过 http://localhost/http://localhost:3000/ 作为 BASE_URL

# simplified .env file
...
BASE_URL=http://1.23.45.67/
...
# simplified register page where we make the ajax request.
...
fetch(`${process.env.BASE_URL}/api/v1/users/register`, {method: 'GET'});
...

对于偶然发现此问题的任何人...这不是 apache 配置的问题。问题是我如何在 nextjs 中使用 dotenv。在我当前的项目中,nextjs 在构建时而不是运行时获取 .env 变量。当我的 github 操作编译并将我的代码推送到服务器时,.env 不存在,所以我在 url 中得到 "undefined"。我相信 NextJS 的 publicRuntimeConfig 是解决方案。如果您希望在运行时而不是构建时获取它们,则需要将前端使用的所有 .env 变量添加为 publicRuntimeConfig 变量。 https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration