Varnish 出现 503 错误,因为它无法连接到后端的 apache。这不是恒定的,但当 mytop 显示 40 秒及以上范围内的等待时间时,它通常会开始发生。
很多时候重启 apache 会阻止更多的 503 错误继续发生。但是有时重新启动 apache 和 Varnish 什么都不做。 那是当我更改 Apache 上的端口时,问题会消失几个小时,甚至可能是 12 小时左右。但最后,503 错误总是回来困扰我。这真是奇怪,怎么换一个端口就可以了一段时间。
我在 mpm_prefork_module 中使用了 MaxRequestWorkers,在 limits.conf 中打开了文件限制,查看了慢速查询、慢速 php 文件。 Apache 日志中没有任何内容。我只是缓解了问题,并没有消除它。
我还注意到,当有很多休眠连接(通过 mytop)时,我开始收到 503 错误。 我将连接时间限制为 45 秒,并且有一个脚本可以检查当前连接的数量、 sleep 连接的 sleep 时长,然后我会杀死它们。 我有时也会将 apache 标记为重新启动,并有一个 perl 脚本来重新启动它。重启 apache 后,大部分时间一切恢复正常。
有什么想法吗?我能做些什么?我搞不清楚了。当我有 php5 和 Ubuntu 12 时,它从来没有发生在我的旧服务器上。也许 Ubuntu 中的一些新东西就是这样做的。如果您能在下面查看我的数据,任何帮助都会很棒。我已经为此工作了 2 周。
每天 500,000 次页面浏览,每个页面加载大量图片的时间不到 1 秒。
Cloudflare -> Varnish -> Apache2 服务器版本:Apache/2.4.18 (Ubuntu) varnishd(varnish-4.1.1 修订版 66bb824) PHP 7.0
免费节目:
总使用免费共享 buff/可用缓存
内存:65940984 6506388 785104
中央处理器:24
型号名称:Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.60GHz
RAID 中有 4 个 1TB SSD 驱动器,我没有看到很多 I/O 或处理器在等待 I/O
负载将上升到 11,但通常看起来像:5.77、6.26、5.82。负载应该可以达到 28,因为我有 28 个过程。没有 Varnish 和直接 Apache 负载确实飙升至 26 及以上
Varnish default.vcl:
backend default {
.host = "127.0.0.1";
.port = "8082";
.connect_timeout = 10s;
.first_byte_timeout = 10s;
.between_bytes_timeout = 6s;
}
sub vcl_recv {
if (req.http.host ~ "(admin.example.com|cron.example.com)") {
return(pass);
}
}
一些 Apache 设置:
Timeout 50
KeepAlive on
MaxKeepAliveRequests 200
KeepAliveTimeout 5
<IfModule mpm_prefork_module>
StartServers 95
MinSpareServers 400
MaxSpareServers 750
MaxRequestWorkers 1400
MaxConnectionsPerChild 0
MaxClients 1550
</IfModule>
/etc/security/limits.conf:
* soft nofile 400000
* hard nofile 700000
mysql soft nofile 1024000
mysql hard nofile 1024000
root@one:/etc/apache2# ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache 内存使用量 (MB): "x/1024; print "平均进程大小 (MB): "x/((y-1)*1024)}'
Apache Memory Usage (MB): 8746.89
Average Proccess Size (MB): 33.9027
Varnishlog 显示 503 错误:
* << Request >> 347834142
- Begin req 347834141 rxreq
- Timestamp Start: 1482876824.797398 0.000000 0.000000
- Timestamp Req: 1482876824.797398 0.000000 0.000000
- ReqStart 108.162.219.141 31651
- ReqMethod HEAD
- ReqURL /226-213-2711
- ReqProtocol HTTP/1.1
- ReqHeader Host: example.com
- ReqHeader Connection: Keep-Alive
- ReqHeader Accept-Encoding: gzip
- ReqHeader CF-IPCountry: CA
- ReqHeader X-Forwarded-For: 209.171.88.168
- ReqHeader CF-RAY: 31801f1a54154716-EWR
- ReqHeader Content-Length: 0
- ReqHeader X-Forwarded-Proto: http
- ReqHeader CF-Visitor: {"scheme":"http"}
- ReqHeader Referer: http://example.com/22-2711
- ReqHeader x-wap-profile: http://wap.samsungmobile.com/uaprof/SGH-I747M.xml
- ReqHeader User-Agent: Mozilla/5.0 (Linux; U; Android 4.3; en-ca; SGH-I747M-parrot Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
- ReqHeader Accept-Language: en-CA, en-US
- ReqHeader Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7
- ReqHeader Accept: */*
- ReqHeader Cookie: __cfduid=d20ebc9c49642d5d5e9a0d76783dd140f1482876662; PHPSESSID=g4k9u49ei5tshlred89ttrib42; _pk_ref.3.082b=%5B%22%22%2C%22%22%2C1482876642%2C%22https%3A%2F%2Fwww.google.ca%22%5D; _pk_id.3.082b=ff9636b2577d5b22.1482876642.1.1482876707.1482876642.;
- ReqHeader CF-Connecting-IP: 209.171.88.168
- ReqUnset X-Forwarded-For: 209.171.88.168
- ReqHeader X-Forwarded-For: 209.171.88.168, 108.162.219.141
- VCL_call RECV
- VCL_return pass
- VCL_call HASH
- VCL_return lookup
- VCL_call PASS
- VCL_return fetch
- Link bereq 347834143 pass
- Timestamp Fetch: 1482877011.807866 187.010467 187.010467
- RespProtocol HTTP/1.1
- RespStatus 503
- RespReason Backend fetch failed
- RespHeader Date: Tue, 27 Dec 2016 22:16:51 GMT
- RespHeader Server: Varnish
- RespHeader Content-Type: text/html; charset=utf-8
- RespHeader Retry-After: 5
- RespHeader X-Varnish: 347834142
- RespHeader Age: 0
- RespHeader Via: 1.1 varnish-v4
- VCL_call DELIVER
- VCL_return deliver
- Timestamp Process: 1482877011.807904 187.010506 0.000038
- Debug "RES_MODE 0"
- RespHeader Connection: keep-alive
- Timestamp Resp: 1482877011.807922 187.010524 0.000018
- ReqAcct 933 0 933 222 0 222
- End
ObjHeader Retry-After: 5
- Length 286
- BereqAcct 0 0 0 0 0 0
- End
* << Request >> 351045135
- Begin req 351045134 rxreq
- Timestamp Start: 1482876998.128500 0.000000 0.000000
- Timestamp Req: 1482876998.128500 0.000000 0.000000
- ReqStart 108.162.245.39 20158
- ReqMethod GET
- ReqURL /604-343-7911/
- ReqProtocol HTTP/1.1
- ReqHeader Host: www.escortspolice.com
- ReqHeader Connection: Keep-Alive
- ReqHeader Accept-Encoding: gzip
- ReqHeader CF-IPCountry: US
- ReqHeader X-Forwarded-For: 157.55.39.219
- ReqHeader CF-RAY: 31802355e3b50c3b-SEA
- ReqHeader X-Forwarded-Proto: http
- ReqHeader CF-Visitor: {"scheme":"http"}
- ReqHeader Cache-Control: no-cache
- ReqHeader Pragma: no-cache
- ReqHeader Accept: */*
- ReqHeader From: bingbot(at)microsoft.com
- ReqHeader User-Agent: Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)
- ReqHeader CF-Connecting-IP: 157.55.39.219
- ReqUnset X-Forwarded-For: 157.55.39.219
- ReqHeader X-Forwarded-For: 157.55.39.219, 108.162.245.39
- VCL_call RECV
- VCL_return hash
- VCL_call HASH
- VCL_return lookup
- VCL_call MISS
- VCL_return fetch
- Link bereq 351045136 fetch
- Timestamp Fetch: 1482877013.130852 15.002352 15.002352
- RespProtocol HTTP/1.1
- RespStatus 503
- RespReason Backend fetch failed
- RespHeader Date: Tue, 27 Dec 2016 22:16:53 GMT
- RespHeader Server: Varnish
- RespHeader Content-Type: text/html; charset=utf-8
- RespHeader Retry-After: 5
- RespHeader X-Varnish: 351045135
- RespHeader Age: 0
- RespHeader Via: 1.1 varnish-v4
- VCL_call DELIVER
- VCL_return deliver
- Timestamp Process: 1482877013.130873 15.002373 0.000021
- RespHeader Content-Length: 286
- Debug "RES_MODE 2"
- RespHeader Connection: keep-alive
- Timestamp Resp: 1482877013.130894 15.002394 0.000021
- ReqAcct 449 0 449 243 286 529
- End
绘制(paint):
9386.38 VCL_return deliver
9327.10 VCL_return fetch
4708.72 RespProtocol HTTP/1.1
4692.61 VCL_call HASH
4692.61 VCL_call RECV
4692.61 VCL_call DELIVER
4692.61 VCL_return lookup
4692.61 RespHeader Via: 1.1 varnish-v4
4692.36 ReqProtocol HTTP/1.1
4688.35 RespHeader Connection: keep-alive
4687.20 ReqMethod GET
4670.60 RespHeader Age: 0
4664.45 BereqProtocol HTTP/1.1
4664.45 VCL_call BACKEND_FETCH
4659.96 BereqMethod GET
4628.04 ReqHeader Accept-Encoding: gzip
4619.31 BereqHeader Accept-Encoding: gzip
4579.22 ReqHeader Connection: Keep-Alive
4487.36 ObjProtocol HTTP/1.1
4487.35 BerespProtocol HTTP/1.1
4450.94 ReqHeader X-Forwarded-Proto: http
4450.94 ReqHeader CF-Visitor: {"scheme":"http"}
4440.67 BereqHeader X-Forwarded-Proto: http
4440.67 BereqHeader CF-Visitor: {"scheme":"http"}
4339.71 RespHeader Server: Apache/2.4.18 (Ubuntu)
4321.25 VCL_call BACKEND_RESPONSE
4321.25 ObjHeader Server: Apache/2.4.18 (Ubuntu)
4321.24 BerespHeader Server: Apache/2.4.18 (Ubuntu)
4207.14 ReqHeader CF-IPCountry: US
4196.86 BereqHeader CF-IPCountry: US
3860.32 Debug RES_MODE 2
3716.96 RespHeader Accept-Ranges: bytes
3482.28 Storage malloc Transient
3423.33 Fetch_Body 3 length stream
3139.05 Begin sess 0 HTTP/1
2947.17 RespStatus 200
2947.17 RespReason OK
2933.81 ObjStatus 200
2933.81 ObjReason OK
2933.79 BerespStatus 200
2933.79 BerespReason OK
2833.36 ReqHeader Accept: */*
2817.50 BereqHeader Accept: */*
2539.02 VCL_return hash
2492.11 VCL_call MISS
2171.20 VCL_call PASS
2153.92 VCL_return pass
1559.31 ReqHeader Pragma: no-cache
1551.06 BereqHeader Pragma: no-cache
1546.19 ReqHeader Cache-Control: no-cache
1539.36 ReqHeader User-Agent: Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bi
1533.79 BereqHeader User-Agent: Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bi
1529.78 RespHeader Content-Type: text/html; charset=UTF-8
1527.04 BerespHeader Content-Type: text/html; charset=UTF-8
1527.04 ObjHeader Content-Type: text/html; charset=UTF-8
1511.80 BerespHeader Pragma: no-cache
1511.80 ObjHeader Pragma: no-cache
1511.80 BerespHeader Cache-Control: no-store, no-cache, must-revalidate
1510.32 RespHeader Pragma: no-cache
1509.84 RespHeader Vary: Accept-Encoding
Apache Errorlog 仅显示以下内容(这是由于 LibOffice,不确定错误出在哪里,但我认为这不会产生任何影响):
W: Unknown node under /registry/extlang: deprecated
W: Unknown node under /registry/grandfathered: comments
W: Unknown node under /registry/extlang: deprecated
W: Unknown node under /registry/grandfathered: comments
W: Unknown node under /registry/grandfathered: comments
Mytop 显示一些正在 sleep 的整体。我让他们睡了 45 秒。在他们有时会休眠 900 秒或更长时间之前,Apache 会崩溃或 Varnish 会显示 503 错误
Key Efficiency: 99.0% Bps in/out: 51.7k/ 1.1M Now in/out: 129.5k/593.4k
Id User Host/IP DB Time Cmd State Query
-- ---- ------- -- ---- --- ----- ----------
540558 em localhost em 44 Sleep
540753 em localhost em 42 Sleep
540802 em localhost em 36 Sleep
540849 em localhost em 29 Sleep
540921 em localhost em 22 Sleep
541603 mt localhost mt 15 Sleep
541710 em localhost em 14 Sleep
541598 em localhost em 13 Sleep
541607 em localhost em 13 Sleep
541798 em localhost em 13 Sleep
最佳答案
我会建议您检查 apache worker 并仔细查看 apache 服务器状态以查看您收到了多少请求,
我认为您的 Apache 正在重载或
检查您的数据库服务器,它是否正常运行或是否给了高 CPU 负载。有时 apache 会由于数据库的开销而延迟
检查您获得了多少数据库连接以及您的数据库服务器是否正在处理它们。
关于php - Varnish 503 错误不会消失并且是随机的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41371687/