简体   繁体   English

如何使用 HTTPS 在节点服务器中部署 Sveltekit 应用程序?

[英]How to deploy Sveltekit app in node server with HTTPS?

I'm want to deploy my Sveltekit app on HTTPS server.我想在 HTTPS 服务器上部署我的 Sveltekit 应用程序。 I have key files also.我也有关键文件。 This is my svelte.config.js file这是我的 svelte.config.js 文件

import preprocess from 'svelte-preprocess';
import node from '@sveltejs/adapter-node';
import fs from 'fs';
import https from 'https';
/** @type {import('@sveltejs/kit').Config} **/
const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: preprocess(),

    kit: {
        // hydrate the <div id="svelte"> element in src/app.html
        target: '#svelte',
        adapter: node(),
        files: { assets: "static" },
        vite: {
          server: {
            https: {
              key: fs.readFileSync("path\\privkey.pem"),
              cert: fs.readFileSync("path\\cert.pem"),
            },
          },
        }
    }
};

export default config;

where should I keep my key files for reading from the config file?我应该在哪里保存我的密钥文件以从配置文件中读取? I tried a few and got some errors screenshot is attached.我尝试了一些,并附上了一些错误截图。 在此处输入图像描述

在此处输入图像描述

Someone please guide me.有人请指导我。 Thanks in advance.提前致谢。

I'm going to explain a solution to my somewhat related problem here, all according to my best understanding, which is limited.我将在这里解释我的一些相关问题的解决方案,所有这些都根据我的最佳理解,这是有限的。

My solution to set up a trusted Sveltekit dev server (running in a private subnet without DNS) was to configure a Nginx reverse proxy that acts as a trusted HTTPS middle man between the Vite server (running in plain HTTP mode) that is bundled in Sveltekit, and the clients (like my Android phone).我设置受信任的 Sveltekit 开发服务器(在没有 DNS 的私有子网中运行)的解决方案是配置一个 Nginx 反向代理,它充当捆绑在 Sveltekit 中的 Vite 服务器(以纯 HTTP 模式运行)之间的受信任 HTTPS 中间人,以及客户端(比如我的安卓手机)。

I found the most useful guidance from the following resources:我从以下资源中找到了最有用的指导:

The main steps to the solution were:解决方案的主要步骤是:

  • Become a local certificate authority and register the authority in your clients (like in the Chrome browser on the desktop, or in the Credential storage of an Android phone).成为本地证书颁发机构并在您的客户端中注册该颁发机构(例如在桌面上的 Chrome 浏览器中,或在 Android 手机的凭据存储中)。
  • Being a certificate authority, sign a x509 certificate for the IP-address (subjectAltName) of the dev server in the local network.作为证书颁发机构,为本地网络中开发服务器的 IP 地址 (subjectAltName) 签署 x509 证书。
  • Setup a Nginx HTTPS reverse proxy (proxy_pass etc.) to forward traffic to the Vite server (typically running in the port 3000).设置 Nginx HTTPS 反向代理(proxy_pass 等)将流量转发到 Vite 服务器(通常在端口 3000 中运行)。 Assign the created certificate and the key for its use.分配创建的证书和密钥以供其使用。 Also add Websocket support as explained in the setup guide linked above.还添加 Websocket 支持,如上面链接的设置指南中所述。
  • Declare kit.vite.server.hmr.port = <port of the Nginx proxy> in svelte.config.js.在 svelte.config.js 中声明kit.vite.server.hmr.port = <port of the Nginx proxy> This is important so that the Sveltekit middleware (?) does not try to bypass the proxy.这很重要,因此 Sveltekit 中间件 (?) 不会尝试绕过代理。

Relevant snippets from my configuration:我的配置中的相关片段:

openssl genrsa -out myCA.key 2048
openssl req -x509 -new -nodes -key myCA.key -sha256 -days 10000 -out myCA.pem
openssl genrsa -out 192.168.22.88.key 2048
openssl req -new -key 192.168.22.88.key -out 192.168.22.88.csr

>192.168.22.88.ext cat <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
IP.1 = 192.168.22.88
IP.2 = 127.0.0.1
DNS.1 = localhost
DNS.2 = localhost.local
EOF

openssl x509 -req -in 192.168.22.88.csr -CA myCA.pem -CAkey myCA.key -CAcreateserial -out 192.168.22.88.crt -days 10000 -sha256 -extfile 192.168.22.88.ext
openssl dhparam -out dhparam.pem 2048

server {
        listen 2200 http2 ssl default_server;
        listen [::]:2200 http2 ssl default_server;
        ssl_certificate     /etc/nginx/ssl/192.168.22.88.crt;
        ssl_certificate_key /etc/nginx/ssl/192.168.22.88.key;
        ssl_dhparam /etc/nginx/ssl/dhparam.pem;
        index index.html;
        server_name 192.168.22.88;

        ssl on;
        ssl_session_cache  builtin:1000  shared:SSL:10m;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers on;

        location / {

            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Forwarded-Proto $scheme;

            proxy_pass          http://127.0.0.1:3000;
            proxy_read_timeout  90;

            proxy_redirect      http://127.0.0.1:3000 https://192.168.22.88:2200;

            # WebSocket support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
 
       }

}

kit: {
    // hydrate the <div id="svelte"> element in src/app.html
    target: '#svelte',
    adapter: adapter(),
    vite: {
        server: {
            hmr: {
                port: 2200,
            }
        }
    }
}

pnpm run dev

I solved it from @sveltejs/adapter-node/README.md#Custom server我从@sveltejs/adapter-node/README.md#Custom server解决了它

The adapter creates two files in your build directory — index.js and handler.js.适配器在您的构建目录中创建两个文件 - index.js 和 handler.js。 Running index.js — eg node build, if you use the default build directory — will start a server on the configured port.运行 index.js - 例如节点构建,如果您使用默认构建目录 - 将在配置的端口上启动服务器。

Alternatively, you can import the handler.js file, which exports a handler suitable for use with Express, Connect or Polka (or even just the built-in http.createServer) and set up your own server或者,您可以导入 handler.js 文件,该文件导出适用于 Express、Connect 或 Polka(甚至只是内置的 http.createServer)的处理程序并设置您自己的服务器

The svelte.config.js use default no changed, run npm run build generate build folder, and touch server.js in project root like this , node server.js and running svelte.config.js 使用默认不变,运行npm run build generate build 文件夹,然后像这样在项目根目录中触摸 server.js , node server.js并运行

 import {handler} from './build/handler.js'; import express from 'express'; import fs from 'fs'; import http from 'http'; import https from 'https'; const privateKey = fs.readFileSync('./config/ssl/xx.site.key', 'utf8'); const certificate = fs.readFileSync('./config/ssl/xx.site.crt', 'utf8'); const credentials = {key: privateKey, cert: certificate}; const app = express(); const httpServer = http.createServer(app); const httpsServer = https.createServer(credentials, app); const PORT = 80; const SSLPORT = 443; httpServer.listen(PORT, function () { console.log('HTTP Server is running on: http://localhost:%s', PORT); }); httpsServer.listen(SSLPORT, function () { console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT); }); // add a route that lives separately from the SvelteKit app app.get('/healthcheck', (req, res) => { res.end('ok'); }); // let SvelteKit handle everything else, including serving prerendered pages and static assets app.use(handler);

Build the product and then serve it using nginx or lighttpd.构建产品,然后使用 nginx 或 lighttpd 提供它。 don't use the hmr it really slow down the web performance because the module keep checking the file change.不要使用 hmr 它确实会降低 Web 性能,因为模块会不断检查文件更改。 then apply https to lighttpd or your nginx.然后将 https 应用于 lighttpd 或您的 nginx。

if it too much.如果它太多。 use npm run preview.使用 npm 运行预览。

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

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