简体   繁体   English

如何在 pm2 中通过 https 提供 static 文件?

[英]How to serve static file over https in pm2?

we can serve our static files (like a frontend app) over http with a simple command in PM2:我们可以在 PM2 中通过一个简单的命令在 http 上提供我们的 static 文件(如前端应用程序):

pm2 serve <path> <port>

How i can serve static files with SSL using the same command pm2 serve ?我如何使用相同的命令 pm2 serve 为 static 文件和 SSL pm2 serve is it possible?可能吗?

Or any alternatives using PM2?或者使用 PM2 的任何替代方案?

A workaround to serve a static site with HTTPS using PM2 is to add serve as a dependency to your project, then create a npm script to run serve, and have PM2 run that script instead of serving your site directly.使用 PM2 为 static 站点和 HTTPS 提供服务的解决方法是将服务添加项目的依赖项,然后创建一个 npm 脚本来运行服务站点,而不是直接运行该服务脚本。

So, for example:因此,例如:

Add serve to your project:serve添加到您的项目:

npm -i serve

Add a script to your package.json , where 'build' is your build directory, and '8080' is the port you want to serve on:将脚本添加到package.json ,其中“build”是您的构建目录,“8080”是您要服务的端口:

  "scripts": {
    "serve-build": "serve -l 8080 -s build --ssl-cert 'path_to/your_certificate.crt' --ssl-key 'path_to/your_key.key'"
  },

Then instead of calling pm2 serve <path> <port> , you would instead tell PM2 to run npm and point at your script:然后不要调用pm2 serve <path> <port> ,而是告诉 PM2 运行 npm 并指向您的脚本:

pm2 start npm --name your-pm2-process-name -- run serve-build

This will make PM2 run your script, which in turn runs serve which supports https when certificates are supplied.这将使 PM2 运行您的脚本,然后在提供证书时运行支持 https 的serve The process will then run in the same manner as if PM2 were serving the static site itself.然后,该过程将以与 PM2 服务于 static 站点本身相同的方式运行。

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

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