如何在生产中基于 create-react-app 的项目中指定端口号?

How to specify a port number in a create-react-app based project in production?

我正在部署一个网站,该网站将包含一些使用 create-react-app 构建的 React 项目。每个项目都将在其自己的网页上:例如:mywebsite.com/project1、mywebsite.com/project。我正在 Ubuntu 服务器上设置一个 nginx 反向代理(我知道如何配置),但我不确定如何为我的每个 create-react-app 项目指定端口号,以便它们中的每一个有一个独特的端口。谁能阐明如何做到这一点?谢谢!

PS - 我见过这样的线程 - ,但我不知道如何将此解决方案应用于生产。

你在create-react-app中用npm start命令启动的服务器是webpack dev server。它只意味着在开发时使用,您不应该在生产环境中使用它。

相反,您可以使用简单的静态文件服务器轻松托管 CRA 应用程序,该服务器可以 easily configure with nginx without a reverse proxy or changing the port of dev server. You simply need run npm run build command and deploy content of build folder to the appropriate folder of your static file server. You can read more about this in the documentation of CRA

此外,请确保在构建项目之前 specify the homepage in your package.json,因为您将在 mywebsite.com/project1 这样的相对路径中托管您的应用程序。因为 CRA 假定您的应用程序使用默认设置托管在服务器根目录下。

希望对您有所帮助!