记录出现5xx状态码的坑(持续更新......)

5xx状态码
  • 500 Internal Server Error:服务器内部出现错误,无法完成请求。
    服务器或上游服务器有错误,如重写规则或代码语法有误等。

  • 502 Bad Gateway:作为网关或代理工作的服务器尝试执行请求时,从上游服务器接收到无效响应。
    上游服务器有异常,可能是服务未启用。

  • 503 Service Temporarily Unavailable:服务暂时不可用。
    服务器或上游服务器超载或正在维护中,服务暂时不可用。

  • 504 Gateway Time-out:由作为代理或网关的服务器使用,未及时从上游服务器接收到响应。
    上游服务器有异常,无法连接上。

500状态码
案例一
http {
    ...
    ...
    server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;
        location / {
            rewrite  ^/test(.*)$ $1 last; # 重写规则
        }
    }
}
[root@centos ~]# curl -v http://localhost/test
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* 拒绝连接
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /test HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 500 Internal Server Error
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 04:06:22 GMT
< Content-Type: text/html
< Content-Length: 177
< Connection: close
< 
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
* Closing connection 0
[root@centos ~]#

具体原因:rewrite规则有问题。


案例二
[root@centos ~]# curl -v http://localhost/1.php
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* 拒绝连接
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /1.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 500 Internal Server Error
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 03:58:36 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.4.16
< 
* Connection #0 to host localhost left intact
[root@centos ~]# 
<?php
$servername = "localhost";
$username = "root";
$password = "password";
try {
    $conn = new PDO("mysql:host=$servername", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SHOW DATABASES";
    $conn->exec($sql);
    var_dump("successful"); 
}
catch(PDOException $e)
{
    var_dump($e->getMessage());
}
$conn = null;
?>
PHP Fatal error:  Class 'PDO' not found in /usr/share/nginx/html/1.php on line 6
PHP Fatal error:  Class 'PDO' not found in /usr/share/nginx/html/1.php on line 6

具体原因:php的pdo_mysql模块没有安装。


案例三
[root@centos ~]# curl -svo /dev/null http://localhost/1.php
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* 拒绝连接
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /1.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 500 Internal Server Error
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 07:15:12 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.4.16
< 
{ [data not shown]
* Connection #0 to host localhost left intact
[root@centos ~]# 
<?php
$servername = "localhost";
$username = "root"  // 少了分号
$password = "password";
try {
    $conn = new PDO("mysql:host=$servername", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SHOW DATABASES";
    $conn->exec($sql);
    var_dump("successful");
}
catch(PDOException $e)
{
    var_dump($e->getMessage());
}
$conn = null;
?>

具体原因:php代码语法有误。


502状态码
案例一
[root@centos ~]# curl -v http://localhost/1.php
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* 拒绝连接
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /1.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 502 Bad Gateway
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 04:20:58 GMT
< Content-Type: text/html
< Content-Length: 157
< Connection: keep-alive
< 
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
* Connection #0 to host localhost left intact
[root@centos ~]# 
[root@centos ~]# ss -lnpt 
State       Recv-Q Send-Q                               Local Address:Port                                              Peer Address:Port              
LISTEN      0      128                                              *:80                                                           *:*                   users:(("nginx",pid=3799,fd=6),("nginx",pid=3793,fd=6))
LISTEN      0      128                                              *:22                                                           *:*                   users:(("sshd",pid=884,fd=3))
LISTEN      0      128                                           [::]:22                                                        [::]:*                   users:(("sshd",pid=884,fd=4))
[root@centos ~]# 
[root@centos ~]# ps -ef | grep php-fpm
root      4093  2249  0 12:23 pts/2    00:00:00 grep --color=auto php-fpm
[root@centos ~]# 

具体原因:php-fpm没有启动。


案例二
root@okokok:~# curl -vo /dev/null http://192.168.3.100/t.img
*   Trying 192.168.3.100:80...
* TCP_NODELAY set
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to 192.168.3.100 (192.168.3.100) port 80 (#0)
> GET /t.img HTTP/1.1
> Host: 192.168.3.100
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 502 Bad Gateway
< Server: nginx/1.20.1
< Date: Thu, 23 Jun 2022 11:31:29 GMT
< Content-Type: text/html
< Content-Length: 157
< Connection: keep-alive
< X-Request-ID: 0d75b66df06957a2b5567d887ec40ad9
< 
{ [157 bytes data]
100   157  100   157    0     0  52333      0 --:--:-- --:--:-- --:--:-- 52333
* Connection #0 to host 192.168.3.100 left intact
root@okokok:~# 
[error] 5242#5242: *574 upstream sent too big header while reading response header from upstream
http {
    ...
    ...
    proxy_buffer_size 100;  # 100 太小了,需要增大。

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass  http://192.168.3.204;
        }
    }
}

具体原因:proxy_buffer_size 设置太小,增大值即可。
proxy_buffer_size:后端服务器的响应头会存放在该buffer中,如果该值比响应头还小就会报502。


503状态码
案例一
[root@centos ~]# curl -vo /dev/null http://localhost/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* 拒绝连接
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 503 Service Temporarily Unavailable
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 04:56:26 GMT
< Content-Type: text/html
< Content-Length: 197
< Connection: keep-alive
< 
{ [data not shown]
100   197  100   197    0     0  41808      0 --:--:-- --:--:-- --:--:-- 49250
* Connection #0 to host localhost left intact
[root@centos ~]# 
http {
    ...
    ...
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; # 限流
    server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;
        location / {
            limit_req zone=one burst=1 nodelay;
        }
    }
}

具体原因:触发限流。

504状态码
案例一
[root@ov ~]# curl -svo /dev/null http://192.168.3.100/
* About to connect() to 192.168.3.100 port 80 (#0)
*   Trying 192.168.3.100...
* Connected to 192.168.3.100 (192.168.3.100) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.3.100
> Accept: */*
>
< HTTP/1.1 504 Gateway Time-out
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 05:23:16 GMT
< Content-Type: text/html
< Content-Length: 167
< Connection: keep-alive
<
{ [data not shown]
* Connection #0 to host 192.168.3.100 left intact
[root@ov ~]#
http {
    ...
    ...
    server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;
        location / {
            proxy_pass http://192.168.3.204;
            proxy_http_version 1.1;
            proxy_connect_timeout 5;
            proxy_read_timeout 5;
            proxy_send_timeout 5;
        }
    }
}
[root@ov ~]# time curl -svo /dev/null http://192.168.3.204/
* About to connect() to 192.168.3.204 port 80 (#0)
*   Trying 192.168.3.204...
* 连接超时
* Failed connect to 192.168.3.204:80; 连接超时
* Closing connection 0

real    2m9.329s
user    0m0.004s
sys     0m0.005s
[root@ov ~]#

具体原因:连接不上远端服务器(192.168.3.204)。

案例二
[root@centos ~]# curl -svo /dev/null http://localhost/1.php
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* 拒绝连接
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /1.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 504 Gateway Time-out
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 06:58:39 GMT
< Content-Type: text/html
< Content-Length: 167
< Connection: keep-alive
< 
{ [data not shown]
* Connection #0 to host localhost left intact
[root@centos ~]# 
http {
    ...
    ...
    server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;
    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include       /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_connect_timeout 5s;
        fastcgi_send_timeout    5s;
        fastcgi_read_timeout    5s;
    }
    }
}
[root@centos ~]# ss -lnpt
State       Recv-Q Send-Q                               Local Address:Port                                              Peer Address:Port              
LISTEN      0      128                                      127.0.0.1:9000                                                         *:*                   users:(("php-fpm",pid=4528,fd=0),("php-fpm",pid=4527,fd=0),("php-fpm",pid=4526,fd=0),("php-fpm",pid=4525,fd=0),("php-fpm",pid=4524,fd=0),("php-fpm",pid=4522,fd=6))
LISTEN      0      128                                              *:80                                                           *:*                   users:(("nginx",pid=4483,fd=6),("nginx",pid=4300,fd=6))
LISTEN      0      128                                              *:22                                                           *:*                   users:(("sshd",pid=884,fd=3))
LISTEN      0      128                                           [::]:22                                                        [::]:*                   users:(("sshd",pid=884,fd=4))
[root@centos ~]# ps -ef | grep php-fpm
root      4522     1  0 15:01 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache    4524  4522  0 15:01 ?        00:00:00 php-fpm: pool www
apache    4525  4522  0 15:01 ?        00:00:00 php-fpm: pool www
apache    4526  4522  0 15:01 ?        00:00:00 php-fpm: pool www
apache    4527  4522  0 15:01 ?        00:00:00 php-fpm: pool www
apache    4528  4522  0 15:01 ?        00:00:00 php-fpm: pool www
root      4534  2249  0 15:01 pts/2    00:00:00 grep --color=auto php-fpm
[root@centos ~]# 
[root@centos ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:9000

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@centos ~]# 

具体原因:连接不上php-fpm,防火墙阻拦所有客户端来访问9000端口。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容