如何使用 create-react-app 提供 SSL 证书? [英] How can I provide a SSL certificate with create-react-app?

查看:48
本文介绍了如何使用 create-react-app 提供 SSL 证书?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用 Facebook 样板来托管我在本地创建和测试的 React 应用程序.
客户端应用程序与我使用 node.js 制作的 API 交互,并且我没有问题设置安全连接(使用 node.js 客户端发送我的 SSL 证书,以进行测试).
但是,在使用 react 发送我的 SSL 证书而不是自签名证书时遇到了困难,这导致我使用 chrome 遇到此错误并尝试访问 https://example.net:3000 :

您的连接不是私密的 (NET:ERR_CERT_AUTHORITY_INVALID)

Your connection is not private (NET:ERR_CERT_AUTHORITY_INVALID)

文档对我帮助不大:

请注意,服务器将使用自签名证书,因此您的 Web 浏览器几乎肯定会在访问该页面时显示警告.https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#using-https-in-development

Note that the server will use a self-signed certificate, so your web browser will almost definitely display a warning upon accessing the page. https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#using-https-in-development

我怎样才能使用我自己的 SSL 证书(我已经在我域中的另一个应用程序上使用过,并且效果很好)而不是这个自签名证书?我错过了什么吗?

How can I use my own SSL certificate (which I already use on another app on my domain and works like a charm) instead of this self-signed one ? Did I miss something ?

推荐答案

我能够使用修改webpack-dev-server文件,使本地证书正常工作react-scripts 3.4.1(技术上添加在 3.4.0 中,但我有一些——可能不相关的——问题).我将这两个环境变量添加到我的 .env.development 中:

I was able to get a local certificate working without modifying the webpack-dev-server files using react-scripts 3.4.1 (technically added in 3.4.0 but I had some—probably unrelated—issues). I added these two environment variables to my .env.development:

SSL_CRT_FILE=.cert/server.crt
SSL_KEY_FILE=.cert/server.key

注意事项:

  • .cert is a folder I created in the root of my project
  • My script that generates the SSL certificate
  • Official documentation on these two environment variables

这篇关于如何使用 create-react-app 提供 SSL 证书?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆