在web开发过程中,通过HTTP错误码快速定位问题是一个非常重要的技能,同时Nginx是非常常用的一个实现HTTP协议的服务,因此本文结合二者谈谈Nginx对HTTP错误码的处理。
在RFC2616对HTTP协议做了定义,其对错误码定义分为5大类,依次分为100-199、200-299、300-399、400-499、500-599。
RFC定义: Informational - Request received, continuing process,即用于响应客户端的某些动作。
RFC定义:Success - The action was successfully received,understood, and accepted,即表示请求成功。
RFC定义:Redirection - Further action must be taken in order to complete the request,即用于重定向的。
其中301与302是最常用到的。
RFC定义:Client Error - The request contains bad syntax or cannot be fulfilled,即表示客户端请求请求包含错误语法或无法履行。
其中较为常见的是400、401、402、403、404、405。
RFC定义:Server Error - The server failed to fulfill an apparently valid request,即表示服务端无法完成客户端请求。
常见的有500、502、503、504。
2023/10/01 21:29:30 [error] 13030#0: *143214 open() “/home/PHP/project/disdir/kk.html” failed (13: Permission denied), client: 200.200.110.29, server: www.8089.cn, request: “GET /disdir/kk.html HTTP/1.1”, host: “200.200.110.39:8089”
2023/10/01 21:23:14 [error] 24291#0: *143205 open() “/home/PHP/project/jj.html” failed (2: No such file or directory), client: 200.200.110.29, server: www.8083.cn, request: “GET /jj.html HTTP/1.1”, host: “200.200.110.39:8089”
405:请求方法不支持,如伪造非HTTP支持的方法(即非GET、POST、PUT等方法),这种Nginx并不会打印错误到error log
500:服务器内部错误,一般见于Nginx代理的上游服务出现错误,但并未断开TCP连接。比如在Nginx+PHP的情况下,PHP运行出现致命错误,就会出现500错误
2023/10/01 21:42:06 [error] 13030#0: *143244 FastCGI sent in stderr: “PHP message: PHP Fatal error: Class ‘Product’ not found in /home/PHP/project/index.php on line 5” while reading response header from upstream, client: 200.200.170.29, server: www.8089.cn, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “200.200.110.39:8089”
2023/09/20 13:52:57 [error] 6068#0: *539 connect() failed (111: Connection refused) while connecting to upstream, client: 200.200.110.39, server: www.8084.cn, request: “GET
/test/jj HTTP/1.1”, upstream: “http://200.200.110.39:9503/test/jj”, host: “200.200.110.39:8089”, referrer: “http://200.200.169.89”
2)TCP 突然连接断开
2023/09/21 10:44:55 [error] 32565#0: *34202 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 200.200.110.29, server: www.8089.cn, request: “GET /index.php?opr=hh HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “200.200.110.39:8089”, referrer: “http://200.200.169.89”
3)无可用上游服务
2023/09/20 13:52:59 [error] 6067#0: *544 no live upstreams while connecting to upstream, client: 200.200.110.29, server: www.8089.cn, request: “GET /test/jj HTTP/1.1”, upstream: “http://test_upstreams/test/jj”, host: “200.200.110.39:8089”, referrer: “http://200.200.169.89”
2023/09/25 10:12:37 [error] 31111#0: *143112 limiting requests, excess: 0.980 by zone “one”, client: 200.200.110.29, server: www.8089.cn, request: “GET /index.php HTTP/1.1”, host: “200.200.110.39:8089”
2023/09/21 11:18:59 [error] 1375#0: *142421 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 200.200.110.29, server: www.8089.cn, request: “GET /index.php?opr=hh HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “200.200.110.39:8089”, referrer: “http://200.200.169.89”
当然了,其他错误还有很多,慢慢积累,欢迎大家评论区补充
1]:RFC文档
2]:HTTP请求错误