资讯专栏INFORMATION COLUMN

【Nginx实战】构建NGINX 4xx 5xx 状态码实例

Lsnsh / 1725人阅读

摘要:运营研发团队张仕华配置系列头不合法头重复参考如上配置访问需要认证将设置为不可读使用非方法访问一个静态文件系列修改为缺少引号语法错误的现在只支持如果客户端随意设置这个值会报修改配置为指向一

运营研发团队 张仕华

nginx配置

</>复制代码

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  9. sendfile on;
  10. keepalive_timeout 65;
  11. server {
  12. listen 8070;
  13. server_name 10.96.79.14;
  14. limit_req zone=one;
  15. location / {
  16. root html;
  17. index index.html index.htm;
  18. }
  19. error_page 500 502 503 504 /50x.html;
  20. location = /50x.html {
  21. root html;
  22. }
  23. location = /abc.html {
  24. root html;
  25. auth_basic "opened site";
  26. auth_basic_user_file conf/htpasswd;
  27. }
  28. location ~ .php$ {
  29. root /home/xiaoju/nginx-1.14.0/html;
  30. fastcgi_index index.php;
  31. fastcgi_pass 127.0.0.1:9000;
  32. fastcgi_param SCRIPT_FILENAME /home/xiaoju/nginx-1.14.0/html$fastcgi_script_name;
  33. include fastcgi.conf;
  34. fastcgi_connect_timeout 300;
  35. fastcgi_send_timeout 300;
  36. fastcgi_read_timeout 300;
  37. }
  38. }
  39. }

index.php

</>复制代码

  1. 4xx系列
  2. 400
  3. NGX_HTTP_BAD_REQUEST

  4. </>复制代码

    1. Host头不合法
    2. curl localhost:8070 -H "Host:123/com"
    3. 400 Bad Request
    4. 400 Bad Request


    5. nginx/1.14.0
    6. Content-Length头重复
    7. curl localhost:8070 -H "Content-Length:1" -H "Content-Length:2"
    8. 400 Bad Request
    9. 400 Bad Request


    10. nginx/1.14.0
  5. 401
  6. NGX_HTTP_UNAUTHORIZED

  7. </>复制代码

    1. 参考如上nginx配置,访问abc.html需要认证
    2. curl localhost:8070/abc.html
    3. 401 Authorization Required
    4. 401 Authorization Required


    5. nginx/1.14.0
  8. 403
  9. NGX_HTTP_FORBIDDEN

  10. </>复制代码

    1. chmod 222 index.html
    2. index.html设置为不可读
    3. curl localhost:8070
    4. 403 Forbidden
    5. 403 Forbidden


    6. nginx/1.14.0
  11. 404
  12. NGX_HTTP_NOT_FOUND

  13. </>复制代码

    1. curl localhost:8070/cde.html
    2. 404 Not Found
    3. 404 Not Found


    4. nginx/1.14.0
  14. 405
  15. NGX_HTTP_NOT_ALLOWED

  16. </>复制代码

    1. 使用非GET/POST/HEAD方法访问一个静态文件
    2. curl -X DELETE localhost:8070/index.html -I
    3. HTTP/1.1 405 Not Allowed
    4. Server: nginx/1.14.0
    5. Date: Tue, 18 Sep 2018 10:02:22 GMT
    6. Content-Type: text/html
    7. Content-Length: 173
    8. Connection: keep-alive
  17. 5xx系列
  18. 500
  19. NGX_HTTP_INTERNAL_SERVER_ERROR

  20. 修改index.php

  21. </>复制代码

    1. 缺少引号,语法错误

    2. </>复制代码

      1. curl localhost:8070/index.php -I
      2. HTTP/1.1 500 Internal Server Error
      3. Server: nginx/1.14.0
      4. Date: Tue, 18 Sep 2018 11:29:19 GMT
      5. Content-Type: text/html; charset=UTF-8
      6. Connection: keep-alive
      7. Set-Cookie: PHPSESSID=aoesvcuvbh1nh95kdkp152r9e1; path=/
      8. Expires: Thu, 19 Nov 1981 08:52:00 GMT
      9. Cache-Control: no-store, no-cache, must-revalidate
      10. Pragma: no-cache
    3. 501
    4. NGX_HTTP_NOT_IMPLEMENTED

    5. </>复制代码

      1. nginx的transfer-encoding现在只支持chunked,如果客户端随意设置这个值,会报501
      2. curl localhost:8070 -H "Transfer-Encoding:1"
      3. 501 Not Implemented
      4. 501 Not Implemented


      5. nginx/1.14.0
    6. 502
    7. NGX_HTTP_BAD_GATEWAY

    8. </>复制代码

      1. 修改nginx配置为
      2. fastcgi_pass 127.0.0.1:8000;
    9. 指向一个未监听的端口

    10. </>复制代码

      1. curl localhost:8070/index.php -I
      2. HTTP/1.1 502 Bad Gateway
      3. Server: nginx/1.14.0
      4. Date: Tue, 18 Sep 2018 11:28:17 GMT
      5. Content-Type: text/html
      6. Content-Length: 537
      7. Connection: keep-alive
      8. ETag: "5ad6113c-219"
    11. 503
    12. NGX_HTTP_SERVICE_UNAVAILABLE

    13. </>复制代码

      1. 修改nginx配置,限速为每分钟10个请求
      2. limit_req_zone $binary_remote_addr zone=one:10m rate=10r/m;
      3. limit_req zone=one;
    14. </>复制代码

      1. 连续发送两个请求,第二请求会报503
      2. curl localhost:8070/index.php -I
      3. HTTP/1.1 503 Service Temporarily Unavailable
      4. Server: nginx/1.14.0
      5. Date: Tue, 18 Sep 2018 11:31:43 GMT
      6. Content-Type: text/html
      7. Content-Length: 537
      8. Connection: keep-alive
      9. ETag: "5ad6113c-219"
    15. 504
    16. NGX_HTTP_GATEWAY_TIME_OUT

    17. 修改index.php

    18. </>复制代码

      1. </>复制代码

        1. curl localhost:8070/index.php -I
        2. HTTP/1.1 504 Gateway Time-out
        3. Server: nginx/1.14.0
        4. Date: Tue, 18 Sep 2018 12:17:57 GMT
        5. Content-Type: text/html
        6. Content-Length: 537
        7. Connection: keep-alive
        8. ETag: "5ad6113c-219"
      2. 505
      3. NGX_HTTP_VERSION_NOT_SUPPORTED

      4. </>复制代码

        1. telnet8070端口,输入GET /index.html HTTP/2.1
        2. 不支持http/2.1,会报505
        3. $telnet localhost 8070
        4. Trying 127.0.0.1...
        5. Connected to localhost.
        6. Escape character is "^]".
        7. GET /index.html HTTP/2.1
        8. HTTP/1.1 505 HTTP Version Not Supported
        9. Server: nginx/1.14.0
        10. Date: Tue, 18 Sep 2018 12:26:35 GMT
        11. Content-Type: text/html
        12. Content-Length: 203
        13. Connection: close
        14. 505 HTTP Version Not Supported
        15. 505 HTTP Version Not Supported


        16. nginx/1.14.0
      5. 后续基于这几种情况,看Nginx源码内部是怎么实现的。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/40174.html

相关文章

  • NGINX 4xx 5xx 状态构造

    摘要:配置系列头不合法头重复参考如上配置访问需要认证将设置为不可读使用非方法访问一个静态文件系列修改为缺少引号语法错误的现在只支持如果客户端随意设置这个值会报修改配置为指向一个未监听的端口 nginx配置 worker_processes 1; events { worker_connections 1024; } http { include mime.ty...

    jemygraw 评论0 收藏0
  • 2小时快速搭建后端接口报警系统(基于阿里云日志服务分析nginx访问日志)

    摘要:目标后端任一接口一分钟内响应超过一定的量,马上收到报警提示报警及慢接口有详细列表可以查看低成本。相关报警请求的详细信息列表慢响应分析 目标 后端任一接口一分钟内5xx响应超过一定的量,马上收到报警提示 报警及慢接口有详细列表可以查看 低成本。几年前公司的日志报警系统是自研的,开发成本比较高,也没有达到阿里云日志服务这种产品化程度 机器部署情况 阿里云EC服务器 功能概述 阿里云日...

    lemanli 评论0 收藏0
  • nginx lua重置请求参数及常量备忘

    序 本文主要讲述一下nginx lua如何重置请求参数以及整理了几类常量。 重置请求参数 获取请求参数 local strider = ngx.var.arg_strider local strider = ngx.req.get_uri_args[strider] 当请求uri中有多个同名参数时,ngx.var.arg_xx的做法是取第一个出现的值,ngx.req_get_uri_args[xx...

    IntMain 评论0 收藏0
  • HTTP状态

    摘要:若用户发起了一个条件请求,而资源近期未被修改,可以通过该状态码表明。将来的请求应该使用老的和状态码之间存在一些交叉。服务器担心请求会引发冲突时,可以发送此状态码。 状态码 状态码是来告诉客户端,发生了什么事情。状态码为客户端提供了一种理解事务处理结果的便捷方式。状态码位于响应的起始行中 比如,在行 HTTP/1.0 200 OK 中,状态码就是200 客户端向一个 HTTP 服务器发送...

    junfeng777 评论0 收藏0

发表评论

0条评论

Lsnsh

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<