【问题标题】:nginx refuses connections after some time一段时间后nginx拒绝连接
【发布时间】:2017-05-28 12:14:24
【问题描述】:

我的 nginx 配置有问题,我的服务器工作正常,但 1 或 2 天后它只是挂起并停止响应。无法再连接到服务器了。

(7) Failed to connect to XX.XX.XX.XX port 80: Connection refused

这个服务器的主要工作是运行繁重的 PHP 任务,我每 5 秒运行一次 cron 任务来处理许多任务。重新启动有帮助,并且 nginx 在接下来的 1-2 天内再次正常工作。我没有任何错误日志,nginx 不会在 /var/log/nginx/error.log 中报告任何内容。它只是使所有连接失败。有什么想法可以从哪里开始寻找问题?

我在 Ubuntu 16.04、2 个 CPU、4 GB 内存和 PHP 7.0 上运行 nginx。 nginx版本:nginx/1.10.0 (Ubuntu)

这里是配置文件:

 user www-data;
 worker_processes 2;
 pid /run/nginx.pid;

 events {
  worker_connections 4096;
  #multi_accept on;
 }

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 300;
types_hash_max_size 2048;
server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

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

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

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

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

这是默认文件:

server {
 large_client_header_buffers 4 128k;
 listen XX:XX:XX:XX:80;

 set $root_path '/var/www/web/public/';

 root $root_path;
 index index.php;

 server_name XX:XX:XX:XX;

 location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ /index.php?$query_string;
 }

 location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_read_timeout 14400;

    proxy_set_header   Connection "";
    proxy_http_version 1.1;
 }

 location /status {
        stub_status on;
        access_log  off;
        allow XX:XX:XX:XX;
        deny all;
 }

 location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
    root $root_path;
 }

 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 location ~ /\.ht {
    deny all;
 }
}

最重要的是,我运行的 cron 作业,例如:

* * * * * sleep 5; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 10; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 15; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 20; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 25; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 30; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 35; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 40; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 45; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1
* * * * * sleep 50; curl --request GET http://XX:XX:XX:XX/queue/fire > /dev/null 2>&1

等等

更新:

PHP FPM 状态

 * php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2017-01-13 10:46:41 CET; 3 days ago
  Process: 1439 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS)
 Main PID: 1662 (php-fpm7.0)
   Status: "Processes active: 0, idle: 25, Requests: 164215, slow: 0, Traffic: 0req/sec"
    Tasks: 26
   Memory: 377.3M
      CPU: 5h 57min 32.279s
   CGroup: /system.slice/php7.0-fpm.service
           |- 1662 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)                      
           |- 1752 php-fpm: pool www                                                            
           |- 8751 php-fpm: pool www                                                            
           |-12078 php-fpm: pool www                                                            
           |-14053 php-fpm: pool www                                                            
           |-14338 php-fpm: pool www                                                            
           |-14639 php-fpm: pool www                                                            
           |-14763 php-fpm: pool www                                                            
           |-16188 php-fpm: pool www                                                            
           |-16212 php-fpm: pool www                                                            
           |-16900 php-fpm: pool www                                                            
           |-17620 php-fpm: pool www                                                            
           |-17621 php-fpm: pool www                                                            
           |-17766 php-fpm: pool www                                                            
           |-18802 php-fpm: pool www                                                            
           |-19084 php-fpm: pool www                                                            
           |-22064 php-fpm: pool www                                                            
           |-24245 php-fpm: pool www                                                            
           |-24690 php-fpm: pool www                                                            
           |-25120 php-fpm: pool www                                                            
           |-27714 php-fpm: pool www                                                            
           |-29415 php-fpm: pool www                                                            
           |-30182 php-fpm: pool www                                                            
           |-30391 php-fpm: pool www                                                            
           |-32053 php-fpm: pool www                                                            
           `-32358 php-fpm: pool www                                                            

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

【问题讨论】:

    标签: php ubuntu nginx cron connection


    【解决方案1】:

    我会检查 php fpm 日志。也许用完了 php fpm 进程。

    【讨论】:

    • 谢谢!刚刚检查了一下,我有很多警告WARNING: [pool www] server reached pm.max_children setting (5), consider raising it.我忘记了默认设置,我会增加它,看看它是否有帮助。
    • 它没有帮助;/我从默认更改为:pm.max_children = 40 and pm.start_servers = 15 and pm.min_spare_servers = 15 and pm.max_spare_servers = 25 and pm.max_requests = 500。 2天后我遇到了同样的问题,没有更多关于孩子的错误,也没有来自nginx的错误;/有什么想法吗?
    • 这是我能想到的唯一会导致这种行为的事情。我建议打开 NGINX 和 php-fpm 的访问和错误日​​志并跟踪失败的请求。
    • 我更新了配置文件,所以下次我会尝试跟踪它。我又遇到了同样的情况,我的 nginx 刚刚停止响应。我检查了 php-fpm 状态并更新了我的主要帖子。不知道为什么我有空闲:状态中的 25 .. 也许我的 PHP 正在做一些讨厌的事情,因为我有繁重的 PHP 任务,它们可能需要 1-2 小时,所以我总是设置在 PHP set_time_limit(14400); ini_set('memory_limit', '-1'); 也许这里是不正确..有趣的是,每当我重新启动 Nginx 时,一切正常……看起来像 Nginx 问题。
    • 有一些fastcgi超时设置可以玩,比如fastcgi_read_timeout:nginx.org/en/docs/http/…
    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 2018-12-11
    • 2021-06-22
    • 1970-01-01
    • 2019-03-03
    • 2013-03-02
    • 2020-09-16
    • 2021-01-16
    相关资源
    最近更新 更多