Next.js中Axios请求K8s后端返回HTML而非JSON问题排查
Next.js服务端调用K8s后端接口返回HTML而非JSON的排查方案
问题背景
在Next.js的getInitialProps函数中调用Kubernetes容器内的后端接口,已通过Axios配置请求头为application/json,但响应始终返回text/html格式的HTML数据,需排查原因。
现有代码及日志
Axios实例创建代码
import axios from 'axios'; const buildClient = ({ req }) => { if (typeof window === 'undefined') { // 服务端环境 console.log('getting executed in the server') const instance = axios.create({ baseURL: 'http://ingress-nginx-controller.ingress-nginx.svc.cluster.local', headers: { ...req.headers, accept: 'application/json', 'Content-Type': 'application/json' }, }); instance.defaults.headers.get['content-type'] = 'application/json'; return instance; } else { // 浏览器环境 console.log('getting executed in the browser') return axios.create({ baseURL: '/' }) } }; export default buildClient;
Axios实例配置日志
创建实例代码:
const client = buildClient(ctx); console.log(client.defaults.headers);
输出日志:
{ [client] common: { Accept: 'application/json, text/plain, */*' }, [client] delete: {}, [client] get: { 'content-type': 'application/json' }, [client] head: {}, [client] post: { 'Content-Type': undefined }, [client] put: { 'Content-Type': undefined }, [client] patch: { 'Content-Type': undefined }, [client] host: 'ticketing.dev', [client] 'x-request-id': 'e38199c89b2b2584aac5480aed9a745b', [client] 'x-real-ip': '192.168.49.1', [client] 'x-forwarded-for': '192.168.49.1', [client] 'x-forwarded-host': 'ticketing.dev', [client] 'x-forwarded-port': '443', [client] 'x-forwarded-proto': 'https', [client] 'x-forwarded-scheme': 'https', [client] 'x-scheme': 'https', [client] 'sec-ch-ua': '\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"', [client] 'sec-ch-ua-mobile': '?0', [client] 'sec-ch-ua-platform': '\"Linux\"', [client] 'upgrade-insecure-requests': '1', [client] 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36', [client] accept: 'application/json', [client] 'sec-fetch-site': 'none', [client] 'sec-fetch-mode': 'navigate', [client] 'sec-fetch-user': '?1', [client] 'sec-fetch-dest': 'document', [client] 'accept-encoding': 'gzip, deflate, br', [client] 'accept-language': 'en-IN,en;q=0.9', [client] 'Content-Type': 'application/json' [client] }
请求接口及响应日志
请求代码:
const res = await client.get('/api/tickets'); console.log(res.headers);
响应头输出:
AxiosHeaders { [client] server: 'openresty/1.13.6.1', [client] date: 'Thu, 09 Feb 2023 18:44:12 GMT', [client] 'content-type': 'text/html', [client] 'transfer-encoding': 'chunked', [client] connection: 'close' [client] },
调用console.log(res.data)返回大量HTML数据。
排查方向
URL端点匹配问题
响应返回HTML通常意味着请求被路由到静态页面(如404页或首页)而非API接口。检查Ingress配置:- 确认Ingress规则中
path是否为/api/tickets,且service指向正确的后端服务名称和端口。 - 服务端环境用K8s内部域名请求时,需确保Ingress的
host配置与请求头中的host(ticketing.dev)一致,否则Ingress无法匹配正确路由。
- 确认Ingress规则中
请求头有效性问题
- GET请求无请求体,
Content-Type字段对后端路由或响应格式无影响,可移除instance.defaults.headers.get['content-type']配置,避免干扰。 - 确认后端服务是否正确处理
Accept头,当前请求头已设置accept: application/json,若后端未识别该头,会返回默认HTML格式。
- GET请求无请求体,
后端服务状态
- 进入K8s集群内的Pod,用
curl直接请求后端服务的/api/tickets接口,验证返回格式是否为JSON。 - 查看后端服务日志,确认是否收到请求、处理过程是否有异常。
- 进入K8s集群内的Pod,用
Ingress Nginx配置问题
响应头显示请求到达了Ingress Nginx但未正确转发到API,检查:- Ingress是否启用了正确的重写规则,比如
rewrite指令或pathType配置(如Prefix/Exact)。
- Ingress是否启用了正确的重写规则,比如
内容的提问来源于stack exchange,提问作者Vaibhav07
相关产品推荐
相关产品推荐

