You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js服务器错误:请求JSON接口遇FetchError及SSL警告求助

解决SSL验证失败导致的FetchError问题

你遇到的FetchError是因为目标网站的SSL证书无法通过Node.js默认验证,rejectUnauthorized: false确实可以绕过这个验证,但要注意这会降低请求安全性,仅建议在开发环境临时使用,生产环境请优先修复证书信任问题。

以下是修改后的代码,通过配置自定义HTTPS Agent关闭SSL验证:

import https from 'https';

export default function Home({ exp }) {
    return (
        <div className="">
            <Head>
                <title>Main Site</title>
                <link rel="icon" href="/favicon.ico" />
            </Head>
            <Header />
            <Banner />
            <main className="max-w-7xl mx-auto px-8 sm:px-16">
                <section className="pt-6">
                    <h2 className="text-4xl font-semibold pb-5">Explore Nearby</h2>
                    
                    {exp?.map((item) => (
                        <h1>{item.location}</h1>
                    ))}

                </section>
            </main>
        </div>
    );
}

export async function getStaticProps() {
    // 创建自定义HTTPS Agent,关闭SSL证书验证
    const agent = new https.Agent({
        rejectUnauthorized: false
    });

    const exp = await fetch(
        'https://jsonkeeper.com/b/4G1G', // 替换为你实际请求的目标链接
        { agent } // 传入自定义Agent
    ).then((res) => res.json());
    
    return {
        props: {
            exp,
        }
    };
}

关键提醒:

  • 开发环境临时用:rejectUnauthorized: false会跳过SSL证书有效性检查,可能让请求暴露在中间人攻击风险下,生产环境绝对禁止使用。
  • 生产环境正确解法:
    • 确认目标网站SSL证书是否有效、未过期、域名配置匹配
    • 若为内部服务,可将其证书添加到Node.js可信证书列表
    • 更换为使用正规可信SSL证书的数据源

内容的提问来源于stack exchange,提问作者AmeriNam20

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 03:10:29