M
M
multiscripter2017-02-26 19:49:26
PHP
multiscripter, 2017-02-26 19:49:26

Why is there a surge in the load on the processor when requesting site pages and how to remove or reduce this surge?

Hello everyone.
Anamnesis:
I go to the VPS via SSH, turn on htop, in the browser, when I go to the site or from one page of the site to another, I observe a sharp surge in cpu load up to 15% in htop. It turns out that the server can put 7 simultaneous requests for site pages?
Hardware:
Xeon 1x2.4Ghz, RAM 1GB DDR4, HDD 5GB SSD.
Software:
Debian 7, Nginx 1.10.3, PHP5-FPM 5.4.45-0+deb7u6, MySQL 5.5.54-0+deb7u1.
Site on WordPress.
Question two:
Why is there a surge in processor load when requesting site pages?
How can this surge be removed or reduced?
I strongly suspect that I did not screw something up in the settings. Therefore, I ask for help from more experienced server admins and webmasters. Thanks in advance.
nginx.conf

# Server globals
user              admin;
worker_processes  1;
error_log         /var/log/nginx/error.log;
pid               /var/run/nginx.pid;

events {
    worker_connections  1024;
    use                 epoll;
}

http {
    #Disable SSLv3 - CVE-2014-3566: http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-3566
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    # Main settings
    sendfile                        on;
    tcp_nopush                      on;
    tcp_nodelay                     on;
    client_header_timeout           1m;
    client_body_timeout             1m;
    client_header_buffer_size       2k;
    client_body_buffer_size         256k;
    client_max_body_size            8m;
    large_client_header_buffers     4   8k;
    send_timeout                    30;
    keepalive_timeout               60 60;
    reset_timedout_connection       on;
    server_tokens                   off;
    server_name_in_redirect         off;
    server_names_hash_max_size      512;
    server_names_hash_bucket_size   512;

    # Log format
    log_format main '[$time_local] $host $server_addr $remote_addr $status $body_bytes_sent $request_time $request $http_referer $http_user_agent';
    access_log /var/log/nginx/access.log main;
    
    # Mime settings
    include             /etc/nginx/mime.types;
    default_type        text/html;

    # Compression
    gzip            on;
    gzip_disable    "MSIE [1-6]\.(?!.*SV1)";
    gzip_proxied    any;
    gzip_min_length 1024;
    gzip_comp_level 5;
    gzip_types      text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript application/atom+xml application/rdf+xml image/svg+xml application/x-font-ttf font/opentype;

    # Proxy settings
    proxy_redirect           off;
    proxy_set_header         Host            $host;
    proxy_set_header         X-Real-IP       $remote_addr;
    proxy_set_header         X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass_header        Set-Cookie;
    proxy_connect_timeout    90;
    proxy_send_timeout       90;
    proxy_read_timeout       90;
    proxy_buffers            32 4k;
    
    # FastCGI settings
    fastcgi_buffers 16 16k; 
    fastcgi_buffer_size 32k;

    # Cache
    proxy_cache_path       /var/cache/nginx levels=2 keys_zone=cache:10m inactive=60m max_size=512m;
    proxy_cache_key        "$host$request_uri $cookie_user";
    proxy_temp_path        /var/cache/nginx/temp;
    proxy_ignore_headers   Expires Cache-Control;
    proxy_cache_use_stale  error timeout invalid_header http_502;
    proxy_cache_valid any  3d;

    map $http_cookie $no_cache {
        default 0;
        ~SESS 1;
        ~wordpress_logged_in 1;
    }
    
    limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;
    
    include /etc/nginx/blockips.conf;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
}

/etc/nginx/conf.d/default.conf;
# Default server
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    # Load default error page if not secified
    include /etc/nginx/error-pages.conf;
}

/etc/nginx/sites-enabled/sitename.conf;
server {
    listen ip_сервера:80;
    server_name sitename.ru www.sitename.ru;
    
    root /var/www/admin/www/sitename.ru/;
    index index.php index.html index.htm;
    
    #access_log /var/www/admin/logs/sitename.ru.access.log;
    access_log off;
    error_log /var/www/admin/logs/sitename.ru.error.log;

    include /etc/nginx/wp-supercache.conf;
    include /etc/nginx/expires.conf;
    
    location ~* /wp-includes/js/tinymce/wp-tinymce.php {
        include /etc/nginx/php-fpm.conf;
    }
    
    location ~* /(cgi-bin|wp-content|wp-includes)/.+\.php$ {
        deny all;
    }
    
    location ~* /wp-login.php {
        limit_req zone=login burst=3;
        auth_basic              "staff only";
        auth_basic_user_file    $document_root/.htpasswd;
        include /etc/nginx/php-fpm.conf;
    }
    
    include /etc/nginx/php-fpm.conf;
    
    location / {
        #try_files $uri $uri/ /index.php?$args;
        try_files $cachefile $uri $uri/ /index.php?$args;
    }
    
    location = /xmlrpc.php {
        deny all;
    }
    
    location ~ /\.ht {
    deny all;
  }
}

/etc/nginx/wp-supercache.conf;
set $cache_uri $request_uri;

# Don`t cache POST requests.
if ($request_method = POST) {
    set $cache_uri 'null cache';
}
# Don`t cache URL`s with GET-params.
#if ($query_string != "") {
#    set $cache_uri 'null cache';
#}

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php
                      |wp-.*.php|/feed/|index.php|wp-comments-popup.php
                      |wp-links-opml.php|wp-locations.php |sitemap(_index)?.xml
                      |[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {

    set $cache_uri 'null cache';
}

# Don't use the cache for logged-in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+
                     |wp-postpass|wordpress_logged_in") {
    set $cache_uri 'null cache';
}

# Set the cache file
set $cachefile "/wp-content/cache/supercache/$http_host/$cache_uri/index.html";
if ($https ~* "on") {
    set $cachefile "/wp-content/cache/supercache/$http_host/$cache_uri/index-https.html";
}

# Add cache file debug info as header
#add_header X-Cache-File $cachefile;

/etc/nginx/php-fpm.conf;
location ~ \.php$ {
   try_files $uri =404;
   fastcgi_split_path_info ^(.+?\.php)(/.*)$;
   fastcgi_pass unix:/var/run/php5-fpm.sock;
   fastcgi_index index.php;
   include fastcgi_params;
#   fastcgi_param DOCUMENT_ROOT $document_root;
#   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

fastcgi_params
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
#fastcgi_param  PATH_INFO          $fastcgi_path_info;
#fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

Answer the question

In order to leave comments, you need to log in

7 answer(s)
N
nirvimel, 2017-02-26
@nirvimel

when I go to a site or from one page of the site to another, I see a sharp spike in cpu load in htop up to 15%

When the director asks the secretary to make him coffee, she turns on the kettle and continues to play solitaire. At the same time, she is busy playing 85% of the time (15% fiddling with the kettle).
Question: can we put the secretary down if the whole department (7 people) ask her for coffee at the same time?

I
Ivan, 2017-02-26
@LiguidCool

7 requests will not put the server down, just the responses from the server will take a little longer. But in general, look at what kind of code you have. Of course, it depends on the computer, but 7% is too much... Although, of course, depending on which VPS.
By the way, you wrote about caching with compression - perhaps the plugin is a curve and presses files all the time. Compression is a costly process.

Y
Yuri Chudnovsky, 2017-02-26
@Frankenstine

I observe in htop a sharp surge in cpu load up to 15%

15% for how long? If for a second, this is strange and worth sounding the alarm, but if for a millisecond, this is normal and you can relax. When the processor has a job, it works, i.e. loaded at 100% until there is a delay somewhere that needs to wait (slow I/O), or until the job is finished.

K
ky0, 2017-02-26
@ky0

I recommend focusing primarily on applied parameters, not numbers. Does the site open slowly, does it give any errors with a large number of hits? If there are no symptoms - maybe everything is working really well?

V
Viktor Taran, 2017-02-27
@shambler81

worker_processes 1; - change to auto
if you have more than 1.6
it's really 1 worker.
Now, according to the page, 99% that this is a non-cached heavy query in the database.
As a result, your IO sags, as a result of which there is a peak-like collapse in performance.
As a result, the site dies either completely or temporarily.
Solution
1 to optimize the query in the database
2. cache everything alive (did not specify the engine)
3. iotop -okawhile the page is running?
4. what the script does.
5. Loading is how much? 1-10 hits per second or 10000 ?

E
Egor Zhivagin, 2017-11-10
@Krasnodar_etc

In short, Angular + canvas

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question