nginx中返回的几个常见错误状态码
|
admin
2025年6月28日 22:18
本文热度 133
|
1. 401,身份证认证未通过
配置身份认证
server {
listen 80;
server_name localhost;
location /home {
# 启用身份认证
auth_basic "closed site";
# 配置认证文件
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
alias /usr/local/nginx/html;
}
}
未登陆,返回401
2. 403,没有访问权限,一般是由代理服务返回的
3. 404,请求的资源不存在
server {
listen 80;
server_name localhost;
location /images/ {
alias /usr/local/nginx/images/;
}
请求/images/2.jpg,对应的images/目录下没有这个文件,返回404
4. 413,请求体大小超出限制
location /upload {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/upload;
}
上传文件超过 client_max_body_size 默认大小1M,返回413
修改 client_max_body_size 值为50M后上传成功
location /upload {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/upload;
client_max_body_size 50M;
}
5. 502,后端服务无响应
代理服务未启动,nginx返回502
6. 504,后端服务响应超时
代理响应超时,返回504
修改代理响应超时时间为120s
location /api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/api/;
proxy_read_timeout 120s;
}
阅读原文:原文链接
该文章在 2025/7/1 23:47:59 编辑过