搜索
搜索
天涯的知库
显示页面
过去修订
您的足迹:
本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。
====== SSL for Localhost ====== Yes, you can apply a SSL/TLS certificate for localhost. You can generate a self-signed certificate for localhost using the openssl command-line tool. Here's how: Install OpenSSL on your machine if it's not already installed. Open a terminal or command prompt and navigate to the directory where you want to store the certificate files. Generate a private key:<color #ff7f27>''%%openssl genrsa -out localhost.key 2048%%''</color> Generate a certificate signing request (CSR): <color #ff7f27>''%%openssl req -new -key localhost.key -out localhost.csr%%''</color> When prompted for information, you can enter anything you like, but make sure to specify localhost as the Common Name (CN). Generate a self-signed certificate: <color #ff7f27>''%%openssl x509 -req -days 365 -in localhost.csr -signkey localhost.key -out localhost.crt%%''</color> This will generate a self-signed certificate that is valid for one year. Start your Node.js server and configure it to use the certificate and private key: <code | download> const https = require('https'); const fs = require('fs'); const options = { key: fs.readFileSync('localhost.key'), cert: fs.readFileSync('localhost.crt') }; const server = https.createServer(options, (req, res) => { res.writeHead(200); res.end('Hello World!'); }); server.listen(8443, () => { console.log('Server listening on port 8443'); }) </code> You can then access your Node.js server using the URL https://localhost:8443. Note that your browser may warn you about the self-signed certificate, but you can choose to ignore the warning and proceed anyway. Keep in mind that self-signed certificates are not trusted by default, so if you want to use SSL/TLS in a production environment, you will need to obtain a trusted certificate from a certificate authority (CA).
it/node/localhost_ssl.txt
· 最后更改: 2023-04-23 16:25 由
goldentianya
回到顶部