0%

nginx之状态码说明

九·一八事变,又称奉天事变、柳条湖事件。是1931年9月18日夜日本在中国东北蓄意制造并发动的一场侵华战争,是日本帝国主义侵华的开端。

一、基础

      HTTP状态码(英语:HTTP Status Code)是用以表示网页服务器超文本传输协议响应状态的3位数字代码。它由RFC 2616规范定义的,并得到RFC 2518、RFC 2817、RFC 2295、RFC 2774与RFC 4918等规范扩展。所有状态码的第一个数字代表了响应的五种状态之一。所示的消息短语是典型的,但是可以提供任何可读取的替代方案。除非另有说明,状态码是HTTP/1.1标准(RFC 7231)的一部分。HTTP状态码的官方注册表由互联网号码分配局(Internet Assigned Numbers Authority)维护。

http请求

二、常见错误码

  1. 400

    • Bad Request,往往因为语法错误而无法被服务端理解
  2. 413

    • Request Entity Too Large,请求体过大,一般出现在上传大文件请求

    • 解决办法

      • 修改nginx配置文件
      1
      client_max_body_size 8M; ## 设置客户端请求体最大值
      • 修改php.ini
      1
      2
      post_max_size = 8M       # 整个表单提交的最大限制
      upload_max_filesize = 2M # 上传单个文件的最大限制
  3. 499

    • nginx独有,表示在收到客户端完整的HTTP request前,客户端试图关闭TCP连接导致

    • 下载nginx-1.16.1.tar.gz,解压到nginx-1.16.1目录,打开nginx-1.16.1/src/http/ngx_http_request.h文件,见499定义:

      1
      2
      3
      4
      5
      6
      7
      /*
      * HTTP does not define the code for the case when a client closed
      * the connection while we are processing its request so we introduce
      * own code to log such situation when a client has closed the connection
      * before we even try to send the HTTP header to it
      */
      #define NGX_HTTP_CLIENT_CLOSED_REQUEST 499
    • 复现

      • 定义处理请求脚本index.php,直接加入sleep(10);
      • 通过curl请求 curl -m 2 http://localhost/index.php
      • 查看访问日志 tail -n 10 access.log
  4. 500

  5. 502:Bad Gateway是指错误网关,无效网关

    • 一般可通过是杀死php-fpm进程再请求web来复现
  6. 503
    Service Temporarily Unavailable,即服务暂时不可用,一般由于临时的服务器维护或者过载,服务器当前无法处理请求。

  7. 504

  8. 403

    • 由于启动用户和nginx工作用户不一致所致
    • 缺少index.html或者index.php文件
    • 权限问题,如果nginx没有web目录的操作权限
    • SELinux设置为开启状态(enabled)的原因
  9. nginx状态码定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#define NGX_HTTP_CONTINUE                  100
#define NGX_HTTP_SWITCHING_PROTOCOLS 101
#define NGX_HTTP_PROCESSING 102

#define NGX_HTTP_OK 200
#define NGX_HTTP_CREATED 201
#define NGX_HTTP_ACCEPTED 202
#define NGX_HTTP_NO_CONTENT 204
#define NGX_HTTP_PARTIAL_CONTENT 206

#define NGX_HTTP_SPECIAL_RESPONSE 300
#define NGX_HTTP_MOVED_PERMANENTLY 301
#define NGX_HTTP_MOVED_TEMPORARILY 302
#define NGX_HTTP_SEE_OTHER 303
#define NGX_HTTP_NOT_MODIFIED 304
#define NGX_HTTP_TEMPORARY_REDIRECT 307
#define NGX_HTTP_PERMANENT_REDIRECT 308

#define NGX_HTTP_BAD_REQUEST 400
#define NGX_HTTP_UNAUTHORIZED 401
#define NGX_HTTP_FORBIDDEN 403
#define NGX_HTTP_NOT_FOUND 404
#define NGX_HTTP_NOT_ALLOWED 405
#define NGX_HTTP_REQUEST_TIME_OUT 408
#define NGX_HTTP_CONFLICT 409
#define NGX_HTTP_LENGTH_REQUIRED 411
#define NGX_HTTP_PRECONDITION_FAILED 412
#define NGX_HTTP_REQUEST_ENTITY_TOO_LARGE 413
#define NGX_HTTP_REQUEST_URI_TOO_LARGE 414
#define NGX_HTTP_UNSUPPORTED_MEDIA_TYPE 415
#define NGX_HTTP_RANGE_NOT_SATISFIABLE 416
#define NGX_HTTP_MISDIRECTED_REQUEST 421
#define NGX_HTTP_TOO_MANY_REQUESTS 429


/* Our own HTTP codes */

/* The special code to close connection without any response */
#define NGX_HTTP_CLOSE 444

#define NGX_HTTP_NGINX_CODES 494

#define NGX_HTTP_REQUEST_HEADER_TOO_LARGE 494

#define NGX_HTTPS_CERT_ERROR 495
#define NGX_HTTPS_NO_CERT 496

/*
* We use the special code for the plain HTTP requests that are sent to
* HTTPS port to distinguish it from 4XX in an error page redirection
*/
#define NGX_HTTP_TO_HTTPS 497

/* 498 is the canceled code for the requests with invalid host name */

/*
* HTTP does not define the code for the case when a client closed
* the connection while we are processing its request so we introduce
* own code to log such situation when a client has closed the connection
* before we even try to send the HTTP header to it
*/
#define NGX_HTTP_CLIENT_CLOSED_REQUEST 499


#define NGX_HTTP_INTERNAL_SERVER_ERROR 500
#define NGX_HTTP_NOT_IMPLEMENTED 501
#define NGX_HTTP_BAD_GATEWAY 502
#define NGX_HTTP_SERVICE_UNAVAILABLE 503
#define NGX_HTTP_GATEWAY_TIME_OUT 504
#define NGX_HTTP_VERSION_NOT_SUPPORTED 505
#define NGX_HTTP_INSUFFICIENT_STORAGE 507

三、参考

  1. 参考一
  2. 参考二