HTTP状态码那些事
当我们浏览网页、调用API或进行网络开发时,总会遇到像404 Not Found、500 Internal Server Error这样的提示。这些3位数字代码就像互联网世界的"摩尔斯电码"。本文将讲述这个知识点。
状态码 | 类别说明 | 常见场景 |
---|---|---|
1xx | 信息响应 | 协议切换、大文件上传确认 |
2xx | 成功操作 | 页面加载成功、API调用成功 |
3xx | 重定向 | 网页跳转、缓存控制 |
4xx | 客户端错误 | 权限不足、请求格式错误 |
5xx | 服务器错误 | 代码异常、服务过载 |
表示请求已被接收,需要继续处理(通常由服务器或代理返回,用户无感知)。
100 Continue
Expect: 100-continue
头 → 服务器返回100
表示允许继续上传。101 Switching Protocols
101
表示协议已切换。102 Processing (WebDAV)
102
,表示处理中。103 Early Hints
103
提示可能需要的资源。请求已被服务器成功接收、理解并处理。
200 OK
201 Created
Location
字段指向新资源URL。201
和新用户URL。202 Accepted
202
表示任务排队中。204 No Content
205 Reset Content
205
让浏览器清空输入框。206 Partial Content
Content-Range
指定返回的数据范围。需要客户端进一步操作(通常是跳转到其他URL)。
300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other
303
,跳转到结果页。304 Not Modified
If-Modified-Since
或If-None-Match
头,服务器比对后返回304
。307 Temporary Redirect
308 Permanent Redirect
客户端请求有误,服务器无法处理。
400 Bad Request
401 Unauthorized
WWW-Authenticate
定义认证方式(如Bearer Token)。403 Forbidden
404 Not Found
405 Method Not Allowed
Allow
列出允许的方法(如GET, POST)。406 Not Acceptable
Accept
指定了不支持的媒体类型。408 Request Timeout
409 Conflict
410 Gone
410
。413 Payload Too Large
414 URI Too Long
415 Unsupported Media Type
Content-Type
不被服务器支持。429 Too Many Requests
Retry-After
提示重试时间。服务器处理请求时发生错误。
500 Internal Server Error
error.log
)。501 Not Implemented
PATCH
方法但服务器未实现。502 Bad Gateway
503 Service Unavailable
Retry-After
提示服务恢复时间。504 Gateway Timeout
505 HTTP Version Not Supported
507 Insufficient Storage (WebDAV)
注意
刚刚巴拉巴拉一大堆文字,可能有点难记忆。如果直接记忆的话,建议直接看下面的
httpHTTP/1.1 206 Partial Content Content-Range: bytes 0-999/4580
状态码 | 缓存行为 | SEO影响 | 典型场景 |
---|---|---|---|
301 | 永久缓存 | 权重转移 | 网站域名永久变更 |
302 | 不缓存 | 不传递权重 | 临时维护跳转 |
graph TD
A[出现404] --> B{资源是否存在?}
B -->|是| C[检查Nginx配置]
B -->|否| D[检查路由逻辑]
C --> E[确认location匹配规则]
D --> F[验证API端点是否存在]
请求 → Nginx → 后端服务 ↓ 502产生原因: 1. 后端进程崩溃(检查pm2日志) 2. 防火墙拦截(确认端口开放) 3. 请求超时(调整proxy_timeout)
bash# 详细追踪请求过程
curl -v https://api.example.com/users
# 仅显示状态码(自动化脚本专用)
curl -s -o /dev/null -w "%{http_code}" https://example.com
# 模拟不同HTTP方法
http PUT https://api.example.com/data name=John
log# Nginx错误日志典型条目 2023/10/20 14:22:33 [error] 668#668: *179 upstream timed out (110: Connection timed out) while reading response header from upstream # 解决方案: 1. 增加proxy_read_timeout值 2. 检查后端服务健康状况 3. 添加负载均衡
json{
"error": {
"code": "AUTH_401",
"message": "需要登录令牌"
}
}
javascript// Axios全局错误处理
axios.interceptors.response.use(
response => response,
error => {
const status = error.response?.status;
switch(status) {
case 401:
redirectToLogin();
break;
case 429:
showRetryNotification();
break;
default:
showErrorToast(error.message);
}
return Promise.reject(error);
}
);
nginx# 自定义错误页面 error_page 404 /custom_404.html; error_page 500 502 503 504 /maintenance.html; # 智能重试配置 proxy_next_upstream error timeout http_500; proxy_connect_timeout 5s;
客户端错误(4xx):检查请求格式、权限、参数。
服务器错误(5xx):排查代码、依赖服务、服务器配置。
重定向(3xx):区分永久(301)和临时(302),避免循环跳转。
工具使用:善用开发者工具和日志快速定位问题。
状态码设计最佳实践【小建议】
准确使用
200
返回错误(如用200 { "error": "..." }
代替4xx
)。安全性
401
或403
,而非404
(避免暴露资源是否存在)。用户体验
404
页面设计友好提示,引导用户返回首页。API设计
201
用于创建,204
用于删除)。本文作者:Dageling003
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!