http-server在本地启动https服务以及配置域名
<p>网上搜了一遍后,才发现,看官方文档才是最直接最准确最快速的做法。</p><p>1、安装http-server</p>
<div class="cnblogs_code">
<pre>npm install --global http-server</pre>
</div>
<p>2、生成证书文件,有两个。一个是cert.pem, 一个是key.pem </p>
<div class="cnblogs_code">
<pre>openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem</pre>
</div>
<p>这里成功的文件可以修改地址,最后在启动的时候 -C 后面跟cert的路径,-K 后面跟key的路径,注意填绝对路径,如/usr/local/etc/nginx/ssl/cert.pem。路径在启动服务的时候对应上就行。默认是生成的当前目录的。</p>
<p> 3、启动服务。启动服务的时候,我的是前端项目,所以会进入到dist目录,里面有index.html的文件夹,运行命令。</p>
<div class="cnblogs_code">
<pre>http-server -S -C cert.pem</pre>
</div>
<p>注意大小写,这里指定目录的时候是大写;用上面代码执行的时候你的两个证书文件都要放在当前目录下,否则的话就要写明证书文件的路径。</p>
<div class="cnblogs_code">
<pre># 这里是不写 —C 或者 —K 的时候的默认值:<br>-C or --cert ssl cert 文件路径 (default: cert.pem)
-K or --key Path to ssl key file (default: key.pem)</pre>
</div>
<p> 看到下面的效果就是启动成功了。</p>
<div class="cnblogs_code">
<pre>Starting up http-server, serving ./ through https
Available on:
https://127.0.0.1:8080
https://10.0.51.99:8080
Hit CTRL-C to stop the server</pre>
</div>
<p>4、配置域名。这一步其实和http-server没有关系,因为我用到也提一下。在某些设定下可以解决后端请求有域名要求,域名限制的问题。</p>
<p>配置其实很简单,就是利用host,以为http-server都是在 127.0.0.1 上启动服务的,我们利用host转发过去就好了。</p>
<div class="cnblogs_code">
<pre>127.0.0.1 test.jd.com</pre>
</div>
<p>配置上述host就可以在浏览器实现 https://test.jd.com:8080 来访问了。</p>
<p> </p>
<p>注意:在最新的谷歌浏览器中,会出现拦截并无法访问的情况。应该是谷歌浏览器安全又升级了,这个换成火狐浏览器,选择高级,接受风险并继续就可以了。根据经验,谷歌浏览器通过忽略安全的方式启动应该也可以,有时间查一下补上吧。</p>
<p><img src="https://img2022.cnblogs.com/blog/1091124/202207/1091124-20220711215152439-2142282781.png" alt="" loading="lazy"><img src="https://img2022.cnblogs.com/blog/1091124/202207/1091124-20220711215443246-174057691.png" width="567" height="361" loading="lazy" style="vertical-align: top; margin-left: 10px; margin-right: 10px"> </p>
<p> </p>
<p>http-server 参数说明:</p>
<div class="cnblogs_code">
<pre>-p 端口号 (默认 8080)
-a IP 地址 (默认 0.0.0.0)
-d 显示目录列表 (默认 'True')
-i 显示 autoIndex (默认 'True')
-e or --ext 如果没有提供默认的文件扩展名(默认 'html')
-s or --silent 禁止日志信息输出
--cors 启用 CORS via the Access-Control-Allow-Origin header
-o 在开始服务后打开浏览器
-c 为 cache-control max-age header 设置Cache time(秒) , e.g. -c10 for 10 seconds (defaults to '3600'). 禁用 caching, 则使用 -c-1.
-U 或 --utc 使用UTC time 格式化log消息
-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
-S or --ssl 启用 https
-C or --cert ssl cert 文件路径 (default: cert.pem)
-K or --key Path to ssl key file (default: key.pem).
-r or --robots Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')</pre>
</div>
<p> </p>
<p> </p>
<p>附上原文(https://www.npmjs.com/package/http-server)。</p>
<h2>TLS/SSL</h2>
<p>First, you need to make sure that openssl is installed correctly, and you have <code>key.pem</code> and <code>cert.pem</code> files. You can generate them using this command:</p>
<div class="highlight highlight-source-shell">
<pre>openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem</pre>
</div>
<p>You will be prompted with a few questions after entering the command. Use <code>127.0.0.1</code> as value for <code>Common name</code> if you want to be able to install the certificate in your OS's root certificate store or browser so that it is trusted.</p>
<p>This generates a cert-key pair and it will be valid for 3650 days (about 10 years).</p>
<p>Then you need to run the server with <code>-S</code> for enabling SSL and <code>-C</code> for your certificate file.</p>
<div class="highlight highlight-source-shell">
<pre>http-server -S -C cert.pem</pre>
</div>
<p>If you wish to use a passphrase with your private key you can include one in the openssl command via the -passout parameter (using password of foobar)</p>
<p>e.g. <code>openssl req -newkey rsa:2048 -passout pass:foobar -keyout key.pem -x509 -days 365 -out cert.pem</code></p>
<p>For security reasons, the passphrase will only be read from the <code>NODE_HTTP_SERVER_SSL_PASSPHRASE</code> environment variable.</p>
<p>This is what should be output if successful:</p>
<div class="highlight highlight-source-shell">
<pre>Starting up http-server, serving ./ through https
http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: <span class="pl-c1">false
Serve Brotli Files: <span class="pl-c1">false
Default File Extension: none
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</span></span></pre>
</div>
</div>
<div id="MySignature" role="contentinfo">
<p style="margin-bottom: 20px">还有分享一个技术群,<span style="color: red">474471759</span>,跟随里面的大佬一起成长,进群之后里面的JimY就是我。</p>
<div class="text text-shadow">如果我的博客解决了你的问题,那请你给个关注吧!</div>
<img style="width: 55%" src="https://img.alicdn.com/tfs/TB1OqN7e5_1gK0jSZFqXXcpaXXa-750-400.jpg"><br><br>
来源:https://www.cnblogs.com/DreamSeeker/p/16468040.html
頁:
[1]