502状态码
模拟502报错有两种办法:
1、不启动php-fpm(本次使用)。
2、iptables拦截客户端来访问9000端口,命令:iptables -A INPUT -p tcp --dport 9000 -j REJECT
。
nginx配置文件如下。
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 *:80 *:* users:(("nginx",pid=4642,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 4664 2227 0 15:30 pts/1 00:00:00 grep --color=auto php-fpm
[root@centos ~]#
tcpdump进行抓包,命令:tcpdump -i any port 9000 -w http502.pcap
。
[root@centos ~]# curl -v http://127.0.0.1/1.php
* About to connect() to 127.0.0.1 port 80 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /1.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1
> Accept: */*
>
< HTTP/1.1 502 Bad Gateway
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 07:26:10 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 127.0.0.1 left intact
[root@centos ~]#
用 Wireshark 查看 http502.pcap
抓包文件里面的内容。
image.png
nginx请求php-fpm 9000端口,9000端口有回包,但是给了个 RST
。
504状态码
nginx配置不变,这次将php-fpm启动起来,并且确定能够正常访问。
[root@centos ~]# curl -svo /dev/null http://127.0.0.1/1.php
* About to connect() to 127.0.0.1 port 80 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /1.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 07:40:54 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 127.0.0.1 left intact
[root@centos ~]#
测试响应正常的200状态码,用 iptables
拦截所有客户端来访问9000端口,这样可以模拟504报错。
iptables命令:iptables -A INPUT -p tcp --dport 9000 -j DROP
。
然后再用tcpdump抓包,tcpdump命令:tcpdump -i any port 9000 -w http504.pcap
。
[root@centos ~]# curl -svo /dev/null http://127.0.0.1/1.php
* About to connect() to 127.0.0.1 port 80 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /1.php HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1
> Accept: */*
>
< HTTP/1.1 504 Gateway Time-out
< Server: nginx/1.20.1
< Date: Wed, 22 Jun 2022 07:46:09 GMT
< Content-Type: text/html
< Content-Length: 167
< Connection: keep-alive
<
{ [data not shown]
* Connection #0 to host 127.0.0.1 left intact
[root@centos ~]#
依旧是用 Wireshark 查看抓包内容。
image.png
nginx请求php-fpm 9000端口,9000端口没有回包,全是SYN
握手包。
总结:502是对端收到请求并给予响应,但是直接给你拒绝了;而504是对端根本就不鸟你,可能是对端没收到请求或把你忽略掉了。