nginx 502的问题
网上的博客,我只想说去他妈了,天下博客一大抄,可恨抄的还是PHP。。。
一些可看的博客:
面试字节的时候,面试官问我502 504咋办?
我。。。。。结果第二天工作就遇到了504,从此之后502 504好像一直陪着我,
然而。。。。我还是一愁莫展
场景1 后端8080端口,而nginx配置成8081
xiaoyu@xiaoyudeMacBook-Pro ~ curl localhost:8080/testCtrl
f44a604b-3886-43df-acdb-d9a96baaabdf xiaoyu@xiaoyudeMacBook-Pro ~
xiaoyu@xiaoyudeMacBook-Pro ~
xiaoyu@xiaoyudeMacBook-Pro ~
xiaoyu@xiaoyudeMacBook-Pro ~ curl localhost:90/test/testCtrl
b8a4d0ae-3b3b-44b7-b8b7-b738d4792e8f xiaoyu@xiaoyudeMacBook-Pro ~
xiaoyu@xiaoyudeMacBook-Pro ~
# 修改了nginx的配置,配置成8081
xiaoyu@xiaoyudeMacBook-Pro ~ curl localhost:90/test/testCtrl -v
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 90 (#0)
> GET /test/testCtrl HTTP/1.1
> Host: localhost:90
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 502 Bad Gateway
< Server: nginx/1.19.10
< Date: Sun, 11 Jul 2021 15:02:52 GMT
< Content-Type: text/html
< Content-Length: 494
< Connection: keep-alive
< ETag: "6075b537-1ee"
场景2 后端服务没有起来
奈何第二份工作还要维护CI的环境,苦逼。但还是要干活,并且要认证的干活
一天下午同事发了一个截图,问我502咋解决。
我。。。。
同事可以看nginx的日志,发现nginx的日志502,又看了一下nginx的配置,断定服务是到了那个后端的服务。然后我去看了后端服务的日志,发现2点30之后就没有日志了。之前都是200啊。于是
结论就是请求到了nginx,但是nginx转发的请求没有到后端。
最后。。。。
后端服务没有起来,因为2.30后一个同事上了线,上线后没有检测服务有没有起来。。。
总结
- 检查服务器连接
- 检查是否更改了DNS
- 检查服务器日志
- 修复防火墙故障
504 问题
场景1 后端耗时1m钟,nginx配置的proxy_read_timeout 30s;
- proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响应超时时间
- proxy_read_timeout:连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
- proxy_send_timeout :后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
upstream myapp1 {
server 10.233.3.172:8080;
keepalive_timeout 10s;
}
upstream mysqlsess {
server 192.168.18.239:8080;
server 192.168.18.239:8081;
}
server {
listen 90;
listen [::]:90;
server_name localhost;
keepalive_timeout 10s;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /test/ {
proxy_pass http://192.168.18.239:8080/;
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}
location /se/ {
proxy_pass http://myapp1/;
# proxy_connect_timeout 30s;
proxy_send_timeout 30s;
# proxy_read_timeout 30s;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
xiaoyu@xiaoyudeMacBook-Pro ~/data/conf.d curl localhost:90/test/longRequest -v
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 90 (#0)
> GET /test/longRequest HTTP/1.1
> Host: localhost:90
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 504 Gateway Time-out
< Server: nginx/1.19.10
< Date: Sun, 11 Jul 2021 15:14:44 GMT
< Content-Type: text/html
< Content-Length: 494
< Connection: keep-alive