Nginx 非常慢。从 Apache2 更改为 Nginx

Nginx 非常慢。从 Apache2 更改为 Nginx

我最近将服务器从 Apache 更改为 Nginx。这是因为该网站正在使用 file_get_contents 访问另一个网站。当该外部网站瘫痪或速度缓慢时,线程就会达到最大值,服务器就会瘫痪。这就像多米诺骨牌效应。

然后,我将服务器从 Apache2 升级到 Nginx,同时还运行一个运行 Nginx 的反向代理。这样,我就可以在 1 个 IP 地址上运行多个具有多个域的服务器。

今天我注意到访问量急剧下降,并迅速采取行动。服务器非常慢,甚至出现几次超时。


我有 3 台服务器:

  1. 反向代理 - Nginx(互联网的主要连接)
  2. 第一个网站 - Nginx(每天约有 10,000 名独立访客)
  3. 第二个网站 - Apache2(实验性网站,响应非常快)

连接到第一个网站时,速度非常慢(等待时间约 28 秒),但连接到第二个网站时,速度非常快(等待时间约 0.8 秒)

我当时对 Nginx 了解甚少,因为我已经使用 Apache2 有 2 年了。


反向代理 - /etc/nginx/sites-available/default

是的,第一个域名上有很多域名

server {
    listen 80;

    server_name domain.com www.domain.com domain.org www.domain.org domain.co.uk www.domain.co.uk domain.co www.domain.co domain.me www.domain.me domain.se www.domain.se;

    location / {
        proxy_pass http://192.168.1.237;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        real_ip_header X-Real-IP;
        real_ip_recursive on;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }
}
server {
    listen 80;

    server_name www.domain2.com domain2.com;

    location / {
        proxy_pass http://192.168.1.230;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
     }
}
server {
    listen 80;

    server_name controller.domain.com;

    location / {
        allow 84.71.x.x/21;
        allow 192.168.0.0/16;
        deny all;
        proxy_pass http://192.168.1.1;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        }
}

第一个 Web 服务器 - /etc/nginx/nginx.conf

user www-data;
worker_processes 10;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        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;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # Virtual Host Configs
        ##

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

第一个 Web 服务器 - /etc/nginx/sites-available/default

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html/pi;
        index index.php;

        # It seems like the server only needed 1 of these, as all the other domains redirects to the main domain
        server_name domain.com domain.org;

        error_page 404 /missing.php;
        error_page 403 /forbidden.php;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_param REMOTE_ADDR $http_x_real_ip;

                # With php5-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

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

我不确定问题出在哪里,所以我链接了几个配置文件。

我用了一些本文(我没有更改端口,只是停止了 apache,安装了 nginx,然后卸载了 apache)从 Apache 转移到 Nginx。

所有计算机都运行 Debian。

我使用以下命令统计了当前每小时大约有 4600 个请求:sudo grep -o '13/Feb/2018:09:' /var/log/nginx/access.log | wc -l

您有什么建议或可以尝试解决此问题的方法吗?


编辑

该问题似乎在高负载时发生。


更新

服务器现在似乎运行良好。我发现此链接并使用配置作为示例。然后我删除/编辑了配置以匹配基础架构(反向代理、自定义参数)。

这是过去一周/一天的服务器状态的直观表示(Monitorix)。我在图像上添加了描述。

我会让服务器按照当前的配置继续运行,看看是否会遇到任何未来的错误。

更新2

已经过去将近一周了,我希望问题已经得到解决......但问题又出现了。

现在它不断抛出这个错误:

2018/02/17 18:52:21 [error] 18185#18185: *2065 connect() to unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: 66.249.x.x, server: domain.org, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "domain.org"

我还发现,通过重新启动 php5-fpm,服务器速度变得非常快,但仅仅持续了大约 10 秒。

我觉得很奇怪,服务器已经运行正常一个星期了,现在突然又出现这个问题?

RAM 为 1 GB / 4 GB,查看 monitorix 时唯一明显的区别(除了所有网络活动丢失)是。最后出现的问题是由于网站瘫痪,但我认为这并不重要。

有什么建议吗?

更新 3

过去一周我一直在密切关注服务器,它非常不稳定。有时速度很快,有时网页加载可能需要大约 20 秒。

我更改了 nginx.conf 的配置,但似乎没有多大帮助。我开始认为问题出在 PHP 上,而不是 nginx 上。我发现这个问题链接到了一篇文章。我将 www.conf 中的几条 pm 改成了:

pm.max_children = 20
pm.start_servers = 8
pm.min_spare_servers = 8
pm.max_spare_servers = 15
pm.max_requests = 500

在过去的一个小时里,响应时间约为 400 毫秒。

我将等待一段时间,看看问题是否再次出现。

答案1

首先,我会尝试找出哪个环节导致网站运行缓慢。

  1. 使用以下方法检查您的网站速度是否缓慢www.webpagetest.org

    • 如果“第一个字节的时间”值很高->可能是网络、dns、缓存或 nginx 相关的问题。

    • 如果初始请求需要很长时间才能完成->可能是 PHP、数据库、应用程序或缓存相关的问题。

  2. 激活 nginx 日志记录的时间变量,以便您可以从日志文件中获取更多信息。请参阅使用 NGINX 日志进行应用程序性能监控. 检查反向代理和应用程序服务器上的 nginx 日志。

  3. 如果在更高负载下问题变得更糟,您可以使用该工具模拟负载ab。例如,要发送 3000 个请求和 100 个并发请求,请使用:

    ab -c 100 -n 3000 http://domain.de/
    

    我还用它来检查我的 PHP-fpm 池套接字文件设置在负载下是否运行良好。

  4. 缓慢程度是否取决于文件大小?查找数据包大小、缓冲和保持活动值。

  5. 其他问题?硬盘故障、切换问题、服务器上的其他进程占用 CPU/RAM?

答案2

于是过了将近半年之后,问题又出现了。。可能是因为访问量增加了近一倍,所以需要更多的资源。

我下载了一个新的监控工具,叫做网络数据,并在 php5-fpm 重启后仔细查看找到了这个

当 PHP 套接字达到最大容量时,网站就会变慢。所以我找到了问题所在。

增加后PHP 资源,它又运行良好了

这些是我当前的设置/etc/php5/fpm/pool.d/www.conf

pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 2000

是的,这不是解决 PHP 过载的问题,而是给予它更多的资源。

相关内容