首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用本地API时Vite自签名证书错误

调用本地API时Vite自签名证书错误
EN

Stack Overflow用户
提问于 2022-10-11 20:25:17
回答 1查看 263关注 0票数 1

我创建了一个简单的Vue3应用程序,并试图在我的机器上调用另一个本地API (在另一个端口上)。为了更好地复制生产服务器环境,我调用了一个相对的API路径。这意味着我需要在vite服务器上使用一个代理将API请求转发到正确的localhost端口进行本地开发。我在我的vite.config.ts文件中像这样定义了我的vite代理:

代码语言:javascript
运行
复制
import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import basicSsl from '@vitejs/plugin-basic-ssl'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    basicSsl(),
    vue()
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    https: true,
    proxy: {
      '/api': {
        target: 'https://localhost:44326', // The API is running locally via IIS on this port
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '') // The local API has a slightly different path
      }
    }
  }
});

我正在成功地从Vue应用程序调用API,但在运行vite服务器的命令行中得到了这个错误:

代码语言:javascript
运行
复制
5:15:14 PM [vite] http proxy error:
Error: self signed certificate
    at TLSSocket.onConnectSecure (node:_tls_wrap:1530:34) 
    at TLSSocket.emit (node:events:526:28)
    at TLSSocket._finishInit (node:_tls_wrap:944:8)       
    at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:725:12)

我已经尝试过添加基本ssl包了,而且我并不特别想安装另一个NPM软件包,这是在顶部投票的答案。为什么在我试图调用本地机器上的另一个API时,vite服务器会抱怨一个自签名证书?我能做些什么来解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-11 20:33:02

你可以试试secure: false

代码语言:javascript
运行
复制
  server: {
    https: true,
    proxy: {
      '/api': {
        target: 'https://localhost:44326', // The API is running locally via IIS on this port
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/api/, '') // The local API has a slightly different path
      }
    }
  }

完整选项集可在https://github.com/http-party/node-http-proxy#options上使用。

选项 httpProxy.createProxyServer支持以下选项:

  • 要用url模块解析的目标:url字符串
  • 转发:要用url模块解析的url字符串
  • 代理:要传递给http(s).request的对象(参见节点的https代理http代理对象)
  • ssl:要传递给https.createServer()的对象
  • ws:true/false,如果您想代理websockets
  • xfwd:true/false,添加x转发头。
  • 安全:真/假,如果您想验证SSL
  • toProxy:true/false,将绝对URL作为path传递(用于代理代理)
  • prependPath:true/false,默认值: true -指定是否要将目标路径放在代理路径的前面
  • ignorePath:true/ false,Default: false-指定是否要忽略传入请求的代理路径(注意:如果需要,必须追加/手动)。
  • 用于向外连接绑定的localAddress:本地接口字符串
  • changeOrigin:true/ false,默认值:false-将主机标头的来源更改为目标URL
  • preserveHeaderKeyCase:true/ false,默认值:false-指定是否保留响应头键的字母大小写
  • auth:基本身份验证,即计算授权头的“用户:密码”。
  • hostRewrite:在(201/301/302/307/308)重定向上重写位置主机名。
  • autoRewrite:基于请求的主机/端口重写(201/301/302/307/308)上的位置主机/端口。默认值:假。
  • protocolRewrite:在(201/301/302/307/308)上重写位置协议,将其重定向到'http‘或'https’。默认值: null。
  • cookieDomainRewrite:重写set-cookie标头的域。可能的价值:
代码语言:javascript
运行
复制
- `false` (default): disable cookie rewriting
- String: new domain, for example `cookieDomainRewrite: "new.domain"`. To remove the domain, use `cookieDomainRewrite: ""`.
- Object: mapping of domains to new domains, use `"*"` to match all domains. For example keep one domain unchanged, rewrite one domain and remove other domains: cookieDomainRewrite: {   "unchanged.domain": "unchanged.domain",   "old.domain": "new.domain",   "\*": "" }
  • cookiePathRewrite:重写set-cookie头的路径。可能的价值:
代码语言:javascript
运行
复制
- `false` (default): disable cookie rewriting
- String: new path, for example `cookiePathRewrite: "/newPath/"`. To remove the path, use `cookiePathRewrite: ""`. To set path to root use `cookiePathRewrite: "/"`.
- Object: mapping of paths to new paths, use `"*"` to match all paths. For example, to keep one path unchanged, rewrite one path and remove other paths: cookiePathRewrite: {   "/unchanged.path/": "/unchanged.path/",   "/old.path/": "/new.path/",   "\*": "" }
  • headers :向目标请求添加额外标题的对象。
  • 传出代理请求的proxyTimeout:超时(以millis表示)
  • 超时值:传入请求的超时(以millis表示)
  • followRedirects:true/ false,默认值:false-指定是否要遵循重定向
  • selfHandleResponse true/false,如果设置为true,则不调用任何webOutgoing传递,您有责任通过侦听和执行proxyRes事件来适当地返回响应
  • 缓冲区:作为请求体发送的数据流。也许您有一些中间件可以在对请求流进行代理之前使用请求流,例如,如果您将请求的主体读入一个名为“req.rawbody”的字段,您可以在缓冲区选项中重新流该字段: ‘使用严格’;const =要求(‘流-数组’);const HttpProxy =要求(‘http-代理’);const代理=新的HttpProxy();module.exports = (req,res,next) => { proxy.web(req,res,{proxy.web:'http://localhost:4003/',缓冲器: streamify(req.rawBody) },next);};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74033733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档