简体   繁体   English

如何通过 https 在生产中部署 React 应用程序?

[英]How to deploy a react app in production over https?

I have a React App that works beautifully over HTTPS using a.env.production file containing:我有一个使用 a.env.production 文件在 HTTPS 上运行良好的 React 应用程序,该文件包含:

HTTPS=true
SSL_CRT_FILE=/etc/ssl/certs/mycert.crt
SSL_KEY_FILE=/etc/ssl/private/mykey.key

the package.json file contains: package.json 文件包含:

"scripts": {
   "start": "env-cmd -f .env.production react-scripts start",
   "build:production": "env-cmd -f .env.production react-scripts build"
}

when I do:当我做:

npm start

everything works, and I can access my server over HTTPS from anywhere.一切正常,我可以从任何地方通过 HTTPS 访问我的服务器。 Now, this is the funny thing, now I do:现在,这是有趣的事情,现在我做:

npm run build:production

which also works fine, and I get:这也可以正常工作,我得到:

The build folder is ready to be deployed.
You may serve it with a static server:

serve -s build

Now, this is what fails:现在,这是失败的:

1) When I use the serve command above, the environment variables in the.env.production file are not picked up? 1)当我使用上面的serve命令时,.env.production文件中的环境变量没有被拾取? Why?为什么?

2) If I pass the environment variables manually as in: 2)如果我手动传递环境变量,如下所示:

HTTPS=true SSL_CRT_FILE=/etc/ssl/certs/mycert.crt SSL_KEY_FILE=/etc/ssl/private/mykey.key PORT=8580 serve -s build 

Only the PORT variable seems to be picked up, but the server now runs on HTTP.似乎只选择了 PORT 变量,但服务器现在在 HTTP 上运行。 Any ideas why?!任何想法为什么?

When you run npm start , a development server is started for you under the hood that is configured to pick up the SSL_CRT_FILE, SSL_KEY_FILE and HTTPS env vars automatically.当您运行npm start时,会在后台为您启动一个开发服务器,该服务器配置为自动获取 SSL_CRT_FILE、SSL_KEY_FILE 和 HTTPS 环境变量。

serve doesn't do that, you need to let it know with these CLI flags: serve不这样做,您需要使用这些 CLI 标志让它知道:

serve -s build --listen 8580 --ssl-cert "/etc/ssl/certs/mycert.crt" --ssl-key "/etc/ssl/private/mykey.key"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM