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 編輯過