node.js 带有 SSL 的 npm http-server

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35127383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:51:30  来源:igfitidea点击:

npm http-server with SSL

node.jssslnpmhttpserver

提问by delucasvb

I'm using the npm package "http-server" (https://www.npmjs.com/package/http-server) to set up a simple webserver, but I cannot get it to use SSL. My command in package.json is

我正在使用 npm 包“http-server”(https://www.npmjs.com/package/http-server)来设置一个简单的网络服务器,但我无法让它使用 SSL。我在 package.json 中的命令是

http-server -p 8000 -o -S

with a cert.pem and key.pem in my root directory (for now). The "-o" option opens a browser to the default page, but the page is served using HTTP and not even accessible through HTTPS. I don't get any errors or warnings. I've also tried adding the "-C" and "-K" options without luck. Has any one had any success with this package?

在我的根目录中使用 cert.pem 和 key.pem(目前)。“-o”选项将浏览器打开到默认页面,但该页面使用 HTTP 提供服务,甚至无法通过 HTTPS 访问。我没有收到任何错误或警告。我也试过添加“-C”和“-K”选项但没有运气。有没有人用这个包成功过?

采纳答案by delucasvb

Just for future reference, my problem was solved by updating the package to the latest version in package.json. I copy-pasted an old example file without updating the version numbers.

仅供将来参考,我的问题已通过将包更新到 package.json 中的最新版本来解决。我复制粘贴了一个旧的示例文件而不更新版本号。

回答by slomek

First, make sure that you have key.pemand cert.pemfiles. You can generate them using this command:

首先,确保您拥有key.pemcert.pem文件。您可以使用以下命令生成它们:

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

You will be prompted with a few questions after entering the command. Use 127.0.0.1as value for "Common name" if you want to be able to install the certificate in your OS's root certificate store or browser so that it is trusted.

输入命令后,系统会提示您几个问题。127.0.0.1如果您希望能够在操作系统的根证书存储或浏览器中安装证书以使其受信任,请用作“通用名称”的值。

This generates a cert-key pair and it will be valid for roughly 10 years (3650 days to be exact).

这会生成一个证书密钥对,有效期大约为 10 年(准确地说是 3650 天)。

Then you need to run the server with -Sfor enabling SSL and -Cfor your certificate file:

然后您需要运行服务器以-S启用 SSL 和-C您的证书文件:

$ http-server -S -C cert.pem -o
Starting up http-server, serving ./ through https
Available on:
  https:127.0.0.1:8080
  https:192.168.1.101:8080
  https:192.168.1.104:8080
Hit CTRL-C to stop the server

回答by L. K?rkk?inen

Firefox didn't accept self-signed certs, so a bit more effort was required. First create a CA:

Firefox 不接受自签名证书,因此需要付出更多努力。首先创建一个 CA:

openssl req -batch -new -newkey ec:(openssl ecparam -name prime256v1|psub) -nodes -keyout ca-key.pem -x509 -out ca.pem -days 3650 -subj "/CN=A localhost CA"

Add ca.pem (A localhost CA) to trusted certs of your OS and/or Firefox (other browsers use system CAs). Keep the ca* files in a secure location for future use, so you never have to do this again.

将 ca.pem(本地 CA)添加到您的操作系统和/或 Firefox(其他浏览器使用系统 CA)的可信证书。将 ca* 文件保存在安全位置以备将来使用,这样您就不必再这样做了。

Then, for any site that you are running, and whenever you wish to change settings, create cert.pem and key.pem with:

然后,对于您正在运行的任何站点,无论何时您希望更改设置,请使用以下命令创建 cert.pem 和 key.pem:

openssl req -batch -new -newkey ec:(openssl ecparam -name prime256v1|psub) -nodes -keyout key.pem -subj /CN=localhost | openssl x509 -req -CAkey ca-key.pem -CA ca.pem -CAcreateserial -out cert.pem -days 365 -extfile (echo subjectAltName=DNS:localhost|psub)

The above should work on most systems. If not, you might want to create temporary files ecparam.tmp and ext.tmp. Commands functionally equivalent to the two oneliners:

以上应该适用于大多数系统。如果没有,您可能需要创建临时文件 ecparam.tmp 和 ext.tmp。功能上等同于两个 oneliner 的命令:

# Output Elliptic Curve parameters to a temporary file
openssl ecparam -name prime256v1 -out ecparam.tmp

# Create CA
openssl req -batch -new -newkey ec:ecparam.tmp -nodes -keyout ca-key.pem \
  -x509 -out ca.pem -days 3650 -subj "/CN=A localhost CA"

# Create a CSR for localhost, then sign it by CA
echo subjectAltName=DNS:localhost > ext.tmp
openssl req -batch -new -newkey ec:ecparam.tmp -nodes -keyout key.pem \
  -subj /CN=localhost | openssl x509 -req -CAkey ca-key.pem -CA ca.pem \
  -CAcreateserial -out cert.pem -days 365 -extfile ext.tmp

回答by yummy_raspberry

I installed mkcert:

我安装了mkcert

brew install mkcert
brew install nss # if you use Firefox
mkcert -install

Then, in your project directory:

然后,在您的项目目录中:

mkcert 0.0.0.0 localhost 127.0.0.1 ::1

Finally, I renamed generated files:

最后,我重命名了生成的文件:

  • 0.0.0.0+3-key.pem-> key.pem
  • 0.0.0.0+3.pem-> cert.pem
  • 0.0.0.0+3-key.pem-> key.pem
  • 0.0.0.0+3.pem-> cert.pem

And ran the following command:

并运行以下命令:

http-server -S -C cert.pem -o

Then I got:

然后我得到:

enter image description here

在此处输入图片说明

I referenced this blog: https://qiita.com/walkers/items/b90a97a99bbb27f6550f(written in Japanese)

我参考了这个博客:https: //qiita.com/walkers/items/b90a97a99bbb27f6550f(日文写的)