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端口。