Nginx错误recv()失败(104:由对等方重置连接)

时间:2014-08-04 15:42:49

标签: php apache nginx recv gateway

几天前,我的服务器上出现了一些错误。我使用CentOS 6.5和Parallels 12.0.18,Apache服务器提供动态内容,Nginx用作代理服务静态内容。

起初,我收到以下错误:

[error] 29951#0: *5138862 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 89.7.24.108, server: , request: "GET /page/2/ HTTP/1.1", upstream: "http://ip:7080/page/2/", host: "domain.es", referrer: "http://domain.es/"

然后,我改变了一些配置,比如在我的" httpd.conf"上增加MaxClients。文件和这行到我的/etc/nginx/conf.d/timeout.conf文件:

proxy_connect_timeout       600;
proxy_send_timeout          600;
proxy_read_timeout          600;
send_timeout                600;

在我再次遇到相同的错误以及一个新错误之前,似乎一切正常。

[error] 15228#0: *130292 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 89.130.25.154, server: domain.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:7080/", host: "domain.com"

我在同一台服务器上有两个不同的网站。这就是你在那里看到两个不同主机的原因。

问题在于:当我收到这些错误时,我得到了一个" 502 Bad Gateway"并且服务器变得如此之慢以至于我甚至无法使用SSH终端登录。我只能通过重置httpd服务暂时解决它。

我知道还有其他与此相似的主题,但我发现的只是PHP-FPM的问题,我不会使用它。

这是我的Nginx配置文件:     用户nginx;     worker_processes 16;

error_log  /var/log/nginx/error.log;

events {
    worker_connections  1024;
}

http {
server_names_hash_max_size 2048;
server_names_hash_bucket_size 512;

server_tokens off;

include    mime.types;
default_type  application/octet-stream;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout  10;

# Gzip on
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_buffers 4 32k;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
gzip_disable "MSIE [1-6]\.";

# Other configurations
ignore_invalid_headers on;
client_max_body_size    8m;
client_header_timeout  3m;
client_body_timeout 3m;
#send_timeout     3m;
connection_pool_size  256;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
request_pool_size  4k;
output_buffers   4 32k;
postpone_output  1460;

# Cache most accessed static files
open_file_cache          max=10000 inactive=10m;
open_file_cache_valid    2m;
open_file_cache_min_uses 1;
open_file_cache_errors   on;

# virtual hosts includes
include "/etc/nginx/conf.d/*.conf";
}

这是我的Nginx vhost文件:     服务器{     listen ip:80 default_server;

server_name domain.es;
server_name www.domain.es;
server_name ipv4.domain.es;

client_max_body_size 128m;

root "/var/www/vhosts/domain.es/httpdocs";
access_log "/var/www/vhosts/system/domain.es/logs/proxy_access_log";
error_log "/var/www/vhosts/system/domain.es/logs/proxy_error_log";

if ($host ~* ^www.domain.es$) {
    rewrite ^(.*)$ http://domain.es$1 permanent;
}

location / {
    proxy_pass http://82.194.74.41:7080;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
    access_log off;
}

location @fallback {
    proxy_pass http://ip:7080;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
    access_log off;
}

location ~ ^/plesk-stat/ {
    proxy_pass http://ip:7080;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
    access_log off;
}

location ~ ^/(.*\.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|swf|tar|tgz|txt|wav|xls|xlsx|zip))$ {
    access_log off;
    expires 7d;
    add_header Cache-Control public;

 try_files $uri @fallback;
}

include "/var/www/vhosts/system/domain.es/conf/vhost_nginx.conf";
}

我在Apache(httpd.conf)中使用的一些配置变量:

<IfModule prefork.c>
StartServers      14
MinSpareServers    8
MaxSpareServers   14
ServerLimit      1000
MaxClients       1000
MaxRequestsPerChild  2000
</IfModule>

非常感谢你!

3 个答案:

答案 0 :(得分:1)

看起来你的Apache比你的Nginx更忙。当Nginx得到一些请求但Apache无法处理时,你会得到“糟糕的网关”,这就是Apache拒绝为Nginx工作。

尝试减少&#39; worker_connections&#39;和&#39; worker_processes&#39;在Nginx中增加&#39; MaxClients&#39; ServerLimit&#39;

确保 worker_connections * worker_processes&lt; MaxClients&lt;将ServerLimit

答案 1 :(得分:1)

在我的情况下,缺少一个php扩展,在我关闭它后,它恢复了!检查/var/log/messages以查看是否有segfault

答案 2 :(得分:0)

在最近的情况下,我发现该错误记录了95%的请求,但是Web服务器和Apps服务器几乎都处于空闲状态,只有几个连接处于打开状态。其余5%成功。

事实证明,我们友好的网络同事已在Web和应用程序服务器之间配置了IPS设备,并在流量增加时开始隐退并断开连接,因为它认为发生了暴力攻击。假阳性。因此,与Nginx或Apache无关,而是重新配置IPS解决了该问题。